Skip to content

Commit 01f0bc9

Browse files
byrootheadius
andcommitted
Make benchmarks JRuby compatible
Co-Authored-By: Charles Oliver Nutter <[email protected]>
1 parent 39ef430 commit 01f0bc9

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

benchmark/encoder.rb

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
require "benchmark/ips"
22
require "json"
33
require "date"
4-
require "oj"
5-
6-
Oj.default_options = Oj.default_options.merge(mode: :compat)
4+
begin
5+
require "oj"
6+
Oj.default_options = Oj.default_options.merge(mode: :compat)
7+
rescue LoadError
8+
end
79

810
if ENV["ONLY"]
911
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
@@ -18,11 +20,16 @@
1820
def implementations(ruby_obj)
1921
state = JSON::State.new(JSON.dump_default_options)
2022
coder = JSON::Coder.new
21-
{
23+
implementations = {
2224
json: ["json", proc { JSON.generate(ruby_obj) }],
2325
json_coder: ["json_coder", proc { coder.dump(ruby_obj) }],
24-
oj: ["oj", proc { Oj.dump(ruby_obj) }],
2526
}
27+
28+
if defined?(Oj)
29+
implementations[:oj] = ["oj", proc { Oj.dump(ruby_obj) }]
30+
end
31+
32+
implementations
2633
end
2734

2835
def benchmark_encoding(benchmark_name, ruby_obj, check_expected: true, except: [])

benchmark/parser.rb

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
require "benchmark/ips"
22
require "json"
3-
require "oj"
4-
require "rapidjson"
3+
begin
4+
require "oj"
5+
rescue LoadError
6+
end
7+
8+
begin
9+
require "rapidjson"
10+
rescue LoadError
11+
end
512

613
if ENV["ONLY"]
714
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
@@ -20,9 +27,16 @@ def benchmark_parsing(name, json_output)
2027
Benchmark.ips do |x|
2128
x.report("json") { JSON.parse(json_output) } if RUN[:json]
2229
x.report("json_coder") { coder.load(json_output) } if RUN[:json_coder]
23-
x.report("oj") { Oj.load(json_output) } if RUN[:oj]
24-
x.report("Oj::Parser") { Oj::Parser.new(:usual).parse(json_output) } if RUN[:oj]
25-
x.report("rapidjson") { RapidJSON.parse(json_output) } if RUN[:rapidjson]
30+
31+
if defined?(Oj)
32+
x.report("oj") { Oj.load(json_output) } if RUN[:oj]
33+
x.report("Oj::Parser") { Oj::Parser.new(:usual).parse(json_output) } if RUN[:oj]
34+
end
35+
36+
if defined?(RapidJSON)
37+
x.report("rapidjson") { RapidJSON.parse(json_output) } if RUN[:rapidjson]
38+
end
39+
2640
x.compare!(order: :baseline)
2741
end
2842
puts

0 commit comments

Comments
 (0)