Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 288 lines (259 sloc) 7.19 kB
12658d6 started json parser
Peter Ohler authored
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'
924f206 added tests
Peter Ohler authored
9 require 'json'
10 require 'json/pure'
11 require 'json/ext'
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
12 require 'msgpack'
12658d6 started json parser
Peter Ohler authored
13 require 'oj'
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
14 require 'ox'
12658d6 started json parser
Peter Ohler authored
15
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
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
eb4fcaa @frsyuki updated to_msgpack() implementation
frsyuki authored
42 def to_msgpack(out='')
43 to_hash().to_msgpack(out)
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
44 end
45 end
46
12658d6 started json parser
Peter Ohler authored
47 $indent = 2
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
48 $iter = 10000
49 $with_object = true
6669206 ready for 0.6.0 release
Peter Ohler authored
50 $with_bignum = true
51 $with_nums = true
12658d6 started json parser
Peter Ohler authored
52
53 opts = OptionParser.new
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
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 }
6669206 ready for 0.6.0 release
Peter Ohler authored
57 opts.on("-b", "without bignum") { $with_bignum = false }
58 opts.on("-n", "without numbers") { $with_nums = false }
12658d6 started json parser
Peter Ohler authored
59 opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
60 files = opts.parse(ARGV)
61
6669206 ready for 0.6.0 release
Peter Ohler authored
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,
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
70 }
6669206 ready for 0.6.0 release
Peter Ohler authored
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
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
83
84 Oj.default_options = { :indent => $indent, :mode => :object }
85
86 s = Oj.dump(obj)
12658d6 started json parser
Peter Ohler authored
87
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
88 xml = Ox.dump(obj, :indent => $indent)
47ec1c4 added options and tests
Peter Ohler authored
89
90 puts
91
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
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 }
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
96 parse_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }
924f206 added tests
Peter Ohler authored
97
12658d6 started json parser
Peter Ohler authored
98 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
99 $iter.times do
12658d6 started json parser
Peter Ohler authored
100 Oj.load(s)
101 end
924f206 added tests
Peter Ohler authored
102 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
103 base_dt = dt
924f206 added tests
Peter Ohler authored
104 parse_results[:oj] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
105 puts "%d Oj.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]
12658d6 started json parser
Peter Ohler authored
106
107 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
108 $iter.times do
12658d6 started json parser
Peter Ohler authored
109 Yajl::Parser.parse(s)
110 end
924f206 added tests
Peter Ohler authored
111 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
112 if base_dt < dt
113 base_dt = dt
114 base_name = 'Yajl'
115 end
924f206 added tests
Peter Ohler authored
116 parse_results[:yajl] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
117 puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
118
119 begin
120 JSON.parser = JSON::Ext::Parser
121 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
122 $iter.times do
924f206 added tests
Peter Ohler authored
123 JSON.parse(s)
124 end
125 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
126 if base_dt < dt
127 base_dt = dt
128 base_name = 'JSON::Ext'
129 end
924f206 added tests
Peter Ohler authored
130 parse_results[:ext] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
131 puts "%d JSON::Ext::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
132 rescue Exception => e
133 puts "JSON::Ext failed: #{e.class}: #{e.message}"
134 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
135
924f206 added tests
Peter Ohler authored
136 begin
137 JSON.parser = JSON::Pure::Parser
138 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
139 $iter.times do
924f206 added tests
Peter Ohler authored
140 JSON.parse(s)
141 end
142 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
143 if base_dt < dt
144 base_dt = dt
145 base_name = 'JSON::Pure'
146 end
924f206 added tests
Peter Ohler authored
147 parse_results[:pure] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
148 puts "%d JSON::Pure::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
149 rescue Exception => e
150 puts "JSON::Pure failed: #{e.class}: #{e.message}"
151 end
152
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
153 begin
6669206 ready for 0.6.0 release
Peter Ohler authored
154 mp = MessagePack.pack(obj)
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
155 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
156 $iter.times do
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
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
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
165 puts "%d MessagePack.unpack()s in %0.3f seconds or %0.1f packs/msec" % [$iter, dt, $iter/dt/1000.0]
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
166 rescue Exception => e
6669206 ready for 0.6.0 release
Peter Ohler authored
167 puts "MessagePack failed: #{e.class}: #{e.message}"
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
168 end
169
170 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
171 $iter.times do
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
172 Ox.load(xml)
173 end
174 dt = Time.now - start
175 parse_results[:ox] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
176 puts "%d Ox.load()s in %0.3f seconds or %0.1f loads/msec" % [$iter, dt, $iter/dt/1000.0]
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
177
924f206 added tests
Peter Ohler authored
178 puts "Parser results:"
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
179 puts "gem seconds parses/msec X faster than #{base_name} (higher is better)"
924f206 added tests
Peter Ohler authored
180 parse_results.each do |name,dt|
6669206 ready for 0.6.0 release
Peter Ohler authored
181 if 0.0 == dt
182 puts "#{name} failed to generate JSON"
183 next
184 end
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
185 puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
924f206 added tests
Peter Ohler authored
186 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
187
47ec1c4 added options and tests
Peter Ohler authored
188 puts
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
189
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
190 # Back to object mode for best performance when dumping.
191 Oj.default_options = { :indent => $indent, :mode => :object }
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
192 dump_results = { :oj => 0.0, :yajl => 0.0, :msgpack => 0.0, :pure => 0.0, :ext => 0.0, :ox => 0.0 }
924f206 added tests
Peter Ohler authored
193
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
194 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
195 $iter.times do
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
196 Oj.dump(obj)
197 end
924f206 added tests
Peter Ohler authored
198 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
199 base_dt = dt
200 base_name = 'Oj'
924f206 added tests
Peter Ohler authored
201 parse_results[:oj] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
202 puts "%d Oj.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
203
204 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
205 $iter.times do
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
206 Yajl::Encoder.encode(obj)
207 end
924f206 added tests
Peter Ohler authored
208 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
209 if base_dt < dt
210 base_dt = dt
211 base_name = 'Yajl'
212 end
924f206 added tests
Peter Ohler authored
213 parse_results[:yajl] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
214 puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
215
216 begin
217 JSON.parser = JSON::Ext::Parser
218 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
219 $iter.times do
924f206 added tests
Peter Ohler authored
220 JSON.generate(obj)
221 end
222 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
223 if base_dt < dt
224 base_dt = dt
225 base_name = 'JSON::Ext'
226 end
924f206 added tests
Peter Ohler authored
227 parse_results[:pure] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
228 puts "%d JSON::Ext generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
229 rescue Exception => e
230 parse_results[:ext] = 0.0
231 puts "JSON::Ext failed: #{e.class}: #{e.message}"
232 end
b8f3b5a added string and number parsing
Peter Ohler authored
233
924f206 added tests
Peter Ohler authored
234 begin
235 JSON.parser = JSON::Pure::Parser
236 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
237 $iter.times do
924f206 added tests
Peter Ohler authored
238 JSON.generate(obj)
239 end
240 dt = Time.now - start
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
241 if base_dt < dt
242 base_dt = dt
243 base_name = 'JSON::Pure'
244 end
924f206 added tests
Peter Ohler authored
245 parse_results[:pure] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
246 puts "%d JSON::Pure generate()s in %0.3f seconds or %0.1f generates/msec" % [$iter, dt, $iter/dt/1000.0]
924f206 added tests
Peter Ohler authored
247 rescue Exception => e
248 parse_results[:pure] = 0.0
249 puts "JSON::Pure failed: #{e.class}: #{e.message}"
250 end
251
6669206 ready for 0.6.0 release
Peter Ohler authored
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}"
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
267 end
268
269 start = Time.now
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
270 $iter.times do
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
271 Ox.dump(obj)
272 end
273 dt = Time.now - start
274 parse_results[:ox] = dt
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
275 puts "%d Ox.dump()s in %0.3f seconds or %0.1f dumps/msec" % [$iter, dt, $iter/dt/1000.0]
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
276
924f206 added tests
Peter Ohler authored
277 puts "Parser results:"
b84b112 added a few more options to performance testing and prefixed extern C…
Peter Ohler authored
278 puts "gem seconds dumps/msec X faster than #{base_name} (higher is better)"
924f206 added tests
Peter Ohler authored
279 parse_results.each do |name,dt|
280 if 0.0 == dt
281 puts "#{name} failed to generate JSON"
282 next
283 end
8711c85 serializes any Object now. Tests added as well.
Peter Ohler authored
284 puts "%-7s %6.3f %5.1f %4.1f" % [name, dt, $iter/dt/1000.0, base_dt/dt]
924f206 added tests
Peter Ohler authored
285 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encodi…
Peter Ohler authored
286
47ec1c4 added options and tests
Peter Ohler authored
287 puts
Something went wrong with that request. Please try again.