Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 155 lines (133 sloc) 3.694 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 'test/unit'
8 require 'optparse'
9 require 'yajl'
924f206 added tests
Peter Ohler authored
10 require 'json'
11 require 'json/pure'
12 require 'json/ext'
12658d6 started json parser
Peter Ohler authored
13 require 'oj'
14
15 $indent = 2
16
17 opts = OptionParser.new
18 opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
19 files = opts.parse(ARGV)
20
b8f3b5a added string and number parsing
Peter Ohler authored
21 iter = 100000
12658d6 started json parser
Peter Ohler authored
22 s = %{
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
23 { "class": "Foo::Bar",
24 "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
25 "attr2": { "one": 1 }
26 }
12658d6 started json parser
Peter Ohler authored
27 }
924f206 added tests
Peter Ohler authored
28 #s = File.read('sample.json')
12658d6 started json parser
Peter Ohler authored
29
47ec1c4 added options and tests
Peter Ohler authored
30 Oj.default_options = { :indent => 0 }
31
32 puts
33
924f206 added tests
Peter Ohler authored
34 parse_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
35
12658d6 started json parser
Peter Ohler authored
36 start = Time.now
37 iter.times do
38 Oj.load(s)
39 end
924f206 added tests
Peter Ohler authored
40 dt = Time.now - start
41 parse_results[:oj] = dt
42 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
43
44 start = Time.now
45 iter.times do
46 Yajl::Parser.parse(s)
47 end
924f206 added tests
Peter Ohler authored
48 dt = Time.now - start
49 base = dt
50 parse_results[:yajl] = dt
51 puts "%d Yajl::Parser.parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
52
53 begin
54 JSON.parser = JSON::Ext::Parser
55 start = Time.now
56 iter.times do
57 JSON.parse(s)
58 end
59 dt = Time.now - start
60 base = dt
61 base_name = 'JSON::Ext'
62 parse_results[:ext] = dt
63 puts "%d JSON::Ext::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
64 rescue Exception => e
65 puts "JSON::Ext failed: #{e.class}: #{e.message}"
66 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
67
924f206 added tests
Peter Ohler authored
68 begin
69 JSON.parser = JSON::Pure::Parser
70 start = Time.now
71 iter.times do
72 JSON.parse(s)
73 end
74 dt = Time.now - start
75 base = dt
76 base_name = 'JSON::Pure'
77 parse_results[:pure] = dt
78 puts "%d JSON::Pure::Parser parse()s in %0.3f seconds or %0.1f parses/msec" % [iter, dt, iter/dt/1000.0]
79 rescue Exception => e
80 puts "JSON::Pure failed: #{e.class}: #{e.message}"
81 end
82
83 puts "Parser results:"
84 puts "gem seconds parses/msec X faster than #{base_name}"
85 parse_results.each do |name,dt|
86 puts "%-4s %6.3f %5.1f %4.1f" % [name, dt, iter/dt/1000.0, base/dt]
87 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
88
47ec1c4 added options and tests
Peter Ohler authored
89 puts
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
90
924f206 added tests
Peter Ohler authored
91 dump_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
92 obj = Oj.load(s)
924f206 added tests
Peter Ohler authored
93
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
94 start = Time.now
95 iter.times do
96 Oj.dump(obj)
97 end
924f206 added tests
Peter Ohler authored
98 dt = Time.now - start
99 parse_results[:oj] = dt
100 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 encoding ...
Peter Ohler authored
101
102 start = Time.now
103 iter.times do
104 Yajl::Encoder.encode(obj)
105 end
924f206 added tests
Peter Ohler authored
106 dt = Time.now - start
107 base = dt
108 base_name = 'YAJL'
109 parse_results[:yajl] = dt
110 puts "%d Yajl::Encoder.encode()s in %0.3f seconds or %0.1f encodes/msec" % [iter, dt, iter/dt/1000.0]
111
112 begin
113 JSON.parser = JSON::Ext::Parser
114 start = Time.now
115 iter.times do
116 JSON.generate(obj)
117 end
118 dt = Time.now - start
119 base = dt
120 base_name = 'JSON::Ext'
121 parse_results[:pure] = dt
122 puts "%d JSON::Ext generate()s in %0.3f seconds or %0.1f generates/msec" % [iter, dt, iter/dt/1000.0]
123 rescue Exception => e
124 parse_results[:ext] = 0.0
125 puts "JSON::Ext failed: #{e.class}: #{e.message}"
126 end
b8f3b5a added string and number parsing
Peter Ohler authored
127
924f206 added tests
Peter Ohler authored
128 begin
129 JSON.parser = JSON::Pure::Parser
130 start = Time.now
131 iter.times do
132 JSON.generate(obj)
133 end
134 dt = Time.now - start
135 base = dt
136 base_name = 'JSON::Pure'
137 parse_results[:pure] = dt
138 puts "%d JSON::Pure generate()s in %0.3f seconds or %0.1f generates/msec" % [iter, dt, iter/dt/1000.0]
139 rescue Exception => e
140 parse_results[:pure] = 0.0
141 puts "JSON::Pure failed: #{e.class}: #{e.message}"
142 end
143
144 puts "Parser results:"
145 puts "gem seconds dumps/msec X faster than #{base_name}"
146 parse_results.each do |name,dt|
147 if 0.0 == dt
148 puts "#{name} failed to generate JSON"
149 next
150 end
151 puts "%-4s %6.3f %5.1f %4.1f" % [name, dt, iter/dt/1000.0, base/dt]
152 end
7bf61c3 dump and load are working at 2X faster than Yajl. Need work on encoding ...
Peter Ohler authored
153
47ec1c4 added options and tests
Peter Ohler authored
154 puts
Something went wrong with that request. Please try again.