oj 3.11.5 → 3.16.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1421 -0
  3. data/README.md +19 -5
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +20 -6
  6. data/ext/oj/cache.c +329 -0
  7. data/ext/oj/cache.h +22 -0
  8. data/ext/oj/cache8.c +10 -9
  9. data/ext/oj/circarray.c +8 -6
  10. data/ext/oj/circarray.h +2 -2
  11. data/ext/oj/code.c +19 -33
  12. data/ext/oj/code.h +2 -2
  13. data/ext/oj/compat.c +27 -77
  14. data/ext/oj/custom.c +86 -179
  15. data/ext/oj/debug.c +126 -0
  16. data/ext/oj/dump.c +256 -249
  17. data/ext/oj/dump.h +26 -12
  18. data/ext/oj/dump_compat.c +565 -642
  19. data/ext/oj/dump_leaf.c +17 -63
  20. data/ext/oj/dump_object.c +65 -187
  21. data/ext/oj/dump_strict.c +27 -51
  22. data/ext/oj/encoder.c +43 -0
  23. data/ext/oj/err.c +2 -13
  24. data/ext/oj/err.h +24 -8
  25. data/ext/oj/extconf.rb +21 -6
  26. data/ext/oj/fast.c +149 -149
  27. data/ext/oj/intern.c +313 -0
  28. data/ext/oj/intern.h +22 -0
  29. data/ext/oj/mem.c +318 -0
  30. data/ext/oj/mem.h +53 -0
  31. data/ext/oj/mimic_json.c +121 -106
  32. data/ext/oj/object.c +85 -162
  33. data/ext/oj/odd.c +89 -67
  34. data/ext/oj/odd.h +15 -15
  35. data/ext/oj/oj.c +542 -411
  36. data/ext/oj/oj.h +99 -73
  37. data/ext/oj/parse.c +175 -187
  38. data/ext/oj/parse.h +26 -24
  39. data/ext/oj/parser.c +1600 -0
  40. data/ext/oj/parser.h +101 -0
  41. data/ext/oj/rails.c +112 -159
  42. data/ext/oj/rails.h +1 -1
  43. data/ext/oj/reader.c +11 -14
  44. data/ext/oj/reader.h +4 -2
  45. data/ext/oj/resolve.c +5 -24
  46. data/ext/oj/rxclass.c +7 -6
  47. data/ext/oj/rxclass.h +1 -1
  48. data/ext/oj/saj.c +22 -33
  49. data/ext/oj/saj2.c +584 -0
  50. data/ext/oj/saj2.h +23 -0
  51. data/ext/oj/scp.c +5 -28
  52. data/ext/oj/sparse.c +28 -72
  53. data/ext/oj/stream_writer.c +50 -40
  54. data/ext/oj/strict.c +56 -61
  55. data/ext/oj/string_writer.c +72 -39
  56. data/ext/oj/trace.h +31 -4
  57. data/ext/oj/usual.c +1218 -0
  58. data/ext/oj/usual.h +69 -0
  59. data/ext/oj/util.h +1 -1
  60. data/ext/oj/val_stack.c +14 -3
  61. data/ext/oj/val_stack.h +8 -7
  62. data/ext/oj/validate.c +46 -0
  63. data/ext/oj/wab.c +63 -88
  64. data/lib/oj/active_support_helper.rb +1 -3
  65. data/lib/oj/bag.rb +7 -1
  66. data/lib/oj/easy_hash.rb +4 -5
  67. data/lib/oj/error.rb +1 -2
  68. data/lib/oj/json.rb +162 -150
  69. data/lib/oj/mimic.rb +9 -7
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/schandler.rb +5 -4
  72. data/lib/oj/state.rb +12 -8
  73. data/lib/oj/version.rb +1 -2
  74. data/lib/oj.rb +2 -0
  75. data/pages/Compatibility.md +1 -1
  76. data/pages/InstallOptions.md +20 -0
  77. data/pages/JsonGem.md +15 -0
  78. data/pages/Modes.md +8 -3
  79. data/pages/Options.md +43 -5
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +14 -2
  82. data/test/_test_active.rb +8 -9
  83. data/test/_test_active_mimic.rb +7 -8
  84. data/test/_test_mimic_rails.rb +17 -20
  85. data/test/activerecord/result_test.rb +5 -6
  86. data/test/activesupport6/encoding_test.rb +63 -28
  87. data/test/{activesupport5 → activesupport7}/abstract_unit.rb +16 -12
  88. data/test/{activesupport5 → activesupport7}/decoding_test.rb +2 -10
  89. data/test/{activesupport5 → activesupport7}/encoding_test.rb +86 -50
  90. data/test/{activesupport5 → activesupport7}/encoding_test_cases.rb +6 -0
  91. data/test/{activesupport5 → activesupport7}/time_zone_test_helpers.rb +8 -0
  92. data/test/files.rb +15 -15
  93. data/test/foo.rb +16 -45
  94. data/test/helper.rb +11 -8
  95. data/test/isolated/shared.rb +3 -2
  96. data/test/json_gem/json_addition_test.rb +2 -2
  97. data/test/json_gem/json_common_interface_test.rb +8 -6
  98. data/test/json_gem/json_encoding_test.rb +0 -0
  99. data/test/json_gem/json_ext_parser_test.rb +1 -0
  100. data/test/json_gem/json_fixtures_test.rb +3 -2
  101. data/test/json_gem/json_generator_test.rb +56 -38
  102. data/test/json_gem/json_generic_object_test.rb +11 -11
  103. data/test/json_gem/json_parser_test.rb +54 -47
  104. data/test/json_gem/json_string_matching_test.rb +9 -9
  105. data/test/json_gem/test_helper.rb +7 -3
  106. data/test/mem.rb +34 -0
  107. data/test/perf.rb +22 -27
  108. data/test/perf_compat.rb +31 -33
  109. data/test/perf_dump.rb +50 -0
  110. data/test/perf_fast.rb +80 -82
  111. data/test/perf_file.rb +27 -29
  112. data/test/perf_object.rb +65 -69
  113. data/test/perf_once.rb +59 -0
  114. data/test/perf_parser.rb +183 -0
  115. data/test/perf_saj.rb +46 -54
  116. data/test/perf_scp.rb +58 -69
  117. data/test/perf_simple.rb +41 -39
  118. data/test/perf_strict.rb +74 -82
  119. data/test/perf_wab.rb +67 -69
  120. data/test/prec.rb +5 -5
  121. data/test/sample/change.rb +0 -1
  122. data/test/sample/dir.rb +0 -1
  123. data/test/sample/doc.rb +0 -1
  124. data/test/sample/file.rb +0 -1
  125. data/test/sample/group.rb +0 -1
  126. data/test/sample/hasprops.rb +0 -1
  127. data/test/sample/layer.rb +0 -1
  128. data/test/sample/rect.rb +0 -1
  129. data/test/sample/shape.rb +0 -1
  130. data/test/sample/text.rb +0 -1
  131. data/test/sample.rb +16 -16
  132. data/test/sample_json.rb +8 -8
  133. data/test/test_compat.rb +95 -43
  134. data/test/test_custom.rb +73 -51
  135. data/test/test_debian.rb +7 -10
  136. data/test/test_fast.rb +135 -79
  137. data/test/test_file.rb +41 -30
  138. data/test/test_gc.rb +16 -5
  139. data/test/test_generate.rb +5 -5
  140. data/test/test_hash.rb +5 -5
  141. data/test/test_integer_range.rb +9 -9
  142. data/test/test_null.rb +20 -20
  143. data/test/test_object.rb +99 -96
  144. data/test/test_parser.rb +11 -0
  145. data/test/test_parser_debug.rb +27 -0
  146. data/test/test_parser_saj.rb +337 -0
  147. data/test/test_parser_usual.rb +251 -0
  148. data/test/test_rails.rb +2 -2
  149. data/test/test_saj.rb +10 -8
  150. data/test/test_scp.rb +37 -39
  151. data/test/test_strict.rb +40 -32
  152. data/test/test_various.rb +165 -84
  153. data/test/test_wab.rb +48 -44
  154. data/test/test_writer.rb +47 -47
  155. data/test/tests.rb +13 -5
  156. data/test/tests_mimic.rb +12 -3
  157. data/test/tests_mimic_addition.rb +12 -3
  158. metadata +74 -128
  159. data/ext/oj/hash.c +0 -131
  160. data/ext/oj/hash.h +0 -19
  161. data/ext/oj/hash_test.c +0 -491
  162. data/test/activesupport4/decoding_test.rb +0 -108
  163. data/test/activesupport4/encoding_test.rb +0 -531
  164. data/test/activesupport4/test_helper.rb +0 -41
  165. data/test/activesupport5/test_helper.rb +0 -72
  166. data/test/bar.rb +0 -35
  167. data/test/baz.rb +0 -16
  168. data/test/zoo.rb +0 -13
data/test/perf_scp.rb CHANGED
@@ -1,61 +1,58 @@
1
1
  #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
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
- $verbose = false
16
- $indent = 0
17
- $iter = 50000
18
- $with_bignum = false
19
- $size = 0
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("-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
- files = opts.parse(ARGV)
29
-
30
- $obj = {
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' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
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
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
40
+ @obj['g'] = 12_345_678_901_234_567_890_123_456_789 if @with_bignum
41
41
 
42
- if 0 < $size
43
- o = $obj
44
- $obj = []
45
- (4 * $size).times do
46
- $obj << o
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 => $indent, :mode => :compat }
50
+ Oj.default_options = { :indent => @indent, :mode => :strict, cache_keys: true, cache_str: 5 }
51
51
 
52
- $json = Oj.dump($obj)
53
- $failed = {} # key is same as String used in tests later
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 initialize()
79
+ def hash_start
80
+ nil
87
81
  end
88
82
 
89
- def hash_start()
90
- return nil
83
+ def hash_end
91
84
  end
92
85
 
93
- def hash_end()
86
+ def array_start
87
+ nil
94
88
  end
95
89
 
96
- def array_start()
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
- begin
122
- obj = blk.call(orig)
123
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
124
- rescue Exception => e
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', $obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
131
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
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 $verbose
134
- puts "json:\n#{$json}\n"
121
+ if @verbose
122
+ puts "json:\n#{@json}\n"
135
123
  end
136
124
 
137
125
  puts '-' * 80
138
- puts "Parse Performance"
126
+ puts 'Parse Performance'
139
127
  perf = Perf.new()
140
- perf.add('Oj::Saj', 'all') { Oj.saj_parse(saj_handler, $json) }
141
- perf.add('Oj::Saj', 'none') { Oj.saj_parse(no_saj, $json) }
142
- perf.add('Oj::Scp', 'all') { Oj.sc_parse(sc_handler, $json) }
143
- perf.add('Oj::Scp', 'none') { Oj.sc_parse(no_handler, $json) }
144
- perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
145
- perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
146
- perf.run($iter)
147
-
148
- unless $failed.empty?
149
- puts "The following packages were not included for the reason listed"
150
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
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
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
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 = "A string"
20
+ @string = 'A string'
21
21
  @array = [true, false, nil]
22
22
  @hash = { 'one' => 1, 'two' => 2 }
23
23
  end
24
- def to_json()
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
- def to_hash()
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 = 10000
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("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
55
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
56
- opts.on("-o", "without objects") { $with_object = false }
57
- opts.on("-b", "without bignum") { $with_bignum = false }
58
- opts.on("-n", "without numbers") { $with_nums = false }
59
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
60
- files = opts.parse(ARGV)
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' => 12345,
67
- 'd' => [ true, [false, [12345, nil], 3.967, ['something', false], nil]],
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'] = 12345678901234567890123456789 if $with_bignum
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 "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d JSON::Ext::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d JSON::Pure::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d MessagePack.unpack()s in %0.3f seconds or %0.1f packs/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]
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 "Parser results:"
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,dt|
181
- if 0.0 == dt
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 "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
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 "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d JSON::Ext generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d JSON::Pure generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d Msgpack()s in %0.3f seconds or %0.1f unpacks/msec" % [$iter, dt, $iter/dt/1000.0]
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 "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]
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 "Parser results:"
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,dt|
280
- if 0.0 == dt
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 "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
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
- # encoding: UTF-8
2
+ # frozen_string_literal: true
3
3
 
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
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
- $verbose = false
13
- $indent = 0
14
- $iter = 20000
15
- $with_bignum = false
16
- $with_nums = true
17
- $size = 0
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("-v", "verbose") { $verbose = true }
21
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
22
- opts.on("-i", "--indent [Int]", Integer, "indentation") { |i| $indent = i }
23
- opts.on("-s", "--size [Int]", Integer, "size (~Kbytes)") { |i| $size = i }
24
- opts.on("-b", "with bignum") { $with_bignum = true }
25
- opts.on("-n", "without numbers") { $with_nums = false }
26
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
27
- files = opts.parse(ARGV)
28
-
29
- if $with_nums
30
- $obj = {
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' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
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
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
44
+ @obj['g'] = 12_345_678_901_234_567_890_123_456_789 if @with_bignum
41
45
  else
42
- $obj = {
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 => $indent, :mode => :strict }
58
+ Oj.default_options = { :indent => @indent, :mode => :strict, cache_keys: @cache_keys, cache_str: 5 }
55
59
 
56
- if 0 < $size
57
- o = $obj
58
- $obj = []
59
- (4 * $size).times do
60
- $obj << o
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
- $json = Oj.dump($obj)
65
- $obj_json = Oj.dump($obj, :mode => :object)
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
- begin
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}"
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', $obj, 'load', 'dump') { |o| Oj.strict_load(Oj.dump(o, :mode => :strict)) }
81
- capture_error('Yajl', $obj, 'encode', 'parse') { |o| require 'yajl'; Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
82
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
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
- if $verbose
97
- puts "json:\n#{$json}\n"
98
- puts "object json:\n#{$obj_json}\n"
99
- puts "Oj loaded object:\n#{Oj.strict_load($json)}\n"
100
- puts "Yajl loaded object:\n#{Yajl::Parser.parse($json)}\n"
101
- puts "JSON loaded object:\n#{JSON::Ext::Parser.new($json).parse}\n"
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 "Strict Parse Performance"
104
+ puts 'Strict Parse Performance'
106
105
  perf = Perf.new()
107
- unless $failed.has_key?('JSON::Ext')
108
- perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
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 $failed.has_key?('JSON::Pure')
112
- perf.add('JSON::Pure', 'parse') { JSON.parse($json) }
113
- perf.before('JSON::Pure') { JSON.parser = JSON::Pure::Parser }
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 $failed.has_key?('Oj:strict')
116
- perf.add('Oj:strict', 'strict_load') { Oj.strict_load($json) }
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 "Strict Dump Performance"
118
+ puts 'Strict Dump Performance'
123
119
  perf = Perf.new()
124
- unless $failed.has_key?('JSON::Ext')
125
- perf.add('JSON::Ext', 'dump') { JSON.generate($obj) }
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 $failed.has_key?('JSON::Pure')
129
- perf.add('JSON::Pure', 'generate') { JSON.generate($obj) }
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($obj) } unless $failed.has_key?('Yajl')
136
- perf.run($iter)
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 $failed.empty?
143
- puts "The following packages were not included for the reason listed"
144
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
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