5
### copyright(c) 2006-2011 kuwata-lab.com all rights reserved.
11
require 'erubis/engine/enhanced'
22
def File.write(filename, content)
23
File.open(filename, 'w') { |f| f.write(content) }
27
## change benchmark library to use $stderr instead of $stdout
42
class BenchmarkApplication
46
Erubis::Eruby Erubis::Eruby(cached)
47
Erubis::FastEruby Erubis::FastEruby(cached)
49
Erubis::ArrayBufferEruby
54
def initialize(ntimes, context, targets=nil, params={})
57
@targets = targets && !targets.empty? ? targets : TARGETS.dup
58
@testmode = params[:testmode] || 'execute'
59
@erubyfile = params[:erubyfile] || 'erubybench.rhtml'
60
@printout = params[:printout] || false
63
attr_accessor :ntimes, :targets
64
attr_accessor :testmode, :erubyfile, :contextfile, :printout
66
def context2code(context, varname='context')
68
context.each { |k, | s << "#{k} = #{varname}[#{k.inspect}]; " }
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"
84
output = self.__send__(method, filename, @context)
86
File.write("output.#{target.gsub(/[^\w]/,'')}", output) if @printout && output && !output.empty?
93
def execute_eruby(filename, context)
95
#eval context2code(context)
96
list = context['list']
98
ERuby.import(filename)
103
def execute_erb(filename, context)
104
#eval context2code(context)
105
list = context['list']
108
eruby = ERB.new(File.read(filename))
109
output = eruby.result(binding())
115
def execute_erb_cached(filename, context)
116
#eval context2code(context)
117
list = context['list']
119
cachefile = filename + '.cache'
120
File.unlink(cachefile) if test(?f, cachefile)
122
if !test(?f, cachefile) || File.mtime(filename) > File.mtime(cachefile)
123
eruby = ERB.new(File.read(filename))
124
File.write(cachefile, eruby.src)
127
#eruby.src = File.read(cachefile)
128
eruby.instance_variable_set("@src", File.read(cachefile))
130
output = eruby.result(binding())
137
for klass in %w[Eruby FastEruby TinyEruby ArrayBufferEruby PrintOutEruby StdoutEruby] do
139
def execute_erubis_#{klass.downcase}(filename, context)
140
#eval context2code(context)
141
list = context['list']
144
eruby = Erubis::#{klass}.new(File.read(filename))
145
output = eruby.result(binding())
155
for klass in %w[Eruby FastEruby] do
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)
164
eruby = Erubis::#{klass}.load_file(filename)
165
output = eruby.result(binding())
168
savefile = cachefile.sub(/\\.cache$/, '.#{klass.downcase}.cache')
169
File.rename(cachefile, savefile)
178
def convert_eruby(filename, context)
180
#eval context2code(context)
181
list = context['list']
184
output = ERuby::Compiler.new.compile_string(File.read(filename))
189
def convert_erb(filename, context)
190
#eval context2code(context)
191
list = context['list']
194
eruby = ERB.new(File.read(filename))
200
for klass in %w[Eruby FastEruby TinyEruby]
202
def convert_erubis_#{klass.downcase}(filename, context)
203
#eval context2code(context)
204
list = context['list']
207
eruby = Erubis::#{klass}.new(File.read(filename))
221
class MainApplication
223
def parse_argv(argv=ARGV)
224
optparser = OptionParser.new
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 }
231
targets = optparser.parse!(argv)
233
$stderr.puts "#{@script}: #{ex.to_s}"
236
return options, targets
240
@script = File.basename($0)
242
targets = BenchmarkApplication::TARGETS.dup
244
contextfile = 'bench_context.yaml'
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']
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')"
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)
276
if options['e'] # escape
278
[ 'bench_eruby.rhtml', '<%= CGI.escapeHTML((\1).to_s) %>' ],
279
[ 'bench_erb.rhtml', '<%=h \1 %>' ],
280
[ 'bench_erubis.rhtml', '<%== \1 %>' ],
282
for fname, replace in tuples
283
content = File.read(fname).gsub(/<%= ?(.*?) ?%>/, replace)
284
File.write(fname, content)
286
targets.delete('Erubis::TinyEruby') ## because TinyEruby doesn't support '<%== =>'
289
context = YAML.load_file(contextfile)
292
:printout=>options['p'],
295
app = BenchmarkApplication.new(ntimes, context, targets, params)
296
app.perform_benchmark()
305
$stdout = File.open('/dev/null', 'w')
311
MainApplication.new().execute()