-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder-simple.rb
58 lines (51 loc) · 1.54 KB
/
encoder-simple.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
require "benchmark/ips"
require "json"
require "date"
require "oj"
Oj.default_options = Oj.default_options.merge(mode: :compat)
if ENV["ONLY"]
RUN = ENV["ONLY"].split(/[,: ]/).map{|x| [x.to_sym, true] }.to_h
RUN.default = false
elsif ENV["EXCEPT"]
RUN = ENV["EXCEPT"].split(/[,: ]/).map{|x| [x.to_sym, false] }.to_h
RUN.default = true
else
RUN = Hash.new(true)
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) }],
}
end
def benchmark_encoding(benchmark_name, ruby_obj, check_expected: true, except: [])
json_output = JSON.dump(ruby_obj)
puts "== Encoding #{benchmark_name} (#{json_output.bytesize} bytes)"
impls = implementations(ruby_obj).select { |name| RUN[name] }
except.each { |i| impls.delete(i) }
Benchmark.ips do |x|
expected = ::JSON.dump(ruby_obj) if check_expected
impls.values.each do |name, block|
begin
result = block.call
if check_expected && expected != result
puts "#{name} does not match expected output. Skipping"
puts "Expected:" + '-' * 40
puts expected
puts "Actual:" + '-' * 40
puts result
puts '-' * 40
next
end
rescue => error
puts "#{name} unsupported (#{error})"
next
end
x.report(name, &block)
end
x.compare!(order: :baseline)
end
puts
end
benchmark_encoding "long string", (["this is a test of the emergency broadcast system."*5]*500)