oj 3.11.5 → 3.16.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +1421 -0
- data/README.md +19 -5
- data/RELEASE_NOTES.md +61 -0
- data/ext/oj/buf.h +20 -6
- data/ext/oj/cache.c +329 -0
- data/ext/oj/cache.h +22 -0
- data/ext/oj/cache8.c +10 -9
- data/ext/oj/circarray.c +8 -6
- data/ext/oj/circarray.h +2 -2
- data/ext/oj/code.c +19 -33
- data/ext/oj/code.h +2 -2
- data/ext/oj/compat.c +27 -77
- data/ext/oj/custom.c +86 -179
- data/ext/oj/debug.c +126 -0
- data/ext/oj/dump.c +256 -249
- data/ext/oj/dump.h +26 -12
- data/ext/oj/dump_compat.c +565 -642
- data/ext/oj/dump_leaf.c +17 -63
- data/ext/oj/dump_object.c +65 -187
- data/ext/oj/dump_strict.c +27 -51
- data/ext/oj/encoder.c +43 -0
- data/ext/oj/err.c +2 -13
- data/ext/oj/err.h +24 -8
- data/ext/oj/extconf.rb +21 -6
- data/ext/oj/fast.c +149 -149
- data/ext/oj/intern.c +313 -0
- data/ext/oj/intern.h +22 -0
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +121 -106
- data/ext/oj/object.c +85 -162
- data/ext/oj/odd.c +89 -67
- data/ext/oj/odd.h +15 -15
- data/ext/oj/oj.c +542 -411
- data/ext/oj/oj.h +99 -73
- data/ext/oj/parse.c +175 -187
- data/ext/oj/parse.h +26 -24
- data/ext/oj/parser.c +1600 -0
- data/ext/oj/parser.h +101 -0
- data/ext/oj/rails.c +112 -159
- data/ext/oj/rails.h +1 -1
- data/ext/oj/reader.c +11 -14
- data/ext/oj/reader.h +4 -2
- data/ext/oj/resolve.c +5 -24
- data/ext/oj/rxclass.c +7 -6
- data/ext/oj/rxclass.h +1 -1
- data/ext/oj/saj.c +22 -33
- data/ext/oj/saj2.c +584 -0
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +5 -28
- data/ext/oj/sparse.c +28 -72
- data/ext/oj/stream_writer.c +50 -40
- data/ext/oj/strict.c +56 -61
- data/ext/oj/string_writer.c +72 -39
- data/ext/oj/trace.h +31 -4
- data/ext/oj/usual.c +1218 -0
- data/ext/oj/usual.h +69 -0
- data/ext/oj/util.h +1 -1
- data/ext/oj/val_stack.c +14 -3
- data/ext/oj/val_stack.h +8 -7
- data/ext/oj/validate.c +46 -0
- data/ext/oj/wab.c +63 -88
- data/lib/oj/active_support_helper.rb +1 -3
- data/lib/oj/bag.rb +7 -1
- data/lib/oj/easy_hash.rb +4 -5
- data/lib/oj/error.rb +1 -2
- data/lib/oj/json.rb +162 -150
- data/lib/oj/mimic.rb +9 -7
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/schandler.rb +5 -4
- data/lib/oj/state.rb +12 -8
- data/lib/oj/version.rb +1 -2
- data/lib/oj.rb +2 -0
- data/pages/Compatibility.md +1 -1
- data/pages/InstallOptions.md +20 -0
- data/pages/JsonGem.md +15 -0
- data/pages/Modes.md +8 -3
- data/pages/Options.md +43 -5
- data/pages/Parser.md +309 -0
- data/pages/Rails.md +14 -2
- data/test/_test_active.rb +8 -9
- data/test/_test_active_mimic.rb +7 -8
- data/test/_test_mimic_rails.rb +17 -20
- data/test/activerecord/result_test.rb +5 -6
- data/test/activesupport6/encoding_test.rb +63 -28
- data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
- data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
- data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
- data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
- data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
- data/test/files.rb +15 -15
- data/test/foo.rb +16 -45
- data/test/helper.rb +11 -8
- data/test/isolated/shared.rb +3 -2
- data/test/json_gem/json_addition_test.rb +2 -2
- data/test/json_gem/json_common_interface_test.rb +8 -6
- data/test/json_gem/json_encoding_test.rb +0 -0
- data/test/json_gem/json_ext_parser_test.rb +1 -0
- data/test/json_gem/json_fixtures_test.rb +3 -2
- data/test/json_gem/json_generator_test.rb +56 -38
- data/test/json_gem/json_generic_object_test.rb +11 -11
- data/test/json_gem/json_parser_test.rb +54 -47
- data/test/json_gem/json_string_matching_test.rb +9 -9
- data/test/json_gem/test_helper.rb +7 -3
- data/test/mem.rb +34 -0
- data/test/perf.rb +22 -27
- data/test/perf_compat.rb +31 -33
- data/test/perf_dump.rb +50 -0
- data/test/perf_fast.rb +80 -82
- data/test/perf_file.rb +27 -29
- data/test/perf_object.rb +65 -69
- data/test/perf_once.rb +59 -0
- data/test/perf_parser.rb +183 -0
- data/test/perf_saj.rb +46 -54
- data/test/perf_scp.rb +58 -69
- data/test/perf_simple.rb +41 -39
- data/test/perf_strict.rb +74 -82
- data/test/perf_wab.rb +67 -69
- data/test/prec.rb +5 -5
- data/test/sample/change.rb +0 -1
- data/test/sample/dir.rb +0 -1
- data/test/sample/doc.rb +0 -1
- data/test/sample/file.rb +0 -1
- data/test/sample/group.rb +0 -1
- data/test/sample/hasprops.rb +0 -1
- data/test/sample/layer.rb +0 -1
- data/test/sample/rect.rb +0 -1
- data/test/sample/shape.rb +0 -1
- data/test/sample/text.rb +0 -1
- data/test/sample.rb +16 -16
- data/test/sample_json.rb +8 -8
- data/test/test_compat.rb +95 -43
- data/test/test_custom.rb +73 -51
- data/test/test_debian.rb +7 -10
- data/test/test_fast.rb +135 -79
- data/test/test_file.rb +41 -30
- data/test/test_gc.rb +16 -5
- data/test/test_generate.rb +5 -5
- data/test/test_hash.rb +5 -5
- data/test/test_integer_range.rb +9 -9
- data/test/test_null.rb +20 -20
- data/test/test_object.rb +99 -96
- data/test/test_parser.rb +11 -0
- data/test/test_parser_debug.rb +27 -0
- data/test/test_parser_saj.rb +337 -0
- data/test/test_parser_usual.rb +251 -0
- data/test/test_rails.rb +2 -2
- data/test/test_saj.rb +10 -8
- data/test/test_scp.rb +37 -39
- data/test/test_strict.rb +40 -32
- data/test/test_various.rb +165 -84
- data/test/test_wab.rb +48 -44
- data/test/test_writer.rb +47 -47
- data/test/tests.rb +13 -5
- data/test/tests_mimic.rb +12 -3
- data/test/tests_mimic_addition.rb +12 -3
- metadata +74 -128
- data/ext/oj/hash.c +0 -131
- data/ext/oj/hash.h +0 -19
- data/ext/oj/hash_test.c +0 -491
- data/test/activesupport4/decoding_test.rb +0 -108
- data/test/activesupport4/encoding_test.rb +0 -531
- data/test/activesupport4/test_helper.rb +0 -41
- data/test/activesupport5/test_helper.rb +0 -72
- data/test/bar.rb +0 -35
- data/test/baz.rb +0 -16
- data/test/zoo.rb +0 -13
data/test/perf_scp.rb
CHANGED
@@ -1,61 +1,58 @@
|
|
1
1
|
#!/usr/bin/env ruby -wW1
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
$LOAD_PATH << '.'
|
5
|
+
$LOAD_PATH << File.join(__dir__, '../lib')
|
6
|
+
$LOAD_PATH << File.join(__dir__, '../ext')
|
7
7
|
|
8
8
|
require 'optparse'
|
9
|
-
require 'yajl'
|
9
|
+
# require 'yajl'
|
10
10
|
require 'perf'
|
11
11
|
require 'json'
|
12
12
|
require 'json/ext'
|
13
13
|
require 'oj'
|
14
14
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
15
|
+
@verbose = false
|
16
|
+
@indent = 0
|
17
|
+
@iter = 50_000
|
18
|
+
@with_bignum = false
|
19
|
+
@size = 1
|
20
20
|
|
21
21
|
opts = OptionParser.new
|
22
|
-
opts.on(
|
23
|
-
opts.on(
|
24
|
-
opts.on(
|
25
|
-
opts.on(
|
26
|
-
opts.on(
|
27
|
-
opts.on(
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
opts.on('-v', 'verbose') { @verbose = true }
|
23
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
|
24
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
|
25
|
+
opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| @size = i }
|
26
|
+
opts.on('-b', 'with bignum') { @with_bignum = true }
|
27
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
28
|
+
opts.parse(ARGV)
|
29
|
+
|
30
|
+
@obj = {
|
31
31
|
'a' => 'Alpha', # string
|
32
32
|
'b' => true, # boolean
|
33
|
-
'c' =>
|
34
|
-
'd' => [ true, [false, [-
|
33
|
+
'c' => 12_345, # number
|
34
|
+
'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
35
35
|
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
36
36
|
'f' => nil, # nil
|
37
37
|
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
38
38
|
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
39
39
|
}
|
40
|
-
|
40
|
+
@obj['g'] = 12_345_678_901_234_567_890_123_456_789 if @with_bignum
|
41
41
|
|
42
|
-
if 0 <
|
43
|
-
|
44
|
-
|
45
|
-
(4 *
|
46
|
-
|
42
|
+
if 0 < @size
|
43
|
+
ob = @obj
|
44
|
+
@obj = []
|
45
|
+
(4 * @size).times do
|
46
|
+
@obj << ob
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
50
|
-
Oj.default_options = { :indent =>
|
50
|
+
Oj.default_options = { :indent => @indent, :mode => :strict, cache_keys: true, cache_str: 5 }
|
51
51
|
|
52
|
-
|
53
|
-
|
52
|
+
@json = Oj.dump(@obj)
|
53
|
+
@failed = {} # key is same as String used in tests later
|
54
54
|
|
55
55
|
class AllSaj < Oj::Saj
|
56
|
-
def initialize()
|
57
|
-
end
|
58
|
-
|
59
56
|
def hash_start(key)
|
60
57
|
end
|
61
58
|
|
@@ -73,31 +70,24 @@ class AllSaj < Oj::Saj
|
|
73
70
|
end # AllSaj
|
74
71
|
|
75
72
|
class NoSaj < Oj::Saj
|
76
|
-
def initialize()
|
77
|
-
end
|
78
73
|
end # NoSaj
|
79
74
|
|
80
75
|
class NoHandler < Oj::ScHandler
|
81
|
-
def initialize()
|
82
|
-
end
|
83
76
|
end # NoHandler
|
84
77
|
|
85
78
|
class AllHandler < Oj::ScHandler
|
86
|
-
def
|
79
|
+
def hash_start
|
80
|
+
nil
|
87
81
|
end
|
88
82
|
|
89
|
-
def
|
90
|
-
return nil
|
83
|
+
def hash_end
|
91
84
|
end
|
92
85
|
|
93
|
-
def
|
86
|
+
def array_start
|
87
|
+
nil
|
94
88
|
end
|
95
89
|
|
96
|
-
def
|
97
|
-
return nil
|
98
|
-
end
|
99
|
-
|
100
|
-
def array_end()
|
90
|
+
def array_end
|
101
91
|
end
|
102
92
|
|
103
93
|
def add_value(value)
|
@@ -105,7 +95,7 @@ class AllHandler < Oj::ScHandler
|
|
105
95
|
|
106
96
|
def hash_set(h, key, value)
|
107
97
|
end
|
108
|
-
|
98
|
+
|
109
99
|
def array_append(a, value)
|
110
100
|
end
|
111
101
|
|
@@ -118,34 +108,33 @@ sc_handler = AllHandler.new()
|
|
118
108
|
no_handler = NoHandler.new()
|
119
109
|
|
120
110
|
def capture_error(tag, orig, load_key, dump_key, &blk)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
$failed[tag] = "#{e.class}: #{e.message}"
|
126
|
-
end
|
111
|
+
obj = blk.call(orig)
|
112
|
+
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
113
|
+
rescue Exception => e
|
114
|
+
@failed[tag] = "#{e.class}: #{e.message}"
|
127
115
|
end
|
128
116
|
|
129
117
|
# Verify that all packages dump and load correctly and return the same Object as the original.
|
130
|
-
capture_error('Yajl',
|
131
|
-
capture_error('JSON::Ext',
|
118
|
+
# capture_error('Yajl', @obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
|
119
|
+
capture_error('JSON::Ext', @obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
|
132
120
|
|
133
|
-
if
|
134
|
-
puts "json:\n#{
|
121
|
+
if @verbose
|
122
|
+
puts "json:\n#{@json}\n"
|
135
123
|
end
|
136
124
|
|
137
125
|
puts '-' * 80
|
138
|
-
puts
|
126
|
+
puts 'Parse Performance'
|
139
127
|
perf = Perf.new()
|
140
|
-
perf.add('Oj::Saj', 'all') { Oj.saj_parse(saj_handler,
|
141
|
-
perf.add('Oj::Saj', 'none') { Oj.saj_parse(no_saj,
|
142
|
-
perf.add('Oj::Scp', 'all') { Oj.sc_parse(sc_handler,
|
143
|
-
perf.add('Oj::Scp', 'none') { Oj.sc_parse(no_handler,
|
144
|
-
perf.add('
|
145
|
-
perf.add('
|
146
|
-
perf.
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
128
|
+
perf.add('Oj::Saj.all', 'all') { Oj.saj_parse(saj_handler, @json) }
|
129
|
+
perf.add('Oj::Saj.none', 'none') { Oj.saj_parse(no_saj, @json) }
|
130
|
+
perf.add('Oj::Scp.all', 'all') { Oj.sc_parse(sc_handler, @json) }
|
131
|
+
perf.add('Oj::Scp.none', 'none') { Oj.sc_parse(no_handler, @json) }
|
132
|
+
perf.add('Oj::load', 'none') { Oj.wab_load(@json) }
|
133
|
+
# perf.add('Yajl', 'parse') { Yajl::Parser.parse(@json) } unless @failed.has_key?('Yajl')
|
134
|
+
perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new(@json).parse } unless @failed.key?('JSON::Ext')
|
135
|
+
perf.run(@iter)
|
136
|
+
|
137
|
+
unless @failed.empty?
|
138
|
+
puts 'The following packages were not included for the reason listed'
|
139
|
+
@failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
|
151
140
|
end
|
data/test/perf_simple.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby -wW1
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
$LOAD_PATH << File.join(__dir__, '../lib')
|
5
|
+
$LOAD_PATH << File.join(__dir__, '../ext')
|
6
6
|
|
7
7
|
require 'optparse'
|
8
8
|
require 'yajl'
|
@@ -14,14 +14,15 @@ require 'oj'
|
|
14
14
|
require 'ox'
|
15
15
|
|
16
16
|
class Jazz
|
17
|
-
def initialize
|
17
|
+
def initialize
|
18
18
|
@boolean = true
|
19
19
|
@number = 58
|
20
|
-
@string =
|
20
|
+
@string = 'A string'
|
21
21
|
@array = [true, false, nil]
|
22
22
|
@hash = { 'one' => 1, 'two' => 2 }
|
23
23
|
end
|
24
|
-
|
24
|
+
|
25
|
+
def to_json(*_args)
|
25
26
|
%{
|
26
27
|
{ "boolean":#{@boolean},
|
27
28
|
"number":#{@number},
|
@@ -31,7 +32,8 @@ class Jazz
|
|
31
32
|
}
|
32
33
|
}
|
33
34
|
end
|
34
|
-
|
35
|
+
|
36
|
+
def to_hash
|
35
37
|
{ 'boolean' => @boolean,
|
36
38
|
'number' => @number,
|
37
39
|
'string' => @string,
|
@@ -39,37 +41,38 @@ class Jazz
|
|
39
41
|
'hash' => @hash,
|
40
42
|
}
|
41
43
|
end
|
44
|
+
|
42
45
|
def to_msgpack(out='')
|
43
46
|
to_hash().to_msgpack(out)
|
44
47
|
end
|
45
48
|
end
|
46
49
|
|
47
50
|
$indent = 2
|
48
|
-
$iter =
|
51
|
+
$iter = 10_000
|
49
52
|
$with_object = true
|
50
53
|
$with_bignum = true
|
51
54
|
$with_nums = true
|
52
55
|
|
53
56
|
opts = OptionParser.new
|
54
|
-
opts.on(
|
55
|
-
opts.on(
|
56
|
-
opts.on(
|
57
|
-
opts.on(
|
58
|
-
opts.on(
|
59
|
-
opts.on(
|
60
|
-
|
57
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| $iter = i }
|
58
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| $indent = i }
|
59
|
+
opts.on('-o', 'without objects') { $with_object = false }
|
60
|
+
opts.on('-b', 'without bignum') { $with_bignum = false }
|
61
|
+
opts.on('-n', 'without numbers') { $with_nums = false }
|
62
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
63
|
+
opts.parse(ARGV)
|
61
64
|
|
62
65
|
if $with_nums
|
63
66
|
obj = {
|
64
67
|
'a' => 'Alpha',
|
65
68
|
'b' => true,
|
66
|
-
'c' =>
|
67
|
-
'd' => [ true, [false, [
|
69
|
+
'c' => 12_345,
|
70
|
+
'd' => [ true, [false, [12_345, nil], 3.967, ['something', false], nil]],
|
68
71
|
'e' => { 'one' => 1, 'two' => 2 },
|
69
72
|
'f' => nil,
|
70
73
|
}
|
71
74
|
obj['g'] = Jazz.new() if $with_object
|
72
|
-
obj['h'] =
|
75
|
+
obj['h'] = 12_345_678_901_234_567_890_123_456_789 if $with_bignum
|
73
76
|
else
|
74
77
|
obj = {
|
75
78
|
'a' => 'Alpha',
|
@@ -102,7 +105,7 @@ end
|
|
102
105
|
dt = Time.now - start
|
103
106
|
base_dt = dt
|
104
107
|
parse_results[:oj] = dt
|
105
|
-
puts
|
108
|
+
puts '%d Oj.load()s in %0.3f seconds or %0.1f loads/msec' % [$iter, dt, $iter/dt/1000.0]
|
106
109
|
|
107
110
|
start = Time.now
|
108
111
|
$iter.times do
|
@@ -114,7 +117,7 @@ if base_dt < dt
|
|
114
117
|
base_name = 'Yajl'
|
115
118
|
end
|
116
119
|
parse_results[:yajl] = dt
|
117
|
-
puts
|
120
|
+
puts '%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec' % [$iter, dt, $iter/dt/1000.0]
|
118
121
|
|
119
122
|
begin
|
120
123
|
JSON.parser = JSON::Ext::Parser
|
@@ -128,7 +131,7 @@ begin
|
|
128
131
|
base_name = 'JSON::Ext'
|
129
132
|
end
|
130
133
|
parse_results[:ext] = dt
|
131
|
-
puts
|
134
|
+
puts '%d JSON::Ext::Parser parse()s in %0.3f seconds or %0.1f parses/msec' % [$iter, dt, $iter/dt/1000.0]
|
132
135
|
rescue Exception => e
|
133
136
|
puts "JSON::Ext failed: #{e.class}: #{e.message}"
|
134
137
|
end
|
@@ -145,7 +148,7 @@ begin
|
|
145
148
|
base_name = 'JSON::Pure'
|
146
149
|
end
|
147
150
|
parse_results[:pure] = dt
|
148
|
-
puts
|
151
|
+
puts '%d JSON::Pure::Parser parse()s in %0.3f seconds or %0.1f parses/msec' % [$iter, dt, $iter/dt/1000.0]
|
149
152
|
rescue Exception => e
|
150
153
|
puts "JSON::Pure failed: #{e.class}: #{e.message}"
|
151
154
|
end
|
@@ -162,7 +165,7 @@ begin
|
|
162
165
|
base_name = 'MessagePack'
|
163
166
|
end
|
164
167
|
parse_results[:msgpack] = dt
|
165
|
-
puts
|
168
|
+
puts '%d MessagePack.unpack()s in %0.3f seconds or %0.1f packs/msec' % [$iter, dt, $iter/dt/1000.0]
|
166
169
|
rescue Exception => e
|
167
170
|
puts "MessagePack failed: #{e.class}: #{e.message}"
|
168
171
|
end
|
@@ -173,23 +176,22 @@ $iter.times do
|
|
173
176
|
end
|
174
177
|
dt = Time.now - start
|
175
178
|
parse_results[:ox] = dt
|
176
|
-
puts
|
179
|
+
puts '%d Ox.load()s in %0.3f seconds or %0.1f loads/msec' % [$iter, dt, $iter/dt/1000.0]
|
177
180
|
|
178
|
-
puts
|
181
|
+
puts 'Parser results:'
|
179
182
|
puts "gem seconds parses/msec X faster than #{base_name} (higher is better)"
|
180
|
-
parse_results.each do |name,
|
181
|
-
if 0.0
|
183
|
+
parse_results.each do |name, dt2|
|
184
|
+
if dt2 <= 0.0
|
182
185
|
puts "#{name} failed to generate JSON"
|
183
186
|
next
|
184
187
|
end
|
185
|
-
puts
|
188
|
+
puts '%-7s %6.3f %5.1f %4.1f' % [name, dt2, $iter/dt/1000.0, base_dt/dt2]
|
186
189
|
end
|
187
190
|
|
188
191
|
puts
|
189
192
|
|
190
193
|
# Back to object mode for best performance when dumping.
|
191
194
|
Oj.default_options = { :indent => $indent, :mode => :object }
|
192
|
-
dump_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }
|
193
195
|
|
194
196
|
start = Time.now
|
195
197
|
$iter.times do
|
@@ -199,7 +201,7 @@ dt = Time.now - start
|
|
199
201
|
base_dt = dt
|
200
202
|
base_name = 'Oj'
|
201
203
|
parse_results[:oj] = dt
|
202
|
-
puts
|
204
|
+
puts '%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec' % [$iter, dt, $iter/dt/1000.0]
|
203
205
|
|
204
206
|
start = Time.now
|
205
207
|
$iter.times do
|
@@ -211,7 +213,7 @@ if base_dt < dt
|
|
211
213
|
base_name = 'Yajl'
|
212
214
|
end
|
213
215
|
parse_results[:yajl] = dt
|
214
|
-
puts
|
216
|
+
puts '%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec' % [$iter, dt, $iter/dt/1000.0]
|
215
217
|
|
216
218
|
begin
|
217
219
|
JSON.parser = JSON::Ext::Parser
|
@@ -225,7 +227,7 @@ begin
|
|
225
227
|
base_name = 'JSON::Ext'
|
226
228
|
end
|
227
229
|
parse_results[:pure] = dt
|
228
|
-
puts
|
230
|
+
puts '%d JSON::Ext generate()s in %0.3f seconds or %0.1f generates/msec' % [$iter, dt, $iter/dt/1000.0]
|
229
231
|
rescue Exception => e
|
230
232
|
parse_results[:ext] = 0.0
|
231
233
|
puts "JSON::Ext failed: #{e.class}: #{e.message}"
|
@@ -243,7 +245,7 @@ begin
|
|
243
245
|
base_name = 'JSON::Pure'
|
244
246
|
end
|
245
247
|
parse_results[:pure] = dt
|
246
|
-
puts
|
248
|
+
puts '%d JSON::Pure generate()s in %0.3f seconds or %0.1f generates/msec' % [$iter, dt, $iter/dt/1000.0]
|
247
249
|
rescue Exception => e
|
248
250
|
parse_results[:pure] = 0.0
|
249
251
|
puts "JSON::Pure failed: #{e.class}: #{e.message}"
|
@@ -260,7 +262,7 @@ begin
|
|
260
262
|
base_name = 'MessagePack'
|
261
263
|
end
|
262
264
|
parse_results[:msgpack] = dt
|
263
|
-
puts
|
265
|
+
puts '%d Msgpack()s in %0.3f seconds or %0.1f unpacks/msec' % [$iter, dt, $iter/dt/1000.0]
|
264
266
|
rescue Exception => e
|
265
267
|
parse_results[:msgpack] = 0.0
|
266
268
|
puts "MessagePack failed: #{e.class}: #{e.message}"
|
@@ -272,16 +274,16 @@ $iter.times do
|
|
272
274
|
end
|
273
275
|
dt = Time.now - start
|
274
276
|
parse_results[:ox] = dt
|
275
|
-
puts
|
277
|
+
puts '%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec' % [$iter, dt, $iter/dt/1000.0]
|
276
278
|
|
277
|
-
puts
|
279
|
+
puts 'Parser results:'
|
278
280
|
puts "gem seconds dumps/msec X faster than #{base_name} (higher is better)"
|
279
|
-
parse_results.each do |name,
|
280
|
-
if 0.0
|
281
|
+
parse_results.each do |name, dt2|
|
282
|
+
if dt2 <= 0.0
|
281
283
|
puts "#{name} failed to generate JSON"
|
282
284
|
next
|
283
285
|
end
|
284
|
-
puts
|
286
|
+
puts '%-7s %6.3f %5.1f %4.1f' % [name, dt2, $iter/dt/1000.0, base_dt/dt2]
|
285
287
|
end
|
286
288
|
|
287
289
|
puts
|
data/test/perf_strict.rb
CHANGED
@@ -1,45 +1,49 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
-
#
|
2
|
+
# frozen_string_literal: true
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
4
|
+
$LOAD_PATH << '.'
|
5
|
+
$LOAD_PATH << File.join(__dir__, '../lib')
|
6
|
+
$LOAD_PATH << File.join(__dir__, '../ext')
|
7
7
|
|
8
8
|
require 'optparse'
|
9
9
|
require 'perf'
|
10
10
|
require 'oj'
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
12
|
+
@verbose = false
|
13
|
+
@indent = 0
|
14
|
+
@iter = 20_000
|
15
|
+
@with_bignum = false
|
16
|
+
@with_nums = true
|
17
|
+
@size = 0
|
18
|
+
@symbolize = false
|
19
|
+
@cache_keys = true
|
18
20
|
|
19
21
|
opts = OptionParser.new
|
20
|
-
opts.on(
|
21
|
-
opts.on(
|
22
|
-
opts.on(
|
23
|
-
opts.on(
|
24
|
-
opts.on(
|
25
|
-
opts.on(
|
26
|
-
opts.on(
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
22
|
+
opts.on('-v', 'verbose') { @verbose = true }
|
23
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
|
24
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
|
25
|
+
opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| @size = i }
|
26
|
+
opts.on('-b', 'with bignum') { @with_bignum = true }
|
27
|
+
opts.on('-n', 'without numbers') { @with_nums = false }
|
28
|
+
opts.on('-z', '--symbolize', 'symbolize keys') { @symbolize = true }
|
29
|
+
opts.on('-k', '--no-cache', 'turn off key caching') { @cache_keys = false }
|
30
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
31
|
+
opts.parse(ARGV)
|
32
|
+
|
33
|
+
if @with_nums
|
34
|
+
@obj = {
|
31
35
|
'a' => 'Alpha', # string
|
32
36
|
'b' => true, # boolean
|
33
|
-
'c' =>
|
34
|
-
'd' => [ true, [false, [-
|
37
|
+
'c' => 12_345, # number
|
38
|
+
'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
35
39
|
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
36
40
|
'f' => nil, # nil
|
37
41
|
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
38
42
|
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
39
43
|
}
|
40
|
-
|
44
|
+
@obj['g'] = 12_345_678_901_234_567_890_123_456_789 if @with_bignum
|
41
45
|
else
|
42
|
-
|
46
|
+
@obj = {
|
43
47
|
'a' => 'Alpha',
|
44
48
|
'b' => true,
|
45
49
|
'c' => '12345',
|
@@ -51,95 +55,83 @@ else
|
|
51
55
|
}
|
52
56
|
end
|
53
57
|
|
54
|
-
Oj.default_options = { :indent =>
|
58
|
+
Oj.default_options = { :indent => @indent, :mode => :strict, cache_keys: @cache_keys, cache_str: 5 }
|
55
59
|
|
56
|
-
if 0 <
|
57
|
-
|
58
|
-
|
59
|
-
(4 *
|
60
|
-
|
60
|
+
if 0 < @size
|
61
|
+
ob = @obj
|
62
|
+
@obj = []
|
63
|
+
(4 * @size).times do
|
64
|
+
@obj << ob
|
61
65
|
end
|
62
66
|
end
|
63
67
|
|
64
|
-
|
65
|
-
|
66
|
-
#puts "*** size: #{$obj_json.size}"
|
67
|
-
#puts "*** #{$obj_json}"
|
68
|
-
$failed = {} # key is same as String used in tests later
|
68
|
+
@json = Oj.dump(@obj)
|
69
|
+
@failed = {} # key is same as String used in tests later
|
69
70
|
|
70
71
|
def capture_error(tag, orig, load_key, dump_key, &blk)
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
$failed[tag] = "#{e.class}: #{e.message}"
|
76
|
-
end
|
72
|
+
obj = blk.call(orig)
|
73
|
+
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
74
|
+
rescue Exception => e
|
75
|
+
@failed[tag] = "#{e.class}: #{e.message}"
|
77
76
|
end
|
78
77
|
|
79
78
|
# Verify that all packages dump and load correctly and return the same Object as the original.
|
80
|
-
capture_error('Oj:strict',
|
81
|
-
|
82
|
-
|
79
|
+
capture_error('Oj:strict', @obj, 'load', 'dump') { |o|
|
80
|
+
Oj.strict_load(Oj.dump(o))
|
81
|
+
}
|
82
|
+
capture_error('Yajl', @obj, 'encode', 'parse') { |o|
|
83
|
+
require 'yajl'
|
84
|
+
Yajl::Parser.parse(Yajl::Encoder.encode(o))
|
85
|
+
}
|
86
|
+
capture_error('JSON::Ext', @obj, 'generate', 'parse') { |o|
|
83
87
|
require 'json'
|
84
88
|
require 'json/ext'
|
85
89
|
JSON.generator = JSON::Ext::Generator
|
86
90
|
JSON.parser = JSON::Ext::Parser
|
87
91
|
JSON.parse(JSON.generate(o))
|
88
92
|
}
|
89
|
-
capture_error('JSON::Pure', $obj, 'generate', 'parse') { |o|
|
90
|
-
require 'json/pure'
|
91
|
-
JSON.generator = JSON::Pure::Generator
|
92
|
-
JSON.parser = JSON::Pure::Parser
|
93
|
-
JSON.parse(JSON.generate(o))
|
94
|
-
}
|
95
93
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
puts "
|
100
|
-
puts "
|
101
|
-
puts "
|
94
|
+
Oj.default_options = { symbol_keys: @symbolize }
|
95
|
+
|
96
|
+
if @verbose
|
97
|
+
puts "json:\n#{@json}\n"
|
98
|
+
puts "Oj loaded object:\n#{Oj.strict_load(@json)}\n"
|
99
|
+
puts "Yajl loaded object:\n#{Yajl::Parser.parse(@json)}\n"
|
100
|
+
puts "JSON loaded object:\n#{JSON::Ext::Parser.new(@json).parse}\n"
|
102
101
|
end
|
103
102
|
|
104
103
|
puts '-' * 80
|
105
|
-
puts
|
104
|
+
puts 'Strict Parse Performance'
|
106
105
|
perf = Perf.new()
|
107
|
-
unless
|
108
|
-
perf.add('JSON::Ext', 'parse') { JSON.parse(
|
106
|
+
unless @failed.key?('JSON::Ext')
|
107
|
+
perf.add('JSON::Ext', 'parse') { JSON.parse(@json, symbolize_names: @symbolize) }
|
109
108
|
perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
|
110
109
|
end
|
111
|
-
unless
|
112
|
-
perf.add('
|
113
|
-
perf.
|
110
|
+
unless @failed.key?('Oj:strict')
|
111
|
+
perf.add('Oj:strict', 'strict_load') { Oj.strict_load(@json) }
|
112
|
+
perf.add('Oj:wab', 'wab_load') { Oj.wab_load(@json) }
|
114
113
|
end
|
115
|
-
unless
|
116
|
-
|
117
|
-
end
|
118
|
-
perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
|
119
|
-
perf.run($iter)
|
114
|
+
perf.add('Yajl', 'parse') { Yajl::Parser.parse(@json) } unless @failed.key?('Yajl')
|
115
|
+
perf.run(@iter)
|
120
116
|
|
121
117
|
puts '-' * 80
|
122
|
-
puts
|
118
|
+
puts 'Strict Dump Performance'
|
123
119
|
perf = Perf.new()
|
124
|
-
unless
|
125
|
-
perf.add('JSON::Ext', 'dump') { JSON.generate(
|
120
|
+
unless @failed.key?('JSON::Ext')
|
121
|
+
perf.add('JSON::Ext', 'dump') { JSON.generate(@obj) }
|
126
122
|
perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
|
127
123
|
end
|
128
|
-
unless
|
129
|
-
perf.add('
|
130
|
-
perf.before('JSON::Pure') { JSON.generator = JSON::Pure::Generator }
|
131
|
-
end
|
132
|
-
unless $failed.has_key?('Oj:strict')
|
133
|
-
perf.add('Oj:strict', 'dump') { Oj.dump($obj, :mode => :strict) }
|
124
|
+
unless @failed.key?('Oj:strict')
|
125
|
+
perf.add('Oj:strict', 'dump') { Oj.dump(@obj) }
|
134
126
|
end
|
135
|
-
perf.add('Yajl', 'encode') { Yajl::Encoder.encode(
|
136
|
-
perf.run(
|
127
|
+
perf.add('Yajl', 'encode') { Yajl::Encoder.encode(@obj) } unless @failed.key?('Yajl')
|
128
|
+
perf.run(@iter)
|
137
129
|
|
138
130
|
puts
|
139
131
|
puts '-' * 80
|
140
132
|
puts
|
141
133
|
|
142
|
-
unless
|
143
|
-
puts
|
144
|
-
|
134
|
+
unless @failed.empty?
|
135
|
+
puts 'The following packages were not included for the reason listed'
|
136
|
+
@failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
|
145
137
|
end
|