Skip to content

Commit

Permalink
Make benchmark runnable without oj available
Browse files Browse the repository at this point in the history
  • Loading branch information
headius committed Jan 8, 2025
1 parent abe144c commit 776993a
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions benchmark/encoder.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
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
# no oj, just do json
end

if ENV["ONLY"]
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
Expand All @@ -16,11 +21,11 @@
end

def implementations(ruby_obj)
state = JSON::State.new(JSON.dump_default_options)
{
json: ["json", proc { JSON.generate(ruby_obj) }],
oj: ["oj", proc { Oj.dump(ruby_obj) }],
}
impls = { json: ["json", proc { JSON.generate(ruby_obj) }] }
if defined? Oj
impls["oj"] = proc { Oj.dump(ruby_obj) }
end
impls
end

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

0 comments on commit 776993a

Please sign in to comment.