oj 3.7.12

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +21 -0
  3. data/README.md +96 -0
  4. data/ext/oj/buf.h +103 -0
  5. data/ext/oj/cache8.c +107 -0
  6. data/ext/oj/cache8.h +48 -0
  7. data/ext/oj/circarray.c +68 -0
  8. data/ext/oj/circarray.h +23 -0
  9. data/ext/oj/code.c +235 -0
  10. data/ext/oj/code.h +42 -0
  11. data/ext/oj/compat.c +299 -0
  12. data/ext/oj/custom.c +1188 -0
  13. data/ext/oj/dump.c +1232 -0
  14. data/ext/oj/dump.h +94 -0
  15. data/ext/oj/dump_compat.c +973 -0
  16. data/ext/oj/dump_leaf.c +252 -0
  17. data/ext/oj/dump_object.c +837 -0
  18. data/ext/oj/dump_strict.c +433 -0
  19. data/ext/oj/encode.h +45 -0
  20. data/ext/oj/err.c +57 -0
  21. data/ext/oj/err.h +70 -0
  22. data/ext/oj/extconf.rb +47 -0
  23. data/ext/oj/fast.c +1771 -0
  24. data/ext/oj/hash.c +163 -0
  25. data/ext/oj/hash.h +46 -0
  26. data/ext/oj/hash_test.c +512 -0
  27. data/ext/oj/mimic_json.c +873 -0
  28. data/ext/oj/object.c +771 -0
  29. data/ext/oj/odd.c +231 -0
  30. data/ext/oj/odd.h +44 -0
  31. data/ext/oj/oj.c +1694 -0
  32. data/ext/oj/oj.h +381 -0
  33. data/ext/oj/parse.c +1085 -0
  34. data/ext/oj/parse.h +111 -0
  35. data/ext/oj/rails.c +1485 -0
  36. data/ext/oj/rails.h +21 -0
  37. data/ext/oj/reader.c +231 -0
  38. data/ext/oj/reader.h +151 -0
  39. data/ext/oj/resolve.c +102 -0
  40. data/ext/oj/resolve.h +14 -0
  41. data/ext/oj/rxclass.c +147 -0
  42. data/ext/oj/rxclass.h +27 -0
  43. data/ext/oj/saj.c +714 -0
  44. data/ext/oj/scp.c +224 -0
  45. data/ext/oj/sparse.c +910 -0
  46. data/ext/oj/stream_writer.c +363 -0
  47. data/ext/oj/strict.c +212 -0
  48. data/ext/oj/string_writer.c +512 -0
  49. data/ext/oj/trace.c +79 -0
  50. data/ext/oj/trace.h +28 -0
  51. data/ext/oj/util.c +136 -0
  52. data/ext/oj/util.h +19 -0
  53. data/ext/oj/val_stack.c +118 -0
  54. data/ext/oj/val_stack.h +185 -0
  55. data/ext/oj/wab.c +631 -0
  56. data/lib/oj.rb +21 -0
  57. data/lib/oj/active_support_helper.rb +41 -0
  58. data/lib/oj/bag.rb +88 -0
  59. data/lib/oj/easy_hash.rb +52 -0
  60. data/lib/oj/error.rb +22 -0
  61. data/lib/oj/json.rb +176 -0
  62. data/lib/oj/mimic.rb +267 -0
  63. data/lib/oj/saj.rb +66 -0
  64. data/lib/oj/schandler.rb +142 -0
  65. data/lib/oj/state.rb +131 -0
  66. data/lib/oj/version.rb +5 -0
  67. data/pages/Advanced.md +22 -0
  68. data/pages/Compatibility.md +25 -0
  69. data/pages/Custom.md +23 -0
  70. data/pages/Encoding.md +65 -0
  71. data/pages/JsonGem.md +79 -0
  72. data/pages/Modes.md +154 -0
  73. data/pages/Options.md +266 -0
  74. data/pages/Rails.md +116 -0
  75. data/pages/Security.md +20 -0
  76. data/pages/WAB.md +13 -0
  77. data/test/_test_active.rb +76 -0
  78. data/test/_test_active_mimic.rb +96 -0
  79. data/test/_test_mimic_rails.rb +126 -0
  80. data/test/activerecord/result_test.rb +27 -0
  81. data/test/activesupport4/decoding_test.rb +108 -0
  82. data/test/activesupport4/encoding_test.rb +531 -0
  83. data/test/activesupport4/test_helper.rb +41 -0
  84. data/test/activesupport5/decoding_test.rb +125 -0
  85. data/test/activesupport5/encoding_test.rb +485 -0
  86. data/test/activesupport5/encoding_test_cases.rb +90 -0
  87. data/test/activesupport5/test_helper.rb +50 -0
  88. data/test/activesupport5/time_zone_test_helpers.rb +24 -0
  89. data/test/big.rb +15 -0
  90. data/test/files.rb +29 -0
  91. data/test/foo.rb +33 -0
  92. data/test/helper.rb +26 -0
  93. data/test/isolated/shared.rb +308 -0
  94. data/test/isolated/test_mimic_after.rb +13 -0
  95. data/test/isolated/test_mimic_alone.rb +12 -0
  96. data/test/isolated/test_mimic_as_json.rb +45 -0
  97. data/test/isolated/test_mimic_before.rb +13 -0
  98. data/test/isolated/test_mimic_define.rb +28 -0
  99. data/test/isolated/test_mimic_rails_after.rb +22 -0
  100. data/test/isolated/test_mimic_rails_before.rb +21 -0
  101. data/test/isolated/test_mimic_redefine.rb +15 -0
  102. data/test/json_gem/json_addition_test.rb +216 -0
  103. data/test/json_gem/json_common_interface_test.rb +148 -0
  104. data/test/json_gem/json_encoding_test.rb +107 -0
  105. data/test/json_gem/json_ext_parser_test.rb +20 -0
  106. data/test/json_gem/json_fixtures_test.rb +35 -0
  107. data/test/json_gem/json_generator_test.rb +383 -0
  108. data/test/json_gem/json_generic_object_test.rb +90 -0
  109. data/test/json_gem/json_parser_test.rb +470 -0
  110. data/test/json_gem/json_string_matching_test.rb +42 -0
  111. data/test/json_gem/test_helper.rb +18 -0
  112. data/test/mem.rb +35 -0
  113. data/test/perf.rb +107 -0
  114. data/test/perf_compat.rb +130 -0
  115. data/test/perf_fast.rb +164 -0
  116. data/test/perf_file.rb +64 -0
  117. data/test/perf_object.rb +138 -0
  118. data/test/perf_saj.rb +109 -0
  119. data/test/perf_scp.rb +151 -0
  120. data/test/perf_simple.rb +287 -0
  121. data/test/perf_strict.rb +145 -0
  122. data/test/perf_wab.rb +131 -0
  123. data/test/sample.rb +54 -0
  124. data/test/sample/change.rb +14 -0
  125. data/test/sample/dir.rb +19 -0
  126. data/test/sample/doc.rb +36 -0
  127. data/test/sample/file.rb +48 -0
  128. data/test/sample/group.rb +16 -0
  129. data/test/sample/hasprops.rb +16 -0
  130. data/test/sample/layer.rb +12 -0
  131. data/test/sample/line.rb +20 -0
  132. data/test/sample/oval.rb +10 -0
  133. data/test/sample/rect.rb +10 -0
  134. data/test/sample/shape.rb +35 -0
  135. data/test/sample/text.rb +20 -0
  136. data/test/sample_json.rb +37 -0
  137. data/test/test_compat.rb +509 -0
  138. data/test/test_custom.rb +406 -0
  139. data/test/test_debian.rb +53 -0
  140. data/test/test_fast.rb +470 -0
  141. data/test/test_file.rb +239 -0
  142. data/test/test_gc.rb +49 -0
  143. data/test/test_hash.rb +29 -0
  144. data/test/test_integer_range.rb +73 -0
  145. data/test/test_null.rb +376 -0
  146. data/test/test_object.rb +1018 -0
  147. data/test/test_saj.rb +186 -0
  148. data/test/test_scp.rb +433 -0
  149. data/test/test_strict.rb +410 -0
  150. data/test/test_various.rb +739 -0
  151. data/test/test_wab.rb +307 -0
  152. data/test/test_writer.rb +380 -0
  153. data/test/tests.rb +24 -0
  154. data/test/tests_mimic.rb +14 -0
  155. data/test/tests_mimic_addition.rb +7 -0
  156. metadata +359 -0
@@ -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,145 @@
1
+ #!/usr/bin/env ruby
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
+ #puts "*** #{$obj_json}"
68
+ $failed = {} # key is same as String used in tests later
69
+
70
+ 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
77
+ end
78
+
79
+ # 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|
83
+ require 'json'
84
+ require 'json/ext'
85
+ JSON.generator = JSON::Ext::Generator
86
+ JSON.parser = JSON::Ext::Parser
87
+ JSON.parse(JSON.generate(o))
88
+ }
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
+
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"
102
+ end
103
+
104
+ puts '-' * 80
105
+ puts "Strict Parse Performance"
106
+ perf = Perf.new()
107
+ unless $failed.has_key?('JSON::Ext')
108
+ perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
109
+ perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
110
+ 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 }
114
+ 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)
120
+
121
+ puts '-' * 80
122
+ puts "Strict Dump Performance"
123
+ perf = Perf.new()
124
+ unless $failed.has_key?('JSON::Ext')
125
+ perf.add('JSON::Ext', 'dump') { JSON.generate($obj) }
126
+ perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
127
+ 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) }
134
+ end
135
+ perf.add('Yajl', 'encode') { Yajl::Encoder.encode($obj) } unless $failed.has_key?('Yajl')
136
+ perf.run($iter)
137
+
138
+ puts
139
+ puts '-' * 80
140
+ puts
141
+
142
+ unless $failed.empty?
143
+ puts "The following packages were not included for the reason listed"
144
+ $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
145
+ end
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env ruby
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("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
26
+ files = opts.parse(ARGV)
27
+
28
+ $obj = {
29
+ a: 'Alpha', # string
30
+ b: true, # boolean
31
+ c: 12345, # number
32
+ d: [ true, [false, [-123456789, nil], 3.9676, ['Something else.', false], nil]], # mix it up array
33
+ e: { zero: nil, one: 1, two: 2, three: [3], four: [0, 1, 2, 3, 4] }, # hash
34
+ f: nil, # nil
35
+ h: { a: { b: { c: { d: {e: { f: { g: nil }}}}}}}, # deep hash, not that deep
36
+ i: [[[[[[[nil]]]]]]] # deep array, again, not that deep
37
+ }
38
+ $obj[:g] = 12345678901234567890123456789 if $with_bignum
39
+
40
+ Oj.default_options = { :indent => $indent, :mode => :wab }
41
+
42
+ if 0 < $size
43
+ o = $obj
44
+ $obj = []
45
+ (4 * $size).times do
46
+ $obj << o
47
+ end
48
+ end
49
+
50
+ $json = Oj.dump($obj)
51
+ $obj_json = Oj.dump($obj, :mode => :object)
52
+ #puts "*** size: #{$obj_json.size}"
53
+ #puts "*** #{$obj_json}"
54
+ $failed = {} # key is same as String used in tests later
55
+
56
+ def capture_error(tag, orig, load_key, dump_key, &blk)
57
+ begin
58
+ obj = blk.call(orig)
59
+ raise "#{tag} #{dump_key} and #{load_key} did not return the same object as the original." unless orig == obj
60
+ rescue Exception => e
61
+ $failed[tag] = "#{e.class}: #{e.message}"
62
+ end
63
+ end
64
+
65
+ # Verify that all packages dump and load correctly and return the same Object as the original.
66
+ capture_error('Oj:wab', $obj, 'load', 'dump') { |o| Oj.wab_load(Oj.dump(o, :mode => :wab)) }
67
+ capture_error('Yajl', $obj, 'encode', 'parse') { |o| require 'yajl'; Yajl::Parser.parse(Yajl::Encoder.encode(o)) }
68
+ capture_error('JSON::Ext', $obj, 'generate', 'parse') { |o|
69
+ require 'json'
70
+ require 'json/ext'
71
+ JSON.generator = JSON::Ext::Generator
72
+ JSON.parser = JSON::Ext::Parser
73
+ JSON.parse(JSON.generate(o))
74
+ }
75
+ capture_error('JSON::Pure', $obj, 'generate', 'parse') { |o|
76
+ require 'json/pure'
77
+ JSON.generator = JSON::Pure::Generator
78
+ JSON.parser = JSON::Pure::Parser
79
+ JSON.parse(JSON.generate(o))
80
+ }
81
+
82
+ if $verbose
83
+ puts "json:\n#{$json}\n"
84
+ puts "object json:\n#{$obj_json}\n"
85
+ puts "Oj loaded object:\n#{Oj.wab_load($json)}\n"
86
+ puts "Yajl loaded object:\n#{Yajl::Parser.parse($json)}\n"
87
+ puts "JSON loaded object:\n#{JSON::Ext::Parser.new($json).parse}\n"
88
+ end
89
+
90
+ puts '-' * 80
91
+ puts "Wab Parse Performance"
92
+ perf = Perf.new()
93
+ unless $failed.has_key?('JSON::Ext')
94
+ perf.add('JSON::Ext', 'parse') { JSON.parse($json) }
95
+ perf.before('JSON::Ext') { JSON.parser = JSON::Ext::Parser }
96
+ end
97
+ unless $failed.has_key?('JSON::Pure')
98
+ perf.add('JSON::Pure', 'parse') { JSON.parse($json) }
99
+ perf.before('JSON::Pure') { JSON.parser = JSON::Pure::Parser }
100
+ end
101
+ unless $failed.has_key?('Oj:wab')
102
+ perf.add('Oj:wab', 'wab_load') { Oj.wab_load($json) }
103
+ end
104
+ perf.add('Yajl', 'parse') { Yajl::Parser.parse($json) } unless $failed.has_key?('Yajl')
105
+ perf.run($iter)
106
+
107
+ puts '-' * 80
108
+ puts "Wab Dump Performance"
109
+ perf = Perf.new()
110
+ unless $failed.has_key?('JSON::Ext')
111
+ perf.add('JSON::Ext', 'dump') { JSON.generate($obj) }
112
+ perf.before('JSON::Ext') { JSON.generator = JSON::Ext::Generator }
113
+ end
114
+ unless $failed.has_key?('JSON::Pure')
115
+ perf.add('JSON::Pure', 'generate') { JSON.generate($obj) }
116
+ perf.before('JSON::Pure') { JSON.generator = JSON::Pure::Generator }
117
+ end
118
+ unless $failed.has_key?('Oj:wab')
119
+ perf.add('Oj:wab', 'dump') { Oj.dump($obj, :mode => :wab) }
120
+ end
121
+ perf.add('Yajl', 'encode') { Yajl::Encoder.encode($obj) } unless $failed.has_key?('Yajl')
122
+ perf.run($iter)
123
+
124
+ puts
125
+ puts '-' * 80
126
+ puts
127
+
128
+ unless $failed.empty?
129
+ puts "The following packages were not included for the reason listed"
130
+ $failed.each { |tag,msg| puts "***** #{tag}: #{msg}" }
131
+ end