oj 2.1.2 → 2.1.3

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of oj might be problematic. Click here for more details.

@@ -1,151 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'optparse'
9
- require 'yajl'
10
- require 'perf'
11
- require 'json'
12
- require 'json/ext'
13
- require 'oj'
14
-
15
- $verbose = false
16
- $indent = 0
17
- $iter = 50000
18
- $with_bignum = false
19
- $size = 0
20
-
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 = {
31
- 'a' => 'Alpha', # string
32
- 'b' => true, # boolean
33
- 'c' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
35
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
36
- 'f' => nil, # nil
37
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
38
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
39
- }
40
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
41
-
42
- if 0 < $size
43
- o = $obj
44
- $obj = []
45
- (4 * $size).times do
46
- $obj << o
47
- end
48
- end
49
-
50
- Oj.default_options = { :indent => $indent, :mode => :compat }
51
-
52
- $json = Oj.dump($obj)
53
- $failed = {} # key is same as String used in tests later
54
-
55
- class AllSaj < Oj::Saj
56
- def initialize()
57
- end
58
-
59
- def hash_start(key)
60
- end
61
-
62
- def hash_end(key)
63
- end
64
-
65
- def array_start(key)
66
- end
67
-
68
- def array_end(key)
69
- end
70
-
71
- def add_value(value, key)
72
- end
73
- end # AllSaj
74
-
75
- class NoSaj < Oj::Saj
76
- def initialize()
77
- end
78
- end # NoSaj
79
-
80
- class NoHandler < Oj::ScHandler
81
- def initialize()
82
- end
83
- end # NoHandler
84
-
85
- class AllHandler < Oj::ScHandler
86
- def initialize()
87
- end
88
-
89
- def hash_start()
90
- return nil
91
- end
92
-
93
- def hash_end()
94
- end
95
-
96
- def array_start()
97
- return nil
98
- end
99
-
100
- def array_end()
101
- end
102
-
103
- def add_value(value)
104
- end
105
-
106
- def hash_set(h, key, value)
107
- end
108
-
109
- def array_append(a, value)
110
- end
111
-
112
- end # AllHandler
113
-
114
- saj_handler = AllSaj.new()
115
- no_saj = NoSaj.new()
116
-
117
- sc_handler = AllHandler.new()
118
- no_handler = NoHandler.new()
119
-
120
- 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
127
- end
128
-
129
- # 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 }
132
-
133
- if $verbose
134
- puts "json:\n#{$json}\n"
135
- end
136
-
137
- puts '-' * 80
138
- puts "Parse Performance"
139
- 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}" }
151
- end
@@ -1,287 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
6
-
7
- require 'optparse'
8
- require 'yajl'
9
- require 'json'
10
- require 'json/pure'
11
- require 'json/ext'
12
- require 'msgpack'
13
- require 'oj'
14
- require 'ox'
15
-
16
- class Jazz
17
- def initialize()
18
- @boolean = true
19
- @number = 58
20
- @string = "A string"
21
- @array = [true, false, nil]
22
- @hash = { 'one' => 1, 'two' => 2 }
23
- end
24
- def to_json()
25
- %{
26
- { "boolean":#{@boolean},
27
- "number":#{@number},
28
- "string":#{@string},
29
- "array":#{@array},
30
- "hash":#{@hash},
31
- }
32
- }
33
- end
34
- def to_hash()
35
- { 'boolean' => @boolean,
36
- 'number' => @number,
37
- 'string' => @string,
38
- 'array' => @array,
39
- 'hash' => @hash,
40
- }
41
- end
42
- def to_msgpack(out='')
43
- to_hash().to_msgpack(out)
44
- end
45
- end
46
-
47
- $indent = 2
48
- $iter = 10000
49
- $with_object = true
50
- $with_bignum = true
51
- $with_nums = true
52
-
53
- 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)
61
-
62
- if $with_nums
63
- obj = {
64
- 'a' => 'Alpha',
65
- 'b' => true,
66
- 'c' => 12345,
67
- 'd' => [ true, [false, [12345, nil], 3.967, ['something', false], nil]],
68
- 'e' => { 'one' => 1, 'two' => 2 },
69
- 'f' => nil,
70
- }
71
- obj['g'] = Jazz.new() if $with_object
72
- obj['h'] = 12345678901234567890123456789 if $with_bignum
73
- else
74
- obj = {
75
- 'a' => 'Alpha',
76
- 'b' => true,
77
- 'c' => '12345',
78
- 'd' => [ true, [false, ['12345', nil], '3.967', ['something', false], nil]],
79
- 'e' => { 'one' => '1', 'two' => '2' },
80
- 'f' => nil,
81
- }
82
- end
83
-
84
- Oj.default_options = { :indent => $indent, :mode => :object }
85
-
86
- s = Oj.dump(obj)
87
-
88
- xml = Ox.dump(obj, :indent => $indent)
89
-
90
- puts
91
-
92
- # Put Oj in strict mode so it only create JSON native types instead of the
93
- # original Ruby Objects. None of the other packages other than Ox support
94
- # Object recreation so no need for Oj to do it in the performance tests.
95
- Oj.default_options = { :mode => :strict }
96
- parse_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }
97
-
98
- start = Time.now
99
- $iter.times do
100
- Oj.load(s)
101
- end
102
- dt = Time.now - start
103
- base_dt = dt
104
- 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]
106
-
107
- start = Time.now
108
- $iter.times do
109
- Yajl::Parser.parse(s)
110
- end
111
- dt = Time.now - start
112
- if base_dt < dt
113
- base_dt = dt
114
- base_name = 'Yajl'
115
- end
116
- 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]
118
-
119
- begin
120
- JSON.parser = JSON::Ext::Parser
121
- start = Time.now
122
- $iter.times do
123
- JSON.parse(s)
124
- end
125
- dt = Time.now - start
126
- if base_dt < dt
127
- base_dt = dt
128
- base_name = 'JSON::Ext'
129
- end
130
- 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]
132
- rescue Exception => e
133
- puts "JSON::Ext failed: #{e.class}: #{e.message}"
134
- end
135
-
136
- begin
137
- JSON.parser = JSON::Pure::Parser
138
- start = Time.now
139
- $iter.times do
140
- JSON.parse(s)
141
- end
142
- dt = Time.now - start
143
- if base_dt < dt
144
- base_dt = dt
145
- base_name = 'JSON::Pure'
146
- end
147
- 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]
149
- rescue Exception => e
150
- puts "JSON::Pure failed: #{e.class}: #{e.message}"
151
- end
152
-
153
- begin
154
- mp = MessagePack.pack(obj)
155
- start = Time.now
156
- $iter.times do
157
- MessagePack.unpack(mp)
158
- end
159
- dt = Time.now - start
160
- if base_dt < dt
161
- base_dt = dt
162
- base_name = 'MessagePack'
163
- end
164
- 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]
166
- rescue Exception => e
167
- puts "MessagePack failed: #{e.class}: #{e.message}"
168
- end
169
-
170
- start = Time.now
171
- $iter.times do
172
- Ox.load(xml)
173
- end
174
- dt = Time.now - start
175
- 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]
177
-
178
- puts "Parser results:"
179
- 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
182
- puts "#{name} failed to generate JSON"
183
- next
184
- end
185
- puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
186
- end
187
-
188
- puts
189
-
190
- # Back to object mode for best performance when dumping.
191
- 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
-
194
- start = Time.now
195
- $iter.times do
196
- Oj.dump(obj)
197
- end
198
- dt = Time.now - start
199
- base_dt = dt
200
- base_name = 'Oj'
201
- 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]
203
-
204
- start = Time.now
205
- $iter.times do
206
- Yajl::Encoder.encode(obj)
207
- end
208
- dt = Time.now - start
209
- if base_dt < dt
210
- base_dt = dt
211
- base_name = 'Yajl'
212
- end
213
- 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]
215
-
216
- begin
217
- JSON.parser = JSON::Ext::Parser
218
- start = Time.now
219
- $iter.times do
220
- JSON.generate(obj)
221
- end
222
- dt = Time.now - start
223
- if base_dt < dt
224
- base_dt = dt
225
- base_name = 'JSON::Ext'
226
- end
227
- 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]
229
- rescue Exception => e
230
- parse_results[:ext] = 0.0
231
- puts "JSON::Ext failed: #{e.class}: #{e.message}"
232
- end
233
-
234
- begin
235
- JSON.parser = JSON::Pure::Parser
236
- start = Time.now
237
- $iter.times do
238
- JSON.generate(obj)
239
- end
240
- dt = Time.now - start
241
- if base_dt < dt
242
- base_dt = dt
243
- base_name = 'JSON::Pure'
244
- end
245
- 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]
247
- rescue Exception => e
248
- parse_results[:pure] = 0.0
249
- puts "JSON::Pure failed: #{e.class}: #{e.message}"
250
- end
251
-
252
- begin
253
- start = Time.now
254
- $iter.times do
255
- MessagePack.pack(obj)
256
- end
257
- dt = Time.now - start
258
- if base_dt < dt
259
- base_dt = dt
260
- base_name = 'MessagePack'
261
- end
262
- parse_results[:msgpack] = dt
263
- puts "%d Msgpack()s in %0.3f seconds or %0.1f unpacks/msec" % [$iter, dt, $iter/dt/1000.0]
264
- rescue Exception => e
265
- parse_results[:msgpack] = 0.0
266
- puts "MessagePack failed: #{e.class}: #{e.message}"
267
- end
268
-
269
- start = Time.now
270
- $iter.times do
271
- Ox.dump(obj)
272
- end
273
- dt = Time.now - start
274
- 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]
276
-
277
- puts "Parser results:"
278
- 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
- puts "#{name} failed to generate JSON"
282
- next
283
- end
284
- puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
285
- end
286
-
287
- puts
@@ -1,127 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << '.'
5
- $: << File.join(File.dirname(__FILE__), "../lib")
6
- $: << File.join(File.dirname(__FILE__), "../ext")
7
-
8
- require 'optparse'
9
- require 'perf'
10
- require 'oj'
11
-
12
- $verbose = false
13
- $indent = 0
14
- $iter = 20000
15
- $with_bignum = false
16
- $with_nums = true
17
- $size = 0
18
-
19
- 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 = {
31
- 'a' => 'Alpha', # string
32
- 'b' => true, # boolean
33
- 'c' => 12345, # number
34
- 'd' => [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
35
- 'e' => { 'zero' => nil, 'one' => 1, 'two' => 2, 'three' => [3], 'four' => [0, 1, 2, 3, 4] }, # hash
36
- 'f' => nil, # nil
37
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
38
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
39
- }
40
- $obj['g'] = 12345678901234567890123456789 if $with_bignum
41
- else
42
- $obj = {
43
- 'a' => 'Alpha',
44
- 'b' => true,
45
- 'c' => '12345',
46
- 'd' => [ true, [false, ['12345', nil], '3.967', ['something', false], nil]],
47
- 'e' => { 'zero' => '0', 'one' => '1', 'two' => '2' },
48
- 'f' => nil,
49
- 'h' => { 'a' => { 'b' => { 'c' => { 'd' => {'e' => { 'f' => { 'g' => nil }}}}}}}, # deep hash, not that deep
50
- 'i' => [[[[[[[nil]]]]]]] # deep array, again, not that deep
51
- }
52
- end
53
-
54
- Oj.default_options = { :indent => $indent, :mode => :strict }
55
-
56
- if 0 < $size
57
- o = $obj
58
- $obj = []
59
- (4 * $size).times do
60
- $obj << o
61
- end
62
- end
63
-
64
- $json = Oj.dump($obj)
65
- $obj_json = Oj.dump($obj, :mode => :object)
66
- #puts "*** size: #{$obj_json.size}"
67
- $failed = {} # key is same as String used in tests later
68
-
69
- def capture_error(tag, orig, load_key, dump_key, &blk)
70
- begin
71
- obj = blk.call(orig)
72
- raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
73
- rescue Exception => e
74
- $failed[tag] = "#{e.class}: #{e.message}"
75
- end
76
- end
77
-
78
- # Verify that all packages dump and load correctly and return the same Object as the original.
79
- capture_error('Oj:strict', $obj, 'load', 'dump') { |o| Oj.strict_load(Oj.dump(o, :mode => :strict)) }
80
- capture_error('Yajl', $obj, 'encode', 'parse') { |o| require 'yajl'; Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
81
- capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
82
- require 'json'
83
- require 'json/ext'
84
- JSON.generator = JSON::Ext::Generator
85
- JSON.parser = JSON::Ext::Parser
86
- JSON.parse(JSON.generate(o))
87
- }
88
- capture_error('JSON::Pure', $obj, 'generate', 'parse') { |o|
89
- require 'json/pure'
90
- JSON.generator = JSON::Pure::Generator
91
- JSON.parser = JSON::Pure::Parser
92
- JSON.parse(JSON.generate(o))
93
- }
94
-
95
- if $verbose
96
- puts "json:\n#{$json}\n"
97
- puts "object json:\n#{$obj_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"
101
- end
102
-
103
- puts '-' * 80
104
- puts "Strict Parse Performance"
105
- perf = Perf.new()
106
- unless $failed.has_key?('JSON::Ext')
107
- perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
108
- perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
109
- end
110
- unless $failed.has_key?('JSON::Pure')
111
- perf.add('JSON::Pure', 'parse') { JSON.parse($json) }
112
- perf.before('JSON::Pure') { JSON.parser = JSON::Pure::Parser }
113
- end
114
- unless $failed.has_key?('Oj:strict')
115
- perf.add('Oj:strict', 'strict_load') { Oj.strict_load($json) }
116
- end
117
- perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
118
- perf.run($iter)
119
-
120
- puts
121
- puts '-' * 80
122
- puts
123
-
124
- unless $failed.empty?
125
- puts "The following packages were not included for the reason listed"
126
- $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
127
- end