Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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