summaryrefslogtreecommitdiff
path: root/benchmark/bench.rb
blob: e7a0b04361652c362c02a713b35f2114feb8809e (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/usr/bin/env ruby

###
### $Rev$
### $Release: $
### $Copyright$
###

require 'erb'
require 'erubis'
require 'erubis/tiny'
require 'erubis/engine/enhanced'
require 'yaml'
require 'cgi'
include ERB::Util

begin
  require 'eruby'
rescue LoadError
  ERuby = nil
end

def File.write(filename, content)
  File.open(filename, 'w') { |f| f.write(content) }
end


## change benchmark library to use $stderr instead of $stdout
require 'benchmark'
module Benchmark
  class Report
    def print(*args)
      $stderr.print(*args)
    end
  end
  module_function
  def print(*args)
    $stderr.print(*args)
  end
end


class BenchmarkApplication

  TARGETS = %w[eruby
               ERB               ERB(cached)
               Erubis::Eruby     Erubis::Eruby(cached)
               Erubis::FastEruby Erubis::FastEruby(cached)
               Erubis::TinyEruby
               Erubis::ArrayBufferEruby
               Erubis::PrintOutEruby
               Erubis::StdoutEruby
              ]

  def initialize(ntimes, context, targets=nil, params={})
    @ntimes      = ntimes
    @context     = context
    @targets     = targets && !targets.empty? ? targets : TARGETS.dup
    @testmode    = params[:testmode]    || 'execute'
    @erubyfile   = params[:erubyfile]   || 'erubybench.rhtml'
    @printout    = params[:printout]    || false
  end

  attr_accessor :ntimes, :targets
  attr_accessor :testmode, :erubyfile, :contextfile, :printout

  def context2code(context, varname='context')
    s = ''
    context.each { |k, | s << "#{k} = #{varname}[#{k.inspect}]; " }
    return s
  end

  def perform_benchmark
    width = 30
    $stderr.puts "*** ntimes=#{@ntimes}, testmode=#{@testmode}"
    Benchmark.bm(width) do |job|
      for target in @targets do
        method = "#{@testmode}_#{target.gsub(/::|-|\(/, '_').gsub(/\)/, '').downcase}"
        #$stderr.puts "*** debug: method=#{method.inspect}"
        next unless self.respond_to?(method)
        filename = "bench_#{(target =~ /^(\w+)/) && $1.downcase}.rhtml"
        title = target
        output = nil
        job.report(title) do
          output = self.__send__(method, filename, @context)
        end
        File.write("output.#{target.gsub(/[^\w]/,'')}", output) if @printout && output && !output.empty?
      end
    end
  end

  ##

  def execute_eruby(filename, context)
    return unless ERuby
    eval context2code(context)
    @ntimes.times do
      ERuby.import(filename)
    end
    return nil
  end

  def execute_erb(filename, context)
    eval context2code(context)
    output = nil
    @ntimes.times do
      eruby = ERB.new(File.read(filename))
      output = eruby.result(binding())
      print output
    end
    return output
  end

  def execute_erb_cached(filename, context)
    eval context2code(context)
    output = nil
    cachefile = filename + '.cache'
    File.unlink(cachefile) if test(?f, cachefile)
    @ntimes.times do
      if !test(?f, cachefile) || File.mtime(filename) > File.mtime(cachefile)
        eruby = ERB.new(File.read(filename))
        File.write(cachefile, eruby.src)
      else
        eruby = ERB.new('')
        #eruby.src = File.read(cachefile)
        eruby.instance_variable_set("@src", File.read(cachefile))
      end
      output = eruby.result(binding())
      print output
    end
    return output
  end

  ## no cached
  for klass in %w[Eruby FastEruby TinyEruby ArrayBufferEruby PrintOutEruby StdoutEruby] do
    s = <<-END
    def execute_erubis_#{klass.downcase}(filename, context)
      eval context2code(context)
      output = nil
      @ntimes.times do
        eruby = Erubis::#{klass}.new(File.read(filename))
        output = eruby.result(binding())
        print output
      end
      return output
    end
    END
    eval s
  end

  ## cached
  for klass in %w[Eruby FastEruby] do
    s = <<-END
    def execute_erubis_#{klass.downcase}_cached(filename, context)
      eval context2code(context)
      cachefile = filename + '.cache'
      File.unlink(cachefile) if test(?f, cachefile)
      output = nil
      @ntimes.times do
        eruby = Erubis::#{klass}.load_file(filename)
        output = eruby.result(binding())
        print output
      end
      savefile = cachefile.sub(/\\.cache$/, '.#{klass.downcase}.cache')
      File.rename(cachefile, savefile)
      return output
    end
    END
    eval s
  end

  ##

  def convert_eruby(filename, context)
    return unless ERuby
    eval context2code(context)
    output = nil
    @ntimes.times do
      output = ERuby::Compiler.new.compile_string(File.read(filename))
    end
    return output
  end

  def convert_erb(filename, context)
    eval context2code(context)
    output = nil
    @ntimes.times do
      eruby = ERB.new(File.read(filename))
      output = eruby.src
    end
    return output
  end

  for klass in %w[Eruby FastEruby TinyEruby]
    s = <<-END
      def convert_erubis_#{klass.downcase}(filename, context)
        eval context2code(context)
        output = nil
        @ntimes.times do
          eruby = Erubis::#{klass}.new(File.read(filename))
          output = eruby.src
        end
        return output
      end
    END
    eval s
  end

end


require 'optparse'

class MainApplication

  def parse_argv(argv=ARGV)
    optparser = OptionParser.new
    options = {}
    ['-h', '-n N', '-t erubyfile', '-f contextfile', '-A', '-e',
      '-x exclude', '-m testmode', '-X', '-p', '-D'].each do |opt|
      optparser.on(opt) { |val| options[opt[1].chr] = val }
    end
    begin
      targets = optparser.parse!(argv)
    rescue => ex
      $stderr.puts "#{@script}: #{ex.to_s}"
      exit(1)
    end
    return options, targets
  end

  def execute
    @script = File.basename($0)
    ntimes = 1000
    targets = BenchmarkApplication::TARGETS.dup
    testmode = 'execute'
    contextfile = 'erubybench.yaml'
    #
    options, args = parse_argv(ARGV)
    ntimes      = options['n'].to_i if options['n']
    targets     = args if args && !args.empty?
    targets     = targets - options['x'].split(/,/) if options['x']
    testmode    = options['m'] if options['m']
    contextfile = options['f'] if options['f']
    erubyfile   = options['t'] if options['t']
    #
    if options['h']
      $stderr.puts "Usage: ruby #{@script} [..options..] [..targets..]"
      $stderr.puts "  -h           :  help"
      $stderr.puts "  -n N         :  loop N times"
      $stderr.puts "  -f datafile  :  context data filename (*.yaml)"
      $stderr.puts "  -x exclude   :  exclude target name"
      $stdout.puts "  -m testmode  :  'execute' or 'convert' (default 'execute')"
      $stderr.puts "  -p           :  print output to file (filename: 'output.TARGETNAME')"
      return
    end
    #
    #if ! options['t']
    for item in %w[eruby erb erubis]
      fname = "bench_#{item}.rhtml"
      header = File.read("templates/_header.html")
      #body   = File.read("templates/#{erubyfile}")
      body   = File.read("templates/#{fname}")
      footer = File.read("templates/_footer.html")
      content = header + body + footer
      File.write(fname, content)
    end
    #
    if options['e']   # escape
      tuples = [
        [ 'bench_eruby.rhtml',  '<%= CGI.escapeHTML((\1).to_s) %>' ],
        [ 'bench_erb.rhtml',    '<%=h \1 %>' ],
        [ 'bench_erubis.rhtml', '<%== \1 %>' ],
      ]
      for fname, replace in tuples
        content = File.read(fname).gsub(/<%= ?(.*?) ?%>/, replace)
        File.write(fname, content)
      end
      targets.delete('Erubis::TinyEruby')   ## because TinyEruby doesn't support '<%== =>'
    end
    #
    context = YAML.load_file(contextfile)
    #
    params = {
      :printout=>options['p'],
      :testmode=>testmode,
    }
    app = BenchmarkApplication.new(ntimes, context, targets, params)
    app.perform_benchmark()
  end

end


if __FILE__ == $0

  ## open /dev/null
  $stdout = File.open('/dev/null', 'w')
  at_exit do
    $stdout.close()
  end

  ## start benchmark
  MainApplication.new().execute()

end