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_compat.rb CHANGED
@@ -1,9 +1,9 @@
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'
@@ -11,25 +11,23 @@ require 'oj'
11
11
 
12
12
  $verbose = false
13
13
  $indent = 0
14
- $iter = 20000
14
+ $iter = 20_000
15
15
  $size = 0
16
16
 
17
17
  opts = OptionParser.new
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
- files = opts.parse(ARGV)
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
- begin
27
- obj = blk.call(orig)
28
- puts obj unless orig == obj
29
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
30
- rescue Exception => e
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.load(JSON.generate(o))
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(*a)
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(*a)
62
+
63
+ def to_json(*_a)
66
64
  JSON.generate(as_json())
67
65
  end
68
66
 
69
- def self.json_create(h)
70
- self.new()
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' => 12345, # number
81
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
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 "Compat Parse Performance"
110
+ puts 'Compat Parse Performance'
113
111
  perf = Perf.new()
114
- unless $failed.has_key?('JSON::Ext')
115
- perf.add('JSON::Ext', 'parse') { JSON.load($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.has_key?('Oj:compat')
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 "The following packages were not included for the reason listed"
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' => 12_345, # number
28
+ 'd' => [ true, [false, [-123_456_789, 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
- # 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 = 100000
18
- $gets = 0
19
- $fetch = false
20
- $write = false
21
- $read = false
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("-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
- files = opts.parse(ARGV)
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.values.each { |e| dig(e, &blk) }
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
- $obj = {
46
+ @obj = {
47
47
  'a' => 'Alpha', # string
48
48
  'b' => true, # boolean
49
- 'c' => 12345, # number
50
- 'd' => [ true, [false, {'12345' => 12345, 'nil' => nil}, 3.967, { 'x' => 'something', 'y' => false, 'z' => true}, nil]], # mix it up array
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' => 12345678901234567890123456789, # big number
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 => $indent, :mode => :compat }
58
+ Oj.default_options = { :indent => @indent, :mode => :compat }
59
59
 
60
- $json = Oj.dump($obj)
61
- $failed = {} # key is same as String used in tests later
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
- begin
65
- obj = blk.call(orig)
66
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
67
- rescue Exception => e
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', $obj, 'load', 'dump') { |o| Oj::Doc.open(Oj.dump(o, :mode => :strict)) { |f| f.fetch() } }
74
- #capture_error('Yajl', $obj, 'encode', 'parse') { |o| Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
75
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o| JSON.generator = JSON::Ext::Generator; JSON::Ext::Parser.new(JSON.generate(o)).parse }
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 $verbose
78
- puts "json:\n#{$json}\n"
75
+ if @verbose
76
+ puts "json:\n#{@json}\n"
79
77
  end
80
78
 
81
79
  puts '-' * 80
82
- puts "Parse Performance"
80
+ puts 'Parse Performance'
83
81
  perf = Perf.new()
84
- perf.add('Oj::Doc', 'parse') { Oj::Doc.open($json) {|f| } } unless $failed.has_key?('Oj::Doc')
85
- #perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
86
- perf.add('JSON::Ext', 'parse') { JSON::Ext::Parser.new($json).parse } unless $failed.has_key?('JSON::Ext')
87
- perf.run($iter)
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 "JSON generation Performance"
91
- Oj::Doc.open($json) do |doc|
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
- # perf.add('Yajl', 'encode') { Yajl::Encoder.encode($obj) }
95
- perf.add('JSON::Ext', 'fast_generate') { JSON.fast_generate($obj) }
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($iter)
95
+ perf.run(@iter)
98
96
  end
99
97
 
100
- if 0 < $gets
98
+ if 0 < @gets
101
99
  puts '-' * 80
102
- puts "Parse and get all values Performance"
100
+ puts 'Parse and get all values Performance'
103
101
  perf = Perf.new()
104
- perf.add('Oj::Doc', 'parse') { Oj::Doc.open($json) {|f| $gets.times { f.each_value() {} } } } unless $failed.has_key?('Oj::Doc')
105
- # perf.add('Yajl', 'parse') { $gets.times { dig(Yajl::Parser.parse($json)) {} } } unless $failed.has_key?('Yajl')
106
- perf.add('JSON::Ext', 'parse') { $gets.times { dig(JSON::Ext::Parser.new($json).parse) {} } } unless $failed.has_key?('JSON::Ext')
107
- perf.run($iter)
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 $fetch
108
+ if @fetch
111
109
  puts '-' * 80
112
- puts "fetch nested Performance"
113
- json_hash = Oj.load($json, :mode => :strict)
114
- Oj::Doc.open($json) do |fast|
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
- # perf.add('Ruby', 'fetch') { $fetch.times { json_hash['d'][1][3][1] } }
127
- perf.run($iter)
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 $write
129
+ if @write
132
130
  puts '-' * 80
133
- puts "JSON write to file Performance"
134
- Oj::Doc.open($json) do |doc|
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
- # perf.add('Yajl', 'encode') { File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode($obj, f) } }
138
- perf.add('JSON::Ext', 'fast_generate') { File.open('json_ext.json', 'w') { |f| f.write(JSON.fast_generate($obj)) } }
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($iter)
138
+ perf.run(@iter)
141
139
  end
142
140
  end
143
141
 
144
- if $read
142
+ if @read
145
143
  puts '-' * 80
146
- puts "JSON read from file Performance"
147
- Oj::Doc.open($json) { |doc| doc.dump(nil, 'oj.json') }
148
- # File.open('yajl.json', 'w') { |f| Yajl::Encoder.encode($obj, f) }
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.open('json_ext.json', 'w') { |f| f.write(JSON.fast_generate($obj)) }
151
- Oj::Doc.open($json) do |doc|
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') { ::Oj::Doc.open_file('oj.json') }
154
- # perf.add('Yajl', 'decode') { Yajl::decoder.decode(File.read('yajl.json')) }
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($iter)
155
+ perf.run(@iter)
158
156
  end
159
157
  end
160
158
 
161
- unless $failed.empty?
162
- puts "The following packages were not included for the reason listed"
163
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
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
- $: << '../lib'
5
- $: << '../ext'
4
+ $LOAD_PATH << '.'
5
+ $LOAD_PATH << '../lib'
6
+ $LOAD_PATH << '../ext'
6
7
 
7
- if __FILE__ == $0
8
- if (i = ARGV.index('-I'))
9
- x,path = ARGV.slice!(i, 2)
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
- $indent = 0
19
- $iter = 1
20
- $size = 1
17
+ @indent = 0
18
+ @iter = 1
19
+ @size = 1
21
20
 
22
21
  opts = OptionParser.new
23
22
 
24
- opts.on("-r", "read") { do_read = true }
25
- opts.on("-c", "--count [Int]", Integer, "iterations") { |i| $iter = i }
26
- opts.on("-i", "--indent [Int]", Integer, "indent") { |i| $indent = i }
27
- opts.on("-s", "--size [Int]", Integer, "size in Mbytes") { |s| $size = s }
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("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
30
- files = opts.parse(ARGV)
28
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
29
+ opts.parse(ARGV)
31
30
 
32
- $obj = {
31
+ @obj = {
33
32
  'a' => 'Alpha', # string
34
33
  'b' => true, # boolean
35
- 'c' => 12345, # number
36
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
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' => 12345678901234567890123456789, #_bignum
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($obj, :indent => $indent)
45
- cnt = ($size * 1024 * 1024 + json.size) / json.size
46
- cnt = 1 if 0 == $size
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, "w") { |f|
48
+ File.open(filename, 'w') { |f|
50
49
  cnt.times do
51
- Oj.to_stream(f, $obj, :indent => $indent)
50
+ Oj.to_stream(f, @obj, :indent => @indent)
52
51
  end
53
52
  }
54
53
 
55
- Oj.default_options = { :mode => :strict, :indent => $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($iter)
64
-
62
+ perf.run(@iter)