Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 154 lines (133 sloc) 3.694 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 'test/unit'
8 require 'optparse'
9 require 'yajl'
924f2067 » Peter Ohler
2012-02-20 added tests
10 require 'json'
11 require 'json/pure'
12 require 'json/ext'
12658d6c » Peter Ohler
2012-02-17 started json parser
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
b8f3b5a8 » Peter Ohler
2012-02-18 added string and number parsing
21 iter = 100000
12658d6c » Peter Ohler
2012-02-17 started json parser
22 s = %{
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
23 { "class": "Foo::Bar",
24 "attr1": [ true, [false, [12345, null], 3.967, ["something", false], null]],
25 "attr2": { "one": 1 }
26 }
12658d6c » Peter Ohler
2012-02-17 started json parser
27 }
924f2067 » Peter Ohler
2012-02-20 added tests
28 #s = File.read('sample.json')
12658d6c » Peter Ohler
2012-02-17 started json parser
29
47ec1c44 » Peter Ohler
2012-02-19 added options and tests
30 Oj.default_options = { :indent => 0 }
31
32 puts
33
924f2067 » Peter Ohler
2012-02-20 added tests
34 parse_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
35
12658d6c » Peter Ohler
2012-02-17 started json parser
36 start = Time.now
37 iter.times do
38 Oj.load(s)
39 end
924f2067 » Peter Ohler
2012-02-20 added tests
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]
12658d6c » Peter Ohler
2012-02-17 started json parser
43
44 start = Time.now
45 iter.times do
46 Yajl::Parser.parse(s)
47 end
924f2067 » Peter Ohler
2012-02-20 added tests
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
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
67
924f2067 » Peter Ohler
2012-02-20 added tests
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
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
88
47ec1c44 » Peter Ohler
2012-02-19 added options and tests
89 puts
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
90
924f2067 » Peter Ohler
2012-02-20 added tests
91 dump_results = { :oj => 0.0, :yajl => 0.0, :pure => 0.0, :ext => 0.0 }
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
92 obj = Oj.load(s)
924f2067 » Peter Ohler
2012-02-20 added tests
93
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
94 start = Time.now
95 iter.times do
96 Oj.dump(obj)
97 end
924f2067 » Peter Ohler
2012-02-20 added tests
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]
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
101
102 start = Time.now
103 iter.times do
104 Yajl::Encoder.encode(obj)
105 end
924f2067 » Peter Ohler
2012-02-20 added tests
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
b8f3b5a8 » Peter Ohler
2012-02-18 added string and number parsing
127
924f2067 » Peter Ohler
2012-02-20 added tests
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
7bf61c31 » Peter Ohler
2012-02-19 dump and load are working at 2X faster than Yajl. Need work on encodi…
153
47ec1c44 » Peter Ohler
2012-02-19 added options and tests
154 puts
Something went wrong with that request. Please try again.