File tree 2 files changed +31
-10
lines changed
2 files changed +31
-10
lines changed Original file line number Diff line number Diff line change 1
1
require "benchmark/ips"
2
2
require "json"
3
3
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
7
9
8
10
if ENV [ "ONLY" ]
9
11
RUN = ENV [ "ONLY" ] . split ( /[,: ]/ ) . map { |x | [ x . to_sym , true ] } . to_h
18
20
def implementations ( ruby_obj )
19
21
state = JSON ::State . new ( JSON . dump_default_options )
20
22
coder = JSON ::Coder . new
21
- {
23
+ implementations = {
22
24
json : [ "json" , proc { JSON . generate ( ruby_obj ) } ] ,
23
25
json_coder : [ "json_coder" , proc { coder . dump ( ruby_obj ) } ] ,
24
- oj : [ "oj" , proc { Oj . dump ( ruby_obj ) } ] ,
25
26
}
27
+
28
+ if defined? ( Oj )
29
+ implementations [ :oj ] = [ "oj" , proc { Oj . dump ( ruby_obj ) } ]
30
+ end
31
+
32
+ implementations
26
33
end
27
34
28
35
def benchmark_encoding ( benchmark_name , ruby_obj , check_expected : true , except : [ ] )
Original file line number Diff line number Diff line change 1
1
require "benchmark/ips"
2
2
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
5
12
6
13
if ENV [ "ONLY" ]
7
14
RUN = ENV [ "ONLY" ] . split ( /[,: ]/ ) . map { |x | [ x . to_sym , true ] } . to_h
@@ -20,9 +27,16 @@ def benchmark_parsing(name, json_output)
20
27
Benchmark . ips do |x |
21
28
x . report ( "json" ) { JSON . parse ( json_output ) } if RUN [ :json ]
22
29
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
+
26
40
x . compare! ( order : :baseline )
27
41
end
28
42
puts
You can’t perform that action at this time.
0 commit comments