oj 3.13.11 → 3.15.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +74 -0
- data/README.md +4 -2
- data/ext/oj/buf.h +11 -6
- data/ext/oj/cache.c +25 -24
- 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 +17 -24
- data/ext/oj/code.h +2 -2
- data/ext/oj/compat.c +17 -44
- data/ext/oj/custom.c +70 -141
- data/ext/oj/debug.c +3 -9
- data/ext/oj/dump.c +128 -118
- data/ext/oj/dump.h +12 -8
- data/ext/oj/dump_compat.c +564 -641
- data/ext/oj/dump_leaf.c +17 -63
- data/ext/oj/dump_object.c +70 -199
- data/ext/oj/dump_strict.c +22 -46
- data/ext/oj/encoder.c +1 -1
- data/ext/oj/err.c +2 -13
- data/ext/oj/err.h +9 -12
- data/ext/oj/extconf.rb +14 -5
- data/ext/oj/fast.c +75 -103
- data/ext/oj/intern.c +52 -50
- data/ext/oj/intern.h +4 -8
- data/ext/oj/mem.c +318 -0
- data/ext/oj/mem.h +53 -0
- data/ext/oj/mimic_json.c +75 -47
- data/ext/oj/object.c +49 -66
- data/ext/oj/odd.c +89 -67
- data/ext/oj/odd.h +15 -15
- data/ext/oj/oj.c +140 -99
- data/ext/oj/oj.h +80 -51
- data/ext/oj/parse.c +162 -184
- data/ext/oj/parse.h +7 -10
- data/ext/oj/parser.c +89 -34
- data/ext/oj/parser.h +18 -7
- data/ext/oj/rails.c +82 -146
- data/ext/oj/rails.h +1 -1
- data/ext/oj/reader.c +11 -12
- data/ext/oj/reader.h +4 -2
- data/ext/oj/resolve.c +3 -4
- data/ext/oj/rxclass.c +6 -5
- data/ext/oj/rxclass.h +1 -1
- data/ext/oj/saj.c +20 -31
- data/ext/oj/saj2.c +329 -93
- data/ext/oj/saj2.h +23 -0
- data/ext/oj/scp.c +3 -14
- data/ext/oj/sparse.c +26 -70
- data/ext/oj/stream_writer.c +12 -22
- data/ext/oj/strict.c +20 -52
- data/ext/oj/string_writer.c +21 -21
- data/ext/oj/trace.h +31 -4
- data/ext/oj/usual.c +105 -150
- data/ext/oj/usual.h +68 -0
- data/ext/oj/util.h +1 -1
- data/ext/oj/val_stack.c +1 -1
- data/ext/oj/val_stack.h +8 -7
- data/ext/oj/validate.c +21 -26
- data/ext/oj/wab.c +31 -68
- data/lib/oj/active_support_helper.rb +0 -1
- data/lib/oj/bag.rb +7 -1
- data/lib/oj/easy_hash.rb +4 -5
- data/lib/oj/error.rb +0 -1
- data/lib/oj/json.rb +4 -2
- data/lib/oj/mimic.rb +4 -2
- data/lib/oj/saj.rb +20 -6
- data/lib/oj/state.rb +9 -6
- 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/Options.md +10 -0
- 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/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
- data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
- data/test/{activesupport5 → activesupport7}/encoding_test.rb +20 -34
- 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 +9 -71
- 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 +4 -4
- 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 +48 -36
- 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 +13 -12
- data/test/perf.rb +21 -26
- 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 +12 -11
- data/test/perf_parser.rb +42 -48
- data/test/perf_saj.rb +46 -54
- data/test/perf_scp.rb +57 -69
- data/test/perf_simple.rb +41 -39
- data/test/perf_strict.rb +68 -70
- data/test/perf_wab.rb +67 -69
- data/test/prec.rb +3 -3
- 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 +76 -42
- data/test/test_custom.rb +72 -51
- data/test/test_debian.rb +7 -10
- data/test/test_fast.rb +86 -90
- 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 +4 -4
- data/test/test_integer_range.rb +9 -9
- data/test/test_null.rb +20 -20
- data/test/test_object.rb +85 -96
- data/test/test_parser.rb +6 -22
- data/test/test_parser_debug.rb +27 -0
- data/test/test_parser_saj.rb +115 -23
- data/test/test_parser_usual.rb +6 -6
- 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 +30 -32
- data/test/test_various.rb +147 -99
- data/test/test_wab.rb +48 -44
- data/test/test_writer.rb +47 -47
- data/test/tests.rb +13 -4
- data/test/tests_mimic.rb +12 -3
- data/test/tests_mimic_addition.rb +12 -3
- metadata +33 -144
- 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 -16
- data/test/baz.rb +0 -16
- data/test/bug.rb +0 -16
- data/test/zoo.rb +0 -13
data/test/perf.rb
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
+
# frozen_string_literal: true
|
1
2
|
|
2
3
|
class Perf
|
3
4
|
|
4
|
-
def initialize
|
5
|
+
def initialize
|
5
6
|
@items = []
|
6
7
|
end
|
7
8
|
|
@@ -19,7 +20,7 @@ class Perf
|
|
19
20
|
end
|
20
21
|
|
21
22
|
def run(iter)
|
22
|
-
base = Item.new(nil, nil) {
|
23
|
+
base = Item.new(nil, nil) {}
|
23
24
|
base.run(iter, 0.0)
|
24
25
|
@items.each do |i|
|
25
26
|
i.run(iter, base.duration)
|
@@ -32,35 +33,31 @@ class Perf
|
|
32
33
|
summary()
|
33
34
|
end
|
34
35
|
|
35
|
-
def summary
|
36
|
-
fastest = nil
|
37
|
-
slowest = nil
|
36
|
+
def summary
|
38
37
|
width = 6
|
39
38
|
@items.each do |i|
|
40
39
|
next if i.duration.nil?
|
40
|
+
|
41
41
|
width = i.title.size if width < i.title.size
|
42
42
|
end
|
43
43
|
iva = @items.clone
|
44
44
|
iva.delete_if { |i| i.duration.nil? }
|
45
45
|
iva = iva.sort_by { |i| i.duration }
|
46
46
|
puts
|
47
|
-
puts
|
48
|
-
puts
|
47
|
+
puts 'Summary:'
|
48
|
+
puts '%*s time (secs) rate (ops/sec)' % [width, 'System']
|
49
49
|
puts "#{'-' * width} ----------- --------------"
|
50
50
|
iva.each do |i|
|
51
|
-
|
52
|
-
else
|
53
|
-
puts "%*s %11.3f %14.3f" % [width, i.title, i.duration, i.rate ]
|
54
|
-
end
|
51
|
+
puts '%*s %11.3f %14.3f' % [width, i.title, i.duration, i.rate ] unless i.duration.nil?
|
55
52
|
end
|
56
53
|
puts
|
57
54
|
puts "Comparison Matrix\n(performance factor, 2.0 means row is twice as fast as column)"
|
58
|
-
puts ([' ' * width] + iva.map { |i|
|
59
|
-
puts (['-' * width] + iva.map { |
|
55
|
+
puts ([' ' * width] + iva.map { |i| '%*s' % [width, i.title] }).join(' ')
|
56
|
+
puts (['-' * width] + iva.map { |_i| '-' * width }).join(' ')
|
60
57
|
iva.each do |i|
|
61
|
-
line = [
|
58
|
+
line = ['%*s' % [width, i.title]]
|
62
59
|
iva.each do |o|
|
63
|
-
line <<
|
60
|
+
line << ('%*.2f' % [width, o.duration / i.duration])
|
64
61
|
end
|
65
62
|
puts line.join(' ')
|
66
63
|
end
|
@@ -90,17 +87,15 @@ class Perf
|
|
90
87
|
end
|
91
88
|
|
92
89
|
def run(iter, base)
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
@error = "#{e.class}: #{e.message}"
|
103
|
-
end
|
90
|
+
GC.start
|
91
|
+
@before.call unless @before.nil?
|
92
|
+
start = Time.now
|
93
|
+
iter.times { @blk.call }
|
94
|
+
@duration = Time.now - start - base
|
95
|
+
@duration = 0.0 if @duration < 0.0
|
96
|
+
@rate = iter / @duration
|
97
|
+
rescue Exception => e
|
98
|
+
@error = "#{e.class}: #{e.message}"
|
104
99
|
end
|
105
100
|
|
106
101
|
end # Item
|
data/test/perf_compat.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
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'
|
@@ -11,25 +11,23 @@ require 'oj'
|
|
11
11
|
|
12
12
|
$verbose = false
|
13
13
|
$indent = 0
|
14
|
-
$iter =
|
14
|
+
$iter = 20_000
|
15
15
|
$size = 0
|
16
16
|
|
17
17
|
opts = OptionParser.new
|
18
|
-
opts.on(
|
19
|
-
opts.on(
|
20
|
-
opts.on(
|
21
|
-
opts.on(
|
22
|
-
opts.on(
|
23
|
-
|
18
|
+
opts.on('-v', 'verbose') { $verbose = true }
|
19
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| $iter = i }
|
20
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| $indent = i }
|
21
|
+
opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| $size = i }
|
22
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
23
|
+
opts.parse(ARGV)
|
24
24
|
|
25
25
|
def capture_error(tag, orig, load_key, dump_key, &blk)
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
$failed[tag] = "#{e.class}: #{e.message}"
|
32
|
-
end
|
26
|
+
obj = blk.call(orig)
|
27
|
+
puts obj unless orig == obj
|
28
|
+
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
29
|
+
rescue Exception => e
|
30
|
+
$failed[tag] = "#{e.class}: #{e.message}"
|
33
31
|
end
|
34
32
|
|
35
33
|
# Verify that all packages dump and load correctly and return the same Object as the original.
|
@@ -39,7 +37,7 @@ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
|
|
39
37
|
require 'json/ext'
|
40
38
|
JSON.generator = JSON::Ext::Generator
|
41
39
|
JSON.parser = JSON::Ext::Parser
|
42
|
-
JSON.
|
40
|
+
JSON.parse(JSON.generate(o))
|
43
41
|
}
|
44
42
|
|
45
43
|
module One
|
@@ -47,7 +45,7 @@ module One
|
|
47
45
|
module Three
|
48
46
|
class Empty
|
49
47
|
|
50
|
-
def initialize
|
48
|
+
def initialize
|
51
49
|
@a = 1
|
52
50
|
@b = 2
|
53
51
|
@c = 3
|
@@ -58,16 +56,16 @@ module One
|
|
58
56
|
end
|
59
57
|
alias == eql?
|
60
58
|
|
61
|
-
def as_json(*
|
59
|
+
def as_json(*_a)
|
62
60
|
{JSON.create_id => self.class.name, 'a' => @a, 'b' => @b, 'c' => @c }
|
63
61
|
end
|
64
|
-
|
65
|
-
def to_json(*
|
62
|
+
|
63
|
+
def to_json(*_a)
|
66
64
|
JSON.generate(as_json())
|
67
65
|
end
|
68
66
|
|
69
|
-
def self.json_create(
|
70
|
-
|
67
|
+
def self.json_create(_h)
|
68
|
+
new()
|
71
69
|
end
|
72
70
|
end # Empty
|
73
71
|
end # Three
|
@@ -77,8 +75,8 @@ end # One
|
|
77
75
|
$obj = {
|
78
76
|
'a' => 'Alpha', # string
|
79
77
|
'b' => true, # boolean
|
80
|
-
'c' =>
|
81
|
-
'd' => [ true, [false, [-
|
78
|
+
'c' => 12_345, # number
|
79
|
+
'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
82
80
|
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
83
81
|
'f' => nil, # nil
|
84
82
|
'g' => One::Two::Three::Empty.new(),
|
@@ -109,13 +107,13 @@ if $verbose
|
|
109
107
|
end
|
110
108
|
|
111
109
|
puts '-' * 80
|
112
|
-
puts
|
110
|
+
puts 'Compat Parse Performance'
|
113
111
|
perf = Perf.new()
|
114
|
-
unless $failed.
|
115
|
-
perf.add('JSON::Ext', 'parse') { JSON.
|
112
|
+
unless $failed.key?('JSON::Ext')
|
113
|
+
perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
|
116
114
|
perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
|
117
115
|
end
|
118
|
-
unless $failed.
|
116
|
+
unless $failed.key?('Oj:compat')
|
119
117
|
perf.add('Oj:compat', 'compat_load') { Oj.compat_load($json) }
|
120
118
|
end
|
121
119
|
perf.run($iter)
|
@@ -125,6 +123,6 @@ puts '-' * 80
|
|
125
123
|
puts
|
126
124
|
|
127
125
|
unless $failed.empty?
|
128
|
-
puts
|
129
|
-
$failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
|
126
|
+
puts 'The following packages were not included for the reason listed'
|
127
|
+
$failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
|
130
128
|
end
|
data/test/perf_dump.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
$LOAD_PATH << '.'
|
5
|
+
$LOAD_PATH << File.join(__dir__, '../lib')
|
6
|
+
$LOAD_PATH << File.join(__dir__, '../ext')
|
7
|
+
|
8
|
+
require 'optparse'
|
9
|
+
require 'oj'
|
10
|
+
|
11
|
+
@verbose = false
|
12
|
+
@indent = 2
|
13
|
+
@iter = 100_000
|
14
|
+
@size = 2
|
15
|
+
|
16
|
+
opts = OptionParser.new
|
17
|
+
opts.on('-v', 'verbose') { @verbose = true }
|
18
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
|
19
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
|
20
|
+
opts.on('-s', '--size [Int]', Integer, 'size (~Kbytes)') { |i| @size = i }
|
21
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
22
|
+
opts.parse(ARGV)
|
23
|
+
|
24
|
+
@obj = {
|
25
|
+
'a' => 'Alpha', # string
|
26
|
+
'b' => true, # boolean
|
27
|
+
'c' => 12345, # number
|
28
|
+
'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
29
|
+
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
30
|
+
'f' => nil, # nil
|
31
|
+
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
32
|
+
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
33
|
+
}
|
34
|
+
|
35
|
+
Oj.default_options = { :indent => @indent, :mode => :strict }
|
36
|
+
|
37
|
+
if 0 < @size
|
38
|
+
o = @obj
|
39
|
+
@obj = []
|
40
|
+
(4 * @size).times do
|
41
|
+
@obj << o
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
@json = Oj.dump(@obj)
|
46
|
+
GC.start
|
47
|
+
start = Time.now
|
48
|
+
@iter.times { Oj.dump(@obj) }
|
49
|
+
duration = Time.now - start
|
50
|
+
puts "Dumped #{@json.length} byte JSON #{@iter} times in %0.3f seconds or %0.3f iteration/sec." % [duration, @iter / duration]
|
data/test/perf_fast.rb
CHANGED
@@ -1,35 +1,35 @@
|
|
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
|
-
|
20
|
-
|
21
|
-
|
15
|
+
@verbose = false
|
16
|
+
@indent = 0
|
17
|
+
@iter = 100_000
|
18
|
+
@gets = 0
|
19
|
+
@fetch = false
|
20
|
+
@write = false
|
21
|
+
@read = false
|
22
22
|
|
23
23
|
opts = OptionParser.new
|
24
|
-
opts.on(
|
25
|
-
opts.on(
|
26
|
-
opts.on(
|
27
|
-
opts.on(
|
28
|
-
opts.on(
|
29
|
-
opts.on(
|
30
|
-
opts.on(
|
31
|
-
opts.on(
|
32
|
-
|
24
|
+
opts.on('-v', 'verbose') { @verbose = true }
|
25
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |i| @iter = i }
|
26
|
+
opts.on('-i', '--indent [Int]', Integer, 'indentation') { |i| @indent = i }
|
27
|
+
opts.on('-g', '--gets [Int]', Integer, 'number of gets') { |i| @gets = i }
|
28
|
+
opts.on('-f', 'fetch') { @fetch = true }
|
29
|
+
opts.on('-w', 'write') { @write = true }
|
30
|
+
opts.on('-r', 'read') { @read = true }
|
31
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
32
|
+
opts.parse(ARGV)
|
33
33
|
|
34
34
|
# This just navigates to each leaf of the JSON structure.
|
35
35
|
def dig(obj, &blk)
|
@@ -37,83 +37,81 @@ def dig(obj, &blk)
|
|
37
37
|
when Array
|
38
38
|
obj.each { |e| dig(e, &blk) }
|
39
39
|
when Hash
|
40
|
-
obj.
|
40
|
+
obj.each_value { |e| dig(e, &blk) }
|
41
41
|
else
|
42
42
|
blk.yield(obj)
|
43
43
|
end
|
44
44
|
end
|
45
45
|
|
46
|
-
|
46
|
+
@obj = {
|
47
47
|
'a' => 'Alpha', # string
|
48
48
|
'b' => true, # boolean
|
49
|
-
'c' =>
|
50
|
-
'd' => [ true, [false, {'12345' =>
|
49
|
+
'c' => 12_345, # number
|
50
|
+
'd' => [ true, [false, {'12345' => 12_345, 'nil' => nil}, 3.967, { 'x' => 'something', 'y' => false, 'z' => true}, nil]], # mix it up array
|
51
51
|
'e' => { 'one' => 1, 'two' => 2 }, # hash
|
52
52
|
'f' => nil, # nil
|
53
|
-
'g' =>
|
53
|
+
'g' => 12_345_678_901_234_567_890_123_456_789, # big number
|
54
54
|
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
55
55
|
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
56
56
|
}
|
57
57
|
|
58
|
-
Oj.default_options = { :indent =>
|
58
|
+
Oj.default_options = { :indent => @indent, :mode => :compat }
|
59
59
|
|
60
|
-
|
61
|
-
|
60
|
+
@json = Oj.dump(@obj)
|
61
|
+
@failed = {} # key is same as String used in tests later
|
62
62
|
|
63
63
|
def capture_error(tag, orig, load_key, dump_key, &blk)
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
$failed[tag] = "#{e.class}: #{e.message}"
|
69
|
-
end
|
64
|
+
obj = blk.call(orig)
|
65
|
+
raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
|
66
|
+
rescue Exception => e
|
67
|
+
@failed[tag] = "#{e.class}: #{e.message}"
|
70
68
|
end
|
71
69
|
|
72
70
|
# Verify that all packages dump and load correctly and return the same Object as the original.
|
73
|
-
capture_error('Oj::Doc',
|
74
|
-
#capture_error('Yajl',
|
75
|
-
capture_error('JSON::Ext',
|
71
|
+
capture_error('Oj::Doc', @obj, 'load', 'dump') { |o| Oj::Doc.open(Oj.dump(o, :mode => :strict)) { |f| f.fetch() } }
|
72
|
+
# capture_error('Yajl', @obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
|
73
|
+
capture_error('JSON::Ext', @obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
|
76
74
|
|
77
|
-
if
|
78
|
-
puts "json:\n#{
|
75
|
+
if @verbose
|
76
|
+
puts "json:\n#{@json}\n"
|
79
77
|
end
|
80
78
|
|
81
79
|
puts '-' * 80
|
82
|
-
puts
|
80
|
+
puts 'Parse Performance'
|
83
81
|
perf = Perf.new()
|
84
|
-
perf.add('Oj::Doc', 'parse') { Oj::Doc.open(
|
85
|
-
#perf.add('Yajl', 'parse') { Yajl::Parser.parse(
|
86
|
-
perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new(
|
87
|
-
perf.run(
|
82
|
+
perf.add('Oj::Doc', 'parse') { Oj::Doc.open(@json) { |f| } } unless @failed.key?('Oj::Doc')
|
83
|
+
# perf.add('Yajl', 'parse') { Yajl::Parser.parse(@json) } unless @failed.has_key?('Yajl')
|
84
|
+
perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new(@json).parse } unless @failed.key?('JSON::Ext')
|
85
|
+
perf.run(@iter)
|
88
86
|
|
89
87
|
puts '-' * 80
|
90
|
-
puts
|
91
|
-
Oj::Doc.open(
|
88
|
+
puts 'JSON generation Performance'
|
89
|
+
Oj::Doc.open(@json) do |doc|
|
92
90
|
perf = Perf.new()
|
93
91
|
perf.add('Oj::Doc', 'dump') { doc.dump() }
|
94
|
-
#
|
95
|
-
perf.add('JSON::Ext', 'fast_generate') { JSON.fast_generate(
|
92
|
+
# perf.add('Yajl', 'encode') { Yajl::Encoder.encode(@obj) }
|
93
|
+
perf.add('JSON::Ext', 'fast_generate') { JSON.fast_generate(@obj) }
|
96
94
|
perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
|
97
|
-
perf.run(
|
95
|
+
perf.run(@iter)
|
98
96
|
end
|
99
97
|
|
100
|
-
if 0 <
|
98
|
+
if 0 < @gets
|
101
99
|
puts '-' * 80
|
102
|
-
puts
|
100
|
+
puts 'Parse and get all values Performance'
|
103
101
|
perf = Perf.new()
|
104
|
-
perf.add('Oj::Doc', 'parse') { Oj::Doc.open(
|
105
|
-
#
|
106
|
-
perf.add('JSON::Ext', 'parse') {
|
107
|
-
perf.run(
|
102
|
+
perf.add('Oj::Doc', 'parse') { Oj::Doc.open(@json) { |f| @gets.times { f.each_value() {} } } } unless @failed.key?('Oj::Doc')
|
103
|
+
# perf.add('Yajl', 'parse') { @gets.times { dig(Yajl::Parser.parse(@json)) {} } } unless @failed.has_key?('Yajl')
|
104
|
+
perf.add('JSON::Ext', 'parse') { @gets.times { dig(JSON::Ext::Parser.new(@json).parse) {} } } unless @failed.key?('JSON::Ext')
|
105
|
+
perf.run(@iter)
|
108
106
|
end
|
109
107
|
|
110
|
-
if
|
108
|
+
if @fetch
|
111
109
|
puts '-' * 80
|
112
|
-
puts
|
113
|
-
json_hash = Oj.load(
|
114
|
-
Oj::Doc.open(
|
115
|
-
#puts "*** C fetch: #{fast.fetch('/d/2/4/y')}"
|
116
|
-
#puts "*** Ruby fetch: #{json_hash.fetch('d', []).fetch(1, []).fetch(3, []).fetch('x', nil)}"
|
110
|
+
puts 'fetch nested Performance'
|
111
|
+
json_hash = Oj.load(@json, :mode => :strict)
|
112
|
+
Oj::Doc.open(@json) do |fast|
|
113
|
+
# puts "*** C fetch: #{fast.fetch('/d/2/4/y')}"
|
114
|
+
# puts "*** Ruby fetch: #{json_hash.fetch('d', []).fetch(1, []).fetch(3, []).fetch('x', nil)}"
|
117
115
|
perf = Perf.new()
|
118
116
|
perf.add('Oj::Doc', 'fetch') { fast.fetch('/d/2/4/x'); fast.fetch('/h/a/b/c/d/e/f/g'); fast.fetch('/i/1/1/1/1/1/1/1') }
|
119
117
|
# version that fails gracefully
|
@@ -123,42 +121,42 @@ if $fetch
|
|
123
121
|
json_hash.fetch('i', []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, []).fetch(0, nil)
|
124
122
|
end
|
125
123
|
# version that raises if the path is incorrect
|
126
|
-
#
|
127
|
-
perf.run(
|
124
|
+
# perf.add('Ruby', 'fetch') { @fetch.times { json_hash['d'][1][3][1] } }
|
125
|
+
perf.run(@iter)
|
128
126
|
end
|
129
127
|
end
|
130
128
|
|
131
|
-
if
|
129
|
+
if @write
|
132
130
|
puts '-' * 80
|
133
|
-
puts
|
134
|
-
Oj::Doc.open(
|
131
|
+
puts 'JSON write to file Performance'
|
132
|
+
Oj::Doc.open(@json) do |doc|
|
135
133
|
perf = Perf.new()
|
136
134
|
perf.add('Oj::Doc', 'dump') { doc.dump(nil, 'oj.json') }
|
137
|
-
#
|
138
|
-
perf.add('JSON::Ext', 'fast_generate') { File.
|
135
|
+
# perf.add('Yajl', 'encode') { File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode(@obj, f) } }
|
136
|
+
perf.add('JSON::Ext', 'fast_generate') { File.write('json_ext.json', JSON.fast_generate(@obj)) }
|
139
137
|
perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
|
140
|
-
perf.run(
|
138
|
+
perf.run(@iter)
|
141
139
|
end
|
142
140
|
end
|
143
141
|
|
144
|
-
if
|
142
|
+
if @read
|
145
143
|
puts '-' * 80
|
146
|
-
puts
|
147
|
-
Oj::Doc.open(
|
148
|
-
#
|
144
|
+
puts 'JSON read from file Performance'
|
145
|
+
Oj::Doc.open(@json) { |doc| doc.dump(nil, 'oj.json') }
|
146
|
+
# File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode(@obj, f) }
|
149
147
|
JSON.generator = JSON::Ext::Generator
|
150
|
-
File.
|
151
|
-
Oj::Doc.open(
|
148
|
+
File.write('json_ext.json', JSON.fast_generate(@obj))
|
149
|
+
Oj::Doc.open(@json) do |_doc|
|
152
150
|
perf = Perf.new()
|
153
|
-
perf.add('Oj::Doc', 'open_file') {
|
154
|
-
#
|
151
|
+
perf.add('Oj::Doc', 'open_file') { Oj::Doc.open_file('oj.json') }
|
152
|
+
# perf.add('Yajl', 'decode') { Yajl::decoder.decode(File.read('yajl.json')) }
|
155
153
|
perf.add('JSON::Ext', '') { JSON.parse(File.read('json_ext.json')) }
|
156
154
|
perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
|
157
|
-
perf.run(
|
155
|
+
perf.run(@iter)
|
158
156
|
end
|
159
157
|
end
|
160
158
|
|
161
|
-
unless
|
162
|
-
puts
|
163
|
-
|
159
|
+
unless @failed.empty?
|
160
|
+
puts 'The following packages were not included for the reason listed'
|
161
|
+
@failed.each { |tag, msg| puts "***** #{tag}: #{msg}" }
|
164
162
|
end
|
data/test/perf_file.rb
CHANGED
@@ -1,58 +1,57 @@
|
|
1
1
|
#!/usr/bin/env ruby -wW1
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
|
4
|
-
|
5
|
-
|
4
|
+
$LOAD_PATH << '.'
|
5
|
+
$LOAD_PATH << '../lib'
|
6
|
+
$LOAD_PATH << '../ext'
|
6
7
|
|
7
|
-
if __FILE__ == $
|
8
|
-
|
9
|
-
|
10
|
-
$: << path
|
11
|
-
end
|
8
|
+
if __FILE__ == $PROGRAM_NAME && (i = ARGV.index('-I'))
|
9
|
+
_, path = ARGV.slice!(i, 2)
|
10
|
+
$LOAD_PATH << path
|
12
11
|
end
|
13
12
|
|
14
13
|
require 'optparse'
|
15
14
|
require 'oj'
|
16
15
|
require 'perf'
|
17
16
|
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
@indent = 0
|
18
|
+
@iter = 1
|
19
|
+
@size = 1
|
21
20
|
|
22
21
|
opts = OptionParser.new
|
23
22
|
|
24
|
-
opts.on(
|
25
|
-
opts.on(
|
26
|
-
opts.on(
|
27
|
-
opts.on(
|
23
|
+
opts.on('-r', 'read') { true }
|
24
|
+
opts.on('-c', '--count [Int]', Integer, 'iterations') { |v| @iter = v }
|
25
|
+
opts.on('-i', '--indent [Int]', Integer, 'indent') { |v| @indent = v }
|
26
|
+
opts.on('-s', '--size [Int]', Integer, 'size in Mbytes') { |s| @size = s }
|
28
27
|
|
29
|
-
opts.on(
|
30
|
-
|
28
|
+
opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
|
29
|
+
opts.parse(ARGV)
|
31
30
|
|
32
|
-
|
31
|
+
@obj = {
|
33
32
|
'a' => 'Alpha', # string
|
34
33
|
'b' => true, # boolean
|
35
|
-
'c' =>
|
36
|
-
'd' => [ true, [false, [-
|
34
|
+
'c' => 12_345, # number
|
35
|
+
'd' => [ true, [false, [-123_456_789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
|
37
36
|
'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
|
38
37
|
'f' => nil, # nil
|
39
|
-
'g' =>
|
38
|
+
'g' => 12_345_678_901_234_567_890_123_456_789, # _bignum
|
40
39
|
'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
|
41
40
|
'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
|
42
41
|
}
|
43
42
|
|
44
|
-
json = Oj.dump(
|
45
|
-
cnt = (
|
46
|
-
cnt = 1 if 0 ==
|
43
|
+
json = Oj.dump(@obj, :indent => @indent)
|
44
|
+
cnt = ((@size * 1024 * 1024) + json.size) / json.size
|
45
|
+
cnt = 1 if 0 == @size
|
47
46
|
|
48
47
|
filename = 'tmp.json'
|
49
|
-
File.open(filename,
|
48
|
+
File.open(filename, 'w') { |f|
|
50
49
|
cnt.times do
|
51
|
-
Oj.to_stream(f,
|
50
|
+
Oj.to_stream(f, @obj, :indent => @indent)
|
52
51
|
end
|
53
52
|
}
|
54
53
|
|
55
|
-
Oj.default_options = { :mode => :strict, :indent =>
|
54
|
+
Oj.default_options = { :mode => :strict, :indent => @indent }
|
56
55
|
|
57
56
|
puts '-' * 80
|
58
57
|
puts "Read from #{cnt * json.size / (1024 * 1024)} Mb file Performance"
|
@@ -60,5 +59,4 @@ perf = Perf.new()
|
|
60
59
|
perf.add('Oj.load_file', '') { Oj.load_file(filename) }
|
61
60
|
perf.add('Oj.load(string)', '') { Oj.load(File.read(filename)) }
|
62
61
|
perf.add('Oj.load(file)', '') { File.open(filename, 'r') { |f| Oj.load(f) } }
|
63
|
-
perf.run(
|
64
|
-
|
62
|
+
perf.run(@iter)
|