summaryrefslogtreecommitdiff
path: root/benchmarks/parser_benchmark.rb
blob: ed2331a3f3ad87888272e6529b688f1bc4e0073d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env ruby
# CODING: UTF-8

require 'rbconfig'
RUBY_PATH=File.join(Config::CONFIG['bindir'], Config::CONFIG['ruby_install_name'])
RAKE_PATH=File.join(Config::CONFIG['bindir'], 'rake')
require 'bullshit'
case ARGV.first
when 'ext'
  require 'json/ext'
when 'pure'
  require 'json/pure'
when 'yaml'
  require 'yaml'
  require 'json/pure'
when 'rails'
  require 'active_support'
  require 'json/pure'
when 'yajl'
  require 'yajl'
  require 'json/pure'
else
  require 'json/pure'
end

module ParserBenchmarkCommon
  include JSON

  def setup
    a = [ nil, false, true, "fÖß\nÄr", [ "n€st€d", true ], { "fooß" => "bär", "qu\r\nux" => true } ]
    @big = a * 100
    @json = JSON.generate(@big)
  end

  def generic_reset_method
    @result == @big or raise "not equal"
  end
end

class ParserBenchmarkExt < Bullshit::RepeatCase
  include ParserBenchmarkCommon

  warmup      yes
  iterations  2000

  truncate_data do
    enabled false
    alpha_level 0.05
    window_size 50
    slope_angle 0.1
  end

  autocorrelation do
    alpha_level 0.05
    max_lags    50
    file        yes
  end

  output_dir File.join(File.dirname(__FILE__), 'data')
  output_filename benchmark_name + '.log'
  data_file yes
  histogram yes

  def benchmark_parser
    @result = JSON.parse(@json)
  end

  alias reset_parser generic_reset_method

  def benchmark_parser_symbolic
    @result = JSON.parse(@json, :symbolize_names => true)
  end

  alias reset_parser_symbolc generic_reset_method
end

class ParserBenchmarkPure < Bullshit::RepeatCase
  include ParserBenchmarkCommon

  warmup      yes
  iterations  400

  truncate_data do
    enabled false
    alpha_level 0.05
    window_size 50
    slope_angle 0.1
  end

  autocorrelation do
    alpha_level 0.05
    max_lags    50
    file        yes
  end

  output_dir File.join(File.dirname(__FILE__), 'data')
  output_filename benchmark_name + '.log'
  data_file yes
  histogram yes

  def benchmark_parser
    @result = JSON.parse(@json)
  end

  alias reset_parser generic_reset_method

  def benchmark_parser_symbolic
    @result = JSON.parse(@json, :symbolize_names => true)
  end

  alias reset_parser_symbolc generic_reset_method
end

class ParserBenchmarkYAML < Bullshit::RepeatCase
  warmup      yes
  iterations  400

  truncate_data do
    enabled false
    alpha_level 0.05
    window_size 50
    slope_angle 0.1
  end

  autocorrelation do
    alpha_level 0.05
    max_lags    50
    file        yes
  end

  output_dir File.join(File.dirname(__FILE__), 'data')
  output_filename benchmark_name + '.log'
  data_file yes
  histogram yes

  def setup
    a = [ nil, false, true, "fÖß\nÄr", [ "n€st€d", true ], { "fooß" => "bär", "qu\r\nux" => true } ]
    @big = a * 100
    @json = JSON.pretty_generate(@big)
  end

  def benchmark_parser
    @result = YAML.load(@json)
  end

  def generic_reset_method
    @result == @big or raise "not equal"
  end
end

class ParserBenchmarkRails < Bullshit::RepeatCase
  warmup      yes
  iterations  400

  truncate_data do
    enabled false
    alpha_level 0.05
    window_size 50
    slope_angle 0.1
  end

  autocorrelation do
    alpha_level 0.05
    max_lags    50
    file        yes
  end

  output_dir File.join(File.dirname(__FILE__), 'data')
  output_filename benchmark_name + '.log'
  data_file yes
  histogram yes

  def setup
    a = [ nil, false, true, "fÖß\nÄr", [ "n€st€d", true ], { "fooß" => "bär", "qu\r\nux" => true } ]
    @big = a * 100
    @json = JSON.generate(@big)
  end

  def benchmark_parser
    @result = ActiveSupport::JSON.decode(@json)
  end

  def generic_reset_method
    @result == @big or raise "not equal"
  end
end

class ParserBenchmarkYajl < Bullshit::RepeatCase
  warmup      yes
  iterations  2000

  truncate_data do
    enabled false
    alpha_level 0.05
    window_size 50
    slope_angle 0.1
  end

  autocorrelation do
    alpha_level 0.05
    max_lags    50
    file        yes
  end

  output_dir File.join(File.dirname(__FILE__), 'data')
  output_filename benchmark_name + '.log'
  data_file yes
  histogram yes

  def setup
    a = [ nil, false, true, "fÖß\nÄr", [ "n€st€d", true ], { "fooß" => "bär", "qu\r\nux" => true } ]
    @big = a * 100
    @json = JSON.generate(@big)
  end

  def benchmark_parser
    @result = Yajl::Parser.new.parse(@json)
  end

  def generic_reset_method
    @result == @big or raise "not equal"
  end
end

if $0 == __FILE__
  Bullshit::Case.autorun false

  case ARGV.first
  when 'ext'
    ParserBenchmarkExt.run
  when 'pure'
    ParserBenchmarkPure.run
  when 'yaml'
    ParserBenchmarkYAML.run
  when 'rails'
    ParserBenchmarkRails.run
  when 'yajl'
    ParserBenchmarkYajl.run
  else
    system "#{RAKE_PATH} clean"
    system "#{RUBY_PATH} #$0 yaml"
    system "#{RUBY_PATH} #$0 rails"
    system "#{RUBY_PATH} #$0 pure"
    system "#{RAKE_PATH} compile"
    system "#{RUBY_PATH} #$0 ext"
    system "#{RUBY_PATH} #$0 yajl"
    Bullshit.compare do
      output_filename File.join(File.dirname(__FILE__), 'data', 'ParserBenchmarkComparison.log')

      benchmark ParserBenchmarkExt,   :parser, :load => yes
      benchmark ParserBenchmarkExt,   :parser_symbolic, :load => yes
      benchmark ParserBenchmarkPure,  :parser, :load => yes
      benchmark ParserBenchmarkPure,  :parser_symbolic, :load => yes
      benchmark ParserBenchmarkYAML,  :parser, :load => yes
      benchmark ParserBenchmarkRails, :parser, :load => yes
      benchmark ParserBenchmarkYajl,  :parser, :load => yes
    end
  end
end