-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #580 from eregon/bench
Add a simple JSON.dump and JSON.load benchmark
- Loading branch information
Showing
3 changed files
with
1,258 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'benchmark/ips' | ||
|
||
$:.unshift File.expand_path('../ext', __dir__) | ||
$:.unshift File.expand_path('../lib', __dir__) | ||
|
||
bench, mode = ARGV | ||
|
||
if mode == 'pure' | ||
require 'json/pure' | ||
else | ||
require 'json/ext' | ||
end | ||
|
||
bench_dump = bench == 'dump' | ||
if bench_dump | ||
p JSON.generator | ||
else | ||
p JSON.parser | ||
end | ||
|
||
str = File.read("#{__dir__}/ohai.json") | ||
obj = JSON.load(str) | ||
|
||
Benchmark.ips do |x| | ||
unless RUBY_ENGINE == 'ruby' | ||
x.warmup = 5 | ||
x.iterations = 5 | ||
end | ||
|
||
if bench_dump | ||
x.report('JSON.dump(obj)') do # max_nesting: false, allow_nan: true | ||
JSON.dump(obj) | ||
end | ||
else | ||
x.report('JSON.load(str)') do # max_nesting: false, allow_nan: true, allow_blank: true, create_additions: true | ||
JSON.load(str) | ||
end | ||
end | ||
|
||
x.compare! | ||
end |
Oops, something went wrong.