oj 2.1.3 → 2.1.4

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.

Files changed (52) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +2 -4
  3. data/ext/oj/compat.c +3 -4
  4. data/ext/oj/encode.h +51 -0
  5. data/ext/oj/fast.c +4 -9
  6. data/ext/oj/object.c +7 -19
  7. data/ext/oj/oj.c +32 -12
  8. data/ext/oj/oj.h +3 -0
  9. data/ext/oj/parse.c +2 -3
  10. data/ext/oj/saj.c +4 -9
  11. data/ext/oj/scp.c +5 -12
  12. data/ext/oj/strict.c +5 -12
  13. data/lib/oj/version.rb +1 -1
  14. data/test/a.rb +38 -0
  15. data/test/bug.rb +15 -0
  16. data/test/e.rb +12 -0
  17. data/test/files.rb +29 -0
  18. data/test/foo.rb +24 -0
  19. data/test/mj.rb +48 -0
  20. data/test/perf.rb +107 -0
  21. data/test/perf_compat.rb +128 -0
  22. data/test/perf_fast.rb +164 -0
  23. data/test/perf_object.rb +136 -0
  24. data/test/perf_saj.rb +109 -0
  25. data/test/perf_scp.rb +151 -0
  26. data/test/perf_simple.rb +287 -0
  27. data/test/perf_strict.rb +127 -0
  28. data/test/sample.rb +55 -0
  29. data/test/sample/change.rb +14 -0
  30. data/test/sample/dir.rb +19 -0
  31. data/test/sample/doc.rb +36 -0
  32. data/test/sample/file.rb +48 -0
  33. data/test/sample/group.rb +16 -0
  34. data/test/sample/hasprops.rb +16 -0
  35. data/test/sample/layer.rb +12 -0
  36. data/test/sample/line.rb +20 -0
  37. data/test/sample/oval.rb +10 -0
  38. data/test/sample/rect.rb +10 -0
  39. data/test/sample/shape.rb +35 -0
  40. data/test/sample/text.rb +20 -0
  41. data/test/sample_json.rb +37 -0
  42. data/test/test_compat.rb +342 -0
  43. data/test/test_fast.rb +416 -0
  44. data/test/test_mimic.rb +208 -0
  45. data/test/test_mimic_after.rb +35 -0
  46. data/test/test_object.rb +390 -0
  47. data/test/test_saj.rb +184 -0
  48. data/test/test_scp.rb +224 -0
  49. data/test/test_strict.rb +259 -0
  50. data/test/tests.rb +1017 -0
  51. data/test/x.rb +59 -0
  52. metadata +41 -2
@@ -0,0 +1,287 @@
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
@@ -0,0 +1,127 @@
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
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby -wW2
2
+
3
+ if $0 == __FILE__
4
+ $: << '.'
5
+ $: << '..'
6
+ $: << '../lib'
7
+ $: << '../ext'
8
+ end
9
+
10
+ require 'pp'
11
+ require 'sample/doc'
12
+
13
+
14
+ def sample_doc(size=3)
15
+ colors = [ :black, :gray, :white, :red, :blue, :yellow, :green, :purple, :orange ]
16
+
17
+ d = ::Sample::Doc.new('Sample')
18
+
19
+ # add some history
20
+ (0..size * 10).each do |i|
21
+ d.add_change("Changed at t+#{i}.")
22
+ end
23
+
24
+ # add some layers
25
+ (1..size).each do |i|
26
+ layer = ::Sample::Layer.new("Layer-#{i}")
27
+ (1..size).each do |j|
28
+ g = ::Sample::Group.new
29
+ (1..size).each do |k|
30
+ g2 = ::Sample::Group.new
31
+ r = ::Sample::Rect.new(j * 40 + 10.0, i * 10.0,
32
+ 10.123456 / k, 10.0 / k, colors[(i + j + k) % colors.size])
33
+ r.add_prop(:part_of, layer.name)
34
+ g2 << r
35
+ g2 << ::Sample::Text.new("#{k} in #{j}", r.left, r.top, r.width, r.height)
36
+ g << g2
37
+ end
38
+ g2 = ::Sample::Group.new
39
+ (1..size).each do |k|
40
+ o = ::Sample::Oval.new(j * 40 + 12.0, i * 10.0 + 2.0,
41
+ 6.0 / k, 6.0 / k, colors[(i + j + k) % colors.size])
42
+ o.add_prop(:inside, true)
43
+ g << o
44
+ end
45
+ g << g2
46
+ layer << g
47
+ end
48
+ d.layers[layer.name] = layer
49
+ end
50
+
51
+ # some properties
52
+ d.add_prop(:purpose, 'an example')
53
+
54
+ d
55
+ end