oj 3.14.3 → 3.15.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (66) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +4 -0
  3. data/ext/oj/custom.c +5 -15
  4. data/ext/oj/dump.c +27 -2
  5. data/ext/oj/mimic_json.c +21 -0
  6. data/ext/oj/object.c +7 -21
  7. data/ext/oj/oj.c +20 -0
  8. data/ext/oj/oj.h +3 -0
  9. data/ext/oj/strict.c +9 -27
  10. data/ext/oj/wab.c +9 -27
  11. data/lib/oj/version.rb +1 -1
  12. data/lib/oj.rb +3 -0
  13. data/pages/Options.md +4 -0
  14. data/test/_test_active.rb +8 -8
  15. data/test/_test_active_mimic.rb +7 -7
  16. data/test/_test_mimic_rails.rb +17 -19
  17. data/test/files.rb +14 -14
  18. data/test/foo.rb +5 -5
  19. data/test/helper.rb +4 -4
  20. data/test/mem.rb +8 -7
  21. data/test/perf.rb +21 -26
  22. data/test/perf_compat.rb +30 -32
  23. data/test/perf_dump.rb +25 -25
  24. data/test/perf_fast.rb +80 -82
  25. data/test/perf_file.rb +27 -29
  26. data/test/perf_object.rb +65 -68
  27. data/test/perf_once.rb +8 -7
  28. data/test/perf_parser.rb +40 -46
  29. data/test/perf_saj.rb +46 -53
  30. data/test/perf_scp.rb +57 -69
  31. data/test/perf_simple.rb +40 -38
  32. data/test/perf_strict.rb +68 -70
  33. data/test/perf_wab.rb +67 -69
  34. data/test/prec.rb +3 -3
  35. data/test/sample.rb +16 -15
  36. data/test/sample_json.rb +8 -7
  37. data/test/test_compat.rb +44 -46
  38. data/test/test_custom.rb +56 -42
  39. data/test/test_debian.rb +6 -9
  40. data/test/test_fast.rb +78 -72
  41. data/test/test_file.rb +16 -21
  42. data/test/test_gc.rb +5 -5
  43. data/test/test_generate.rb +5 -5
  44. data/test/test_hash.rb +4 -4
  45. data/test/test_integer_range.rb +9 -9
  46. data/test/test_null.rb +18 -20
  47. data/test/test_object.rb +76 -86
  48. data/test/test_parser.rb +4 -4
  49. data/test/test_parser_debug.rb +4 -4
  50. data/test/test_parser_saj.rb +31 -31
  51. data/test/test_parser_usual.rb +3 -3
  52. data/test/test_rails.rb +2 -2
  53. data/test/test_saj.rb +8 -8
  54. data/test/test_scp.rb +29 -29
  55. data/test/test_strict.rb +25 -31
  56. data/test/test_various.rb +121 -75
  57. data/test/test_wab.rb +43 -42
  58. data/test/test_writer.rb +46 -46
  59. data/test/tests.rb +7 -7
  60. data/test/tests_mimic.rb +6 -6
  61. data/test/tests_mimic_addition.rb +6 -6
  62. metadata +3 -6
  63. data/test/bar.rb +0 -11
  64. data/test/baz.rb +0 -16
  65. data/test/bug.rb +0 -16
  66. data/test/zoo.rb +0 -13
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)
data/test/perf_object.rb CHANGED
@@ -1,14 +1,13 @@
1
1
  #!/usr/bin/env ruby
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'
@@ -18,9 +17,9 @@ require 'perf'
18
17
  require 'sample'
19
18
  require 'files'
20
19
 
21
- $circular = false
22
- $indent = 0
23
- $allow_gc = true
20
+ @circular = false
21
+ @indent = 0
22
+ @allow_gc = true
24
23
 
25
24
  do_sample = false
26
25
  do_files = false
@@ -29,34 +28,34 @@ do_load = false
29
28
  do_dump = false
30
29
  do_read = false
31
30
  do_write = false
32
- $iter = 1000
33
- $mult = 1
31
+ @iter = 1000
32
+ @mult = 1
34
33
 
35
34
  opts = OptionParser.new
36
- opts.on("-c", "circular options") { $circular = true }
35
+ opts.on('-c', 'circular options') { @circular = true }
37
36
 
38
- opts.on("-x", "use sample instead of files") { do_sample = true }
39
- opts.on("-g", "no GC during parsing") { $allow_gc = false }
37
+ opts.on('-x', 'use sample instead of files') { do_sample = true }
38
+ opts.on('-g', 'no GC during parsing') { @allow_gc = false }
40
39
 
41
- opts.on("-s", "load and dump as sample Ruby object") { do_sample = true }
42
- opts.on("-f", "load and dump as files Ruby object") { do_files = true }
40
+ opts.on('-s', 'load and dump as sample Ruby object') { do_sample = true }
41
+ opts.on('-f', 'load and dump as files Ruby object') { do_files = true }
43
42
 
44
- opts.on("-l", "load") { do_load = true }
45
- opts.on("-d", "dump") { do_dump = true }
46
- opts.on("-r", "read") { do_read = true }
47
- opts.on("-w", "write") { do_write = true }
48
- opts.on("-a", "load, dump, read and write") { do_load = true; do_dump = true; do_read = true; do_write = true }
43
+ opts.on('-l', 'load') { do_load = true }
44
+ opts.on('-d', 'dump') { do_dump = true }
45
+ opts.on('-r', 'read') { do_read = true }
46
+ opts.on('-w', 'write') { do_write = true }
47
+ opts.on('-a', 'load, dump, read and write') { do_load = true; do_dump = true; do_read = true; do_write = true }
49
48
 
50
- opts.on("-i", "--iterations [Int]", Integer, "iterations") { |i| $iter = i }
51
- opts.on("-m", "--multiply [Int]", Integer, "multiplier") { |i| $mult = i }
49
+ opts.on('-i', '--iterations [Int]', Integer, 'iterations') { |v| @iter = v }
50
+ opts.on('-m', '--multiply [Int]', Integer, 'multiplier') { |v| @mult = v }
52
51
 
53
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
52
+ opts.on('-h', '--help', 'Show this display') { puts opts; Process.exit!(0) }
54
53
  files = opts.parse(ARGV)
55
54
 
56
- $obj = nil
57
- $xml = nil
58
- $mars = nil
59
- $json = nil
55
+ @obj = nil
56
+ @xml = nil
57
+ @mars = nil
58
+ @json = nil
60
59
 
61
60
  unless do_load || do_dump || do_read || do_write
62
61
  do_load = true
@@ -67,71 +66,69 @@ end
67
66
 
68
67
  # prepare all the formats for input
69
68
  if files.empty?
70
- $obj = []
71
- $mult.times do
72
- $obj << (do_sample ? sample_doc(2) : files('..'))
69
+ @obj = []
70
+ @mult.times do
71
+ @obj << (do_sample ? sample_doc(2) : files('..'))
73
72
  end
74
73
 
75
- $mars = Marshal.dump($obj)
76
- $xml = Ox.dump($obj, :indent => $indent, :circular => $circular)
77
- $json = Oj.dump($obj, :indent => $indent, :circular => $circular, :mode => :object)
78
- File.open('sample.xml', 'w') { |f| f.write($xml) }
79
- File.open('sample.json', 'w') { |f| f.write($json) }
80
- File.open('sample.marshal', 'w') { |f| f.write($mars) }
74
+ @mars = Marshal.dump(@obj)
75
+ @xml = Ox.dump(@obj, :indent => @indent, :circular => @circular)
76
+ @json = Oj.dump(@obj, :indent => @indent, :circular => @circular, :mode => :object)
77
+ File.write('sample.xml', @xml)
78
+ File.write('sample.json', @json)
79
+ File.write('sample.marshal', @mars)
81
80
  else
82
81
  puts "loading and parsing #{files}\n\n"
83
- data = files.map do |f|
84
- $xml = File.read(f)
85
- $obj = Ox.load($xml);
86
- $mars = Marshal.dump($obj)
87
- $json = Oj.dump($obj, :indent => $indent, :circular => $circular)
82
+ files.map do |f|
83
+ @xml = File.read(f)
84
+ @obj = Ox.load(@xml)
85
+ @mars = Marshal.dump(@obj)
86
+ @json = Oj.dump(@obj, :indent => @indent, :circular => @circular)
88
87
  end
89
88
  end
90
89
 
91
- Oj.default_options = { :mode => :object, :indent => $indent, :circular => $circular, :allow_gc => $allow_gc }
92
- #puts "json: #{$json.size}"
93
- #puts "xml: #{$xml.size}"
94
- #puts "marshal: #{$mars.size}"
90
+ Oj.default_options = { :mode => :object, :indent => @indent, :circular => @circular, :allow_gc => @allow_gc }
91
+ # puts "json: #{@json.size}"
92
+ # puts "xml: #{@xml.size}"
93
+ # puts "marshal: #{@mars.size}"
95
94
 
96
95
  if do_load
97
96
  puts '-' * 80
98
- puts "Load Performance"
97
+ puts 'Load Performance'
99
98
  perf = Perf.new()
100
- perf.add('Oj.object', 'load') { Oj.object_load($json) }
101
- perf.add('Ox', 'load') { Ox.load($xml, :mode => :object) }
102
- perf.add('Marshal', 'load') { Marshal.load($mars) }
103
- perf.run($iter)
99
+ perf.add('Oj.object', 'load') { Oj.object_load(@json) }
100
+ perf.add('Ox', 'load') { Ox.load(@xml, :mode => :object) }
101
+ perf.add('Marshal', 'load') { Marshal.load(@mars) }
102
+ perf.run(@iter)
104
103
  end
105
104
 
106
105
  if do_dump
107
106
  puts '-' * 80
108
- puts "Dump Performance"
107
+ puts 'Dump Performance'
109
108
  perf = Perf.new()
110
- perf.add('Oj', 'dump') { Oj.dump($obj) }
111
- perf.add('Ox', 'dump') { Ox.dump($obj, :indent => $indent, :circular => $circular) }
112
- perf.add('Marshal', 'dump') { Marshal.dump($obj) }
113
- perf.run($iter)
109
+ perf.add('Oj', 'dump') { Oj.dump(@obj) }
110
+ perf.add('Ox', 'dump') { Ox.dump(@obj, :indent => @indent, :circular => @circular) }
111
+ perf.add('Marshal', 'dump') { Marshal.dump(@obj) }
112
+ perf.run(@iter)
114
113
  end
115
114
 
116
115
  if do_read
117
116
  puts '-' * 80
118
- puts "Read from file Performance"
117
+ puts 'Read from file Performance'
119
118
  perf = Perf.new()
120
119
  perf.add('Oj', 'load') { Oj.load_file('sample.json') }
121
- #perf.add('Oj', 'load') { Oj.load(File.read('sample.json')) }
120
+ # perf.add('Oj', 'load') { Oj.load(File.read('sample.json')) }
122
121
  perf.add('Ox', 'load_file') { Ox.load_file('sample.xml', :mode => :object) }
123
122
  perf.add('Marshal', 'load') { Marshal.load(File.new('sample.marshal')) }
124
- perf.run($iter)
123
+ perf.run(@iter)
125
124
  end
126
125
 
127
126
  if do_write
128
127
  puts '-' * 80
129
- puts "Write to file Performance"
128
+ puts 'Write to file Performance'
130
129
  perf = Perf.new()
131
- perf.add('Oj', 'to_file') { Oj.to_file('sample.json', $obj) }
132
- perf.add('Ox', 'to_file') { Ox.to_file('sample.xml', $obj, :indent => $indent, :circular => $circular) }
133
- perf.add('Marshal', 'dump') { Marshal.dump($obj, File.new('sample.marshal', 'w')) }
134
- perf.run($iter)
130
+ perf.add('Oj', 'to_file') { Oj.to_file('sample.json', @obj) }
131
+ perf.add('Ox', 'to_file') { Ox.to_file('sample.xml', @obj, :indent => @indent, :circular => @circular) }
132
+ perf.add('Marshal', 'dump') { Marshal.dump(@obj, File.new('sample.marshal', 'w')) }
133
+ perf.run(@iter)
135
134
  end
136
-
137
-
data/test/perf_once.rb CHANGED
@@ -1,14 +1,15 @@
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
+ require 'English'
5
+ $LOAD_PATH << '.'
6
+ $LOAD_PATH << File.join(__dir__, '../lib')
7
+ $LOAD_PATH << File.join(__dir__, '../ext')
7
8
 
8
9
  require 'oj'
9
10
 
10
11
  filename = 'tmp.json'
11
- File.open(filename, "w") { |f|
12
+ File.open(filename, 'w') { |f|
12
13
  cnt = 0
13
14
  f.puts('{')
14
15
  ('a'..'z').each { |a|
@@ -25,7 +26,7 @@ File.open(filename, "w") { |f|
25
26
  }
26
27
 
27
28
  def mem
28
- `ps -o rss= -p #{$$}`.to_i
29
+ `ps -o rss= -p #{$PROCESS_ID}`.to_i
29
30
  end
30
31
 
31
32
  Oj.default_options = { mode: :strict, cache_keys: false, cache_str: -1 }
@@ -48,7 +49,7 @@ dur = Time.now - start
48
49
  GC.start
49
50
  puts "second cache duration: #{dur} @ #{mem}"
50
51
 
51
- 10.times{ GC.start }
52
+ 10.times { GC.start }
52
53
  start = Time.now
53
54
  Oj.load_file('tmp.json')
54
55
  dur = Time.now - start