Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a simple JSON.dump and JSON.load benchmark #580

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ gem "rake"
gem "test-unit"
gem "test-unit-ruby-core"
gem "all_images", "~> 0" unless RUBY_PLATFORM =~ /java/
gem "benchmark-ips"
41 changes: 41 additions & 0 deletions benchmarks/bench.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'benchmark/ips'
eregon marked this conversation as resolved.
Show resolved Hide resolved

$:.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
Loading
Loading