Skip to content

Commit 80b5865

Browse files
authored
Merge pull request #580 from eregon/bench
Add a simple JSON.dump and JSON.load benchmark
2 parents 308b3d6 + 457a00e commit 80b5865

File tree

3 files changed

+1258
-0
lines changed

3 files changed

+1258
-0
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ gem "rake"
1717
gem "test-unit"
1818
gem "test-unit-ruby-core"
1919
gem "all_images", "~> 0" unless RUBY_PLATFORM =~ /java/
20+
gem "benchmark-ips"

benchmarks/bench.rb

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
require 'benchmark/ips'
2+
3+
$:.unshift File.expand_path('../ext', __dir__)
4+
$:.unshift File.expand_path('../lib', __dir__)
5+
6+
bench, mode = ARGV
7+
8+
if mode == 'pure'
9+
require 'json/pure'
10+
else
11+
require 'json/ext'
12+
end
13+
14+
bench_dump = bench == 'dump'
15+
if bench_dump
16+
p JSON.generator
17+
else
18+
p JSON.parser
19+
end
20+
21+
str = File.read("#{__dir__}/ohai.json")
22+
obj = JSON.load(str)
23+
24+
Benchmark.ips do |x|
25+
unless RUBY_ENGINE == 'ruby'
26+
x.warmup = 5
27+
x.iterations = 5
28+
end
29+
30+
if bench_dump
31+
x.report('JSON.dump(obj)') do # max_nesting: false, allow_nan: true
32+
JSON.dump(obj)
33+
end
34+
else
35+
x.report('JSON.load(str)') do # max_nesting: false, allow_nan: true, allow_blank: true, create_additions: true
36+
JSON.load(str)
37+
end
38+
end
39+
40+
x.compare!
41+
end

0 commit comments

Comments
 (0)