Skip to content

Commit

Permalink
adding headings to benchmarks, adding a benchmark for streaming psych
Browse files Browse the repository at this point in the history
  • Loading branch information
tenderlove committed Feb 21, 2011
1 parent c6868c1 commit 7fcf5fb
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions benchmark/encode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,49 @@
end

filename = ARGV[0] || 'benchmark/subjects/ohai.json'
json = File.new(filename, 'r')
hash = Yajl::Parser.new.parse(json)
json.close
hash = File.open(filename, 'rb') { |f| Yajl::Parser.new.parse(f.read) }

times = ARGV[1] ? ARGV[1].to_i : 1000
puts "Starting benchmark encoding #{filename} #{times} times\n\n"
Benchmark.bmbm { |x|
io_encoder = Yajl::Encoder.new
x.report {
puts "Yajl::Encoder#encode (to an IO)"
string_encoder = Yajl::Encoder.new

x.report("Yajl::Encoder#encode (to an IO)") {
times.times {
io_encoder.encode(hash, StringIO.new)
}
}
string_encoder = Yajl::Encoder.new
x.report {
puts "Yajl::Encoder#encode (to a String)"
x.report("Yajl::Encoder#encode (to a String)") {
times.times {
output = string_encoder.encode(hash)
}
}
if defined?(JSON)
x.report {
puts "JSON.generate"
x.report("JSON.generate") {
times.times {
JSON.generate(hash)
}
}
end
if defined?(Psych)
x.report {
puts "Psych.to_json"
x.report("Psych.to_json") {
times.times {
Psych.to_json(hash)
}
}
x.report("Psych::JSON::Stream") {
times.times {
io = StringIO.new
stream = Psych::JSON::Stream.new io
stream.start
stream.push hash
stream.finish
}
}
end
if defined?(ActiveSupport::JSON)
x.report {
puts "ActiveSupport::JSON.encode"
x.report("ActiveSupport::JSON.encode") {
times.times {
ActiveSupport::JSON.encode(hash)
}
Expand Down

0 comments on commit 7fcf5fb

Please sign in to comment.