~ubuntu-branches/ubuntu/saucy/ruby-erubis/saucy

« back to all changes in this revision

Viewing changes to benchmark/bench.rb

  • Committer: Package Import Robot
  • Author(s): Laurent Bigonville
  • Date: 2012-01-26 15:15:58 UTC
  • Revision ID: package-import@ubuntu.com-20120126151558-9u7mnf9ooqnw3bwz
Tags: upstream-2.7.0
ImportĀ upstreamĀ versionĀ 2.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
###
 
4
### $Release: 2.7.0 $
 
5
### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
 
6
###
 
7
 
 
8
require 'erb'
 
9
require 'erubis'
 
10
require 'erubis/tiny'
 
11
require 'erubis/engine/enhanced'
 
12
require 'yaml'
 
13
require 'cgi'
 
14
include ERB::Util
 
15
 
 
16
begin
 
17
  require 'eruby'
 
18
rescue LoadError
 
19
  ERuby = nil
 
20
end
 
21
 
 
22
def File.write(filename, content)
 
23
  File.open(filename, 'w') { |f| f.write(content) }
 
24
end
 
25
 
 
26
 
 
27
## change benchmark library to use $stderr instead of $stdout
 
28
require 'benchmark'
 
29
module Benchmark
 
30
  class Report
 
31
    def print(*args)
 
32
      $stderr.print(*args)
 
33
    end
 
34
  end
 
35
  module_function
 
36
  def print(*args)
 
37
    $stderr.print(*args)
 
38
  end
 
39
end
 
40
 
 
41
 
 
42
class BenchmarkApplication
 
43
 
 
44
  TARGETS = %w[eruby
 
45
               ERB               ERB(cached)
 
46
               Erubis::Eruby     Erubis::Eruby(cached)
 
47
               Erubis::FastEruby Erubis::FastEruby(cached)
 
48
               Erubis::TinyEruby
 
49
               Erubis::ArrayBufferEruby
 
50
               Erubis::PrintOutEruby
 
51
               Erubis::StdoutEruby
 
52
              ]
 
53
 
 
54
  def initialize(ntimes, context, targets=nil, params={})
 
55
    @ntimes      = ntimes
 
56
    @context     = context
 
57
    @targets     = targets && !targets.empty? ? targets : TARGETS.dup
 
58
    @testmode    = params[:testmode]    || 'execute'
 
59
    @erubyfile   = params[:erubyfile]   || 'erubybench.rhtml'
 
60
    @printout    = params[:printout]    || false
 
61
  end
 
62
 
 
63
  attr_accessor :ntimes, :targets
 
64
  attr_accessor :testmode, :erubyfile, :contextfile, :printout
 
65
 
 
66
  def context2code(context, varname='context')
 
67
    s = ''
 
68
    context.each { |k, | s << "#{k} = #{varname}[#{k.inspect}]; " }
 
69
    return s
 
70
  end
 
71
 
 
72
  def perform_benchmark
 
73
    width = 30
 
74
    $stderr.puts "*** ntimes=#{@ntimes}, testmode=#{@testmode}"
 
75
    Benchmark.bm(width) do |job|
 
76
      for target in @targets do
 
77
        method = "#{@testmode}_#{target.gsub(/::|-|\(/, '_').gsub(/\)/, '').downcase}"
 
78
        #$stderr.puts "*** debug: method=#{method.inspect}"
 
79
        next unless self.respond_to?(method)
 
80
        filename = "bench_#{(target =~ /^(\w+)/) && $1.downcase}.rhtml"
 
81
        title = target
 
82
        output = nil
 
83
        job.report(title) do
 
84
          output = self.__send__(method, filename, @context)
 
85
        end
 
86
        File.write("output.#{target.gsub(/[^\w]/,'')}", output) if @printout && output && !output.empty?
 
87
      end
 
88
    end
 
89
  end
 
90
 
 
91
  ##
 
92
 
 
93
  def execute_eruby(filename, context)
 
94
    return unless ERuby
 
95
    #eval context2code(context)
 
96
    list = context['list']
 
97
    @ntimes.times do
 
98
      ERuby.import(filename)
 
99
    end
 
100
    return nil
 
101
  end
 
102
 
 
103
  def execute_erb(filename, context)
 
104
    #eval context2code(context)
 
105
    list = context['list']
 
106
    output = nil
 
107
    @ntimes.times do
 
108
      eruby = ERB.new(File.read(filename))
 
109
      output = eruby.result(binding())
 
110
      print output
 
111
    end
 
112
    return output
 
113
  end
 
114
 
 
115
  def execute_erb_cached(filename, context)
 
116
    #eval context2code(context)
 
117
    list = context['list']
 
118
    output = nil
 
119
    cachefile = filename + '.cache'
 
120
    File.unlink(cachefile) if test(?f, cachefile)
 
121
    @ntimes.times do
 
122
      if !test(?f, cachefile) || File.mtime(filename) > File.mtime(cachefile)
 
123
        eruby = ERB.new(File.read(filename))
 
124
        File.write(cachefile, eruby.src)
 
125
      else
 
126
        eruby = ERB.new('')
 
127
        #eruby.src = File.read(cachefile)
 
128
        eruby.instance_variable_set("@src", File.read(cachefile))
 
129
      end
 
130
      output = eruby.result(binding())
 
131
      print output
 
132
    end
 
133
    return output
 
134
  end
 
135
 
 
136
  ## no cached
 
137
  for klass in %w[Eruby FastEruby TinyEruby ArrayBufferEruby PrintOutEruby StdoutEruby] do
 
138
    s = <<-END
 
139
    def execute_erubis_#{klass.downcase}(filename, context)
 
140
      #eval context2code(context)
 
141
      list = context['list']
 
142
      output = nil
 
143
      @ntimes.times do
 
144
        eruby = Erubis::#{klass}.new(File.read(filename))
 
145
        output = eruby.result(binding())
 
146
        print output
 
147
      end
 
148
      return output
 
149
    end
 
150
    END
 
151
    eval s
 
152
  end
 
153
 
 
154
  ## cached
 
155
  for klass in %w[Eruby FastEruby] do
 
156
    s = <<-END
 
157
    def execute_erubis_#{klass.downcase}_cached(filename, context)
 
158
      #eval context2code(context)
 
159
      list = context['list']
 
160
      cachefile = filename + '.cache'
 
161
      File.unlink(cachefile) if test(?f, cachefile)
 
162
      output = nil
 
163
      @ntimes.times do
 
164
        eruby = Erubis::#{klass}.load_file(filename)
 
165
        output = eruby.result(binding())
 
166
        print output
 
167
      end
 
168
      savefile = cachefile.sub(/\\.cache$/, '.#{klass.downcase}.cache')
 
169
      File.rename(cachefile, savefile)
 
170
      return output
 
171
    end
 
172
    END
 
173
    eval s
 
174
  end
 
175
 
 
176
  ##
 
177
 
 
178
  def convert_eruby(filename, context)
 
179
    return unless ERuby
 
180
    #eval context2code(context)
 
181
    list = context['list']
 
182
    output = nil
 
183
    @ntimes.times do
 
184
      output = ERuby::Compiler.new.compile_string(File.read(filename))
 
185
    end
 
186
    return output
 
187
  end
 
188
 
 
189
  def convert_erb(filename, context)
 
190
    #eval context2code(context)
 
191
    list = context['list']
 
192
    output = nil
 
193
    @ntimes.times do
 
194
      eruby = ERB.new(File.read(filename))
 
195
      output = eruby.src
 
196
    end
 
197
    return output
 
198
  end
 
199
 
 
200
  for klass in %w[Eruby FastEruby TinyEruby]
 
201
    s = <<-END
 
202
      def convert_erubis_#{klass.downcase}(filename, context)
 
203
        #eval context2code(context)
 
204
        list = context['list']
 
205
        output = nil
 
206
        @ntimes.times do
 
207
          eruby = Erubis::#{klass}.new(File.read(filename))
 
208
          output = eruby.src
 
209
        end
 
210
        return output
 
211
      end
 
212
    END
 
213
    eval s
 
214
  end
 
215
 
 
216
end
 
217
 
 
218
 
 
219
require 'optparse'
 
220
 
 
221
class MainApplication
 
222
 
 
223
  def parse_argv(argv=ARGV)
 
224
    optparser = OptionParser.new
 
225
    options = {}
 
226
    ['-h', '-n N', '-t erubyfile', '-f contextfile', '-A', '-e',
 
227
      '-x exclude', '-m testmode', '-X', '-p', '-D'].each do |opt|
 
228
      optparser.on(opt) { |val| options[opt[1].chr] = val }
 
229
    end
 
230
    begin
 
231
      targets = optparser.parse!(argv)
 
232
    rescue => ex
 
233
      $stderr.puts "#{@script}: #{ex.to_s}"
 
234
      exit(1)
 
235
    end
 
236
    return options, targets
 
237
  end
 
238
 
 
239
  def execute
 
240
    @script = File.basename($0)
 
241
    ntimes = 1000
 
242
    targets = BenchmarkApplication::TARGETS.dup
 
243
    testmode = 'execute'
 
244
    contextfile = 'bench_context.yaml'
 
245
    #
 
246
    options, args = parse_argv(ARGV)
 
247
    ntimes      = options['n'].to_i if options['n']
 
248
    targets     = args if args && !args.empty?
 
249
    targets     = targets - options['x'].split(/,/) if options['x']
 
250
    testmode    = options['m'] if options['m']
 
251
    contextfile = options['f'] if options['f']
 
252
    erubyfile   = options['t'] if options['t']
 
253
    #
 
254
    if options['h']
 
255
      $stderr.puts "Usage: ruby #{@script} [..options..] [..targets..]"
 
256
      $stderr.puts "  -h           :  help"
 
257
      $stderr.puts "  -n N         :  loop N times"
 
258
      $stderr.puts "  -f datafile  :  context data filename (*.yaml)"
 
259
      $stderr.puts "  -x exclude   :  exclude target name"
 
260
      $stdout.puts "  -m testmode  :  'execute' or 'convert' (default 'execute')"
 
261
      $stderr.puts "  -p           :  print output to file (filename: 'output.TARGETNAME')"
 
262
      return
 
263
    end
 
264
    #
 
265
    #if ! options['t']
 
266
    for item in %w[eruby erb erubis]
 
267
      fname = "bench_#{item}.rhtml"
 
268
      header = File.read("templates/_header.html")
 
269
      #body   = File.read("templates/#{erubyfile}")
 
270
      body   = File.read("templates/#{fname}")
 
271
      footer = File.read("templates/_footer.html")
 
272
      content = header + body + footer
 
273
      File.write(fname, content)
 
274
    end
 
275
    #
 
276
    if options['e']   # escape
 
277
      tuples = [
 
278
        [ 'bench_eruby.rhtml',  '<%= CGI.escapeHTML((\1).to_s) %>' ],
 
279
        [ 'bench_erb.rhtml',    '<%=h \1 %>' ],
 
280
        [ 'bench_erubis.rhtml', '<%== \1 %>' ],
 
281
      ]
 
282
      for fname, replace in tuples
 
283
        content = File.read(fname).gsub(/<%= ?(.*?) ?%>/, replace)
 
284
        File.write(fname, content)
 
285
      end
 
286
      targets.delete('Erubis::TinyEruby')   ## because TinyEruby doesn't support '<%== =>'
 
287
    end
 
288
    #
 
289
    context = YAML.load_file(contextfile)
 
290
    #
 
291
    params = {
 
292
      :printout=>options['p'],
 
293
      :testmode=>testmode,
 
294
    }
 
295
    app = BenchmarkApplication.new(ntimes, context, targets, params)
 
296
    app.perform_benchmark()
 
297
  end
 
298
 
 
299
end
 
300
 
 
301
 
 
302
if __FILE__ == $0
 
303
 
 
304
  ## open /dev/null
 
305
  $stdout = File.open('/dev/null', 'w')
 
306
  at_exit do
 
307
    $stdout.close()
 
308
  end
 
309
 
 
310
  ## start benchmark
 
311
  MainApplication.new().execute()
 
312
 
 
313
end