Skip to content

Commit

Permalink
Make benchmarks JRuby compatible
Browse files Browse the repository at this point in the history
Co-Authored-By: Charles Oliver Nutter <[email protected]>
  • Loading branch information
byroot and headius committed Jan 29, 2025
1 parent 39ef430 commit 01f0bc9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
17 changes: 12 additions & 5 deletions benchmark/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
require "benchmark/ips"
require "json"
require "date"
require "oj"

Oj.default_options = Oj.default_options.merge(mode: :compat)
begin
require "oj"
Oj.default_options = Oj.default_options.merge(mode: :compat)
rescue LoadError
end

if ENV["ONLY"]
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
Expand All @@ -18,11 +20,16 @@
def implementations(ruby_obj)
state = JSON::State.new(JSON.dump_default_options)
coder = JSON::Coder.new
{
implementations = {
json: ["json", proc { JSON.generate(ruby_obj) }],
json_coder: ["json_coder", proc { coder.dump(ruby_obj) }],
oj: ["oj", proc { Oj.dump(ruby_obj) }],
}

if defined?(Oj)
implementations[:oj] = ["oj", proc { Oj.dump(ruby_obj) }]
end

implementations
end

def benchmark_encoding(benchmark_name, ruby_obj, check_expected: true, except: [])
Expand Down
24 changes: 19 additions & 5 deletions benchmark/parser.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
require "benchmark/ips"
require "json"
require "oj"
require "rapidjson"
begin
require "oj"
rescue LoadError
end

begin
require "rapidjson"
rescue LoadError
end

if ENV["ONLY"]
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
Expand All @@ -20,9 +27,16 @@ def benchmark_parsing(name, json_output)
Benchmark.ips do |x|
x.report("json") { JSON.parse(json_output) } if RUN[:json]
x.report("json_coder") { coder.load(json_output) } if RUN[:json_coder]
x.report("oj") { Oj.load(json_output) } if RUN[:oj]
x.report("Oj::Parser") { Oj::Parser.new(:usual).parse(json_output) } if RUN[:oj]
x.report("rapidjson") { RapidJSON.parse(json_output) } if RUN[:rapidjson]

if defined?(Oj)
x.report("oj") { Oj.load(json_output) } if RUN[:oj]
x.report("Oj::Parser") { Oj::Parser.new(:usual).parse(json_output) } if RUN[:oj]
end

if defined?(RapidJSON)
x.report("rapidjson") { RapidJSON.parse(json_output) } if RUN[:rapidjson]
end

x.compare!(order: :baseline)
end
puts
Expand Down

0 comments on commit 01f0bc9

Please sign in to comment.