2 # Ruby Benchmark driver
14 $:.unshift File.join(File.dirname(__FILE__), '../lib')
25 def self.benchmark(opt)
26 driver = self.new(opt[:execs], opt[:dir], opt)
36 @output and @output.puts(*args)
40 output(*args) if @verbose
43 def message_print *args
47 @output and @output.print(*args)
51 def progress_message *args
58 def initialize execs, dir, opt = {}
59 @execs = execs.map{|e|
64 # ex) ruby-a::/path/to/ruby-a
69 v.sub!(/ patchlevel \d+/, '')
75 @repeat = opt[:repeat] || 1
76 @repeat = 1 if @repeat < 1
77 @pattern = opt[:pattern] || nil
78 @verbose = opt[:quiet] ? false : (opt[:verbose] || false)
79 @output = opt[:output] ? open(opt[:output], 'w') : nil
80 @loop_wl1 = @loop_wl2 = nil
83 # [[name, [[r-1-1, r-1-2, ...], [r-2-1, r-2-2, ...]]], ...]
87 @start_time = Time.now
89 @execs.each_with_index{|(e, v), i|
90 message "target #{i}: #{v}"
99 message '-----------------------------------------------------------'
102 message PP.pp(@results, "", 79)
104 message "Elapesed time: #{Time.now - @start_time} (sec)"
107 output '-----------------------------------------------------------'
108 output 'benchmark results:'
110 if @verbose and @repeat > 1
111 output "minimum results in each #{@repeat} measurements."
114 output "name\t#{@execs.map{|(e, v)| v}.join("\t")}"
115 @results.each{|v, result|
118 result.each_with_index{|e, i|
132 rets << sprintf("%.3f", r)
134 output "#{v}#{s}\t#{rets.join("\t")}"
140 vm1 = vm2 = wl1 = wl2 = false
141 @files = Dir.glob(File.join(@dir, 'bm*.rb')).map{|file|
142 next if @pattern && /#{@pattern}/ !~ File.basename(file)
144 when /bm_(vm[12])_/, /bm_loop_(whileloop2?).rb/
150 if flag['vm1'] && !flag['whileloop']
151 @files << File.join(@dir, 'bm_loop_whileloop.rb')
152 elsif flag['vm2'] && !flag['whileloop2']
153 @files << File.join(@dir, 'bm_loop_whileloop2.rb')
157 progress_message "total: #{@files.size * @repeat} trial(s) (#{@repeat} trial(s) for #{@files.size} benchmark(s))\n"
162 files.each_with_index{|file, i|
164 r = measure_file(file)
166 if /bm_loop_whileloop.rb/ =~ file
167 @loop_wl1 = r[1].map{|e| e.min}
168 elsif /bm_loop_whileloop2.rb/ =~ file
169 @loop_wl2 = r[1].map{|e| e.min}
174 def measure_file file
175 name = File.basename(file, '.rb').sub(/^bm_/, '')
176 prepare_file = File.join(File.dirname(file), "prepare_#{name}.rb")
177 load prepare_file if FileTest.exist?(prepare_file)
181 output '-----------------------------------------------------------'
184 output File.read(file)
189 result << @execs.map{|(e, v)|
191 message_print "#{v}\t"
203 def measure executable, file
204 cmd = "#{executable} #{file}"
205 m = Benchmark.measure{
210 raise "Benchmark process exited with abnormal status (#{$?})"
222 :output => "bmlog-#{Time.now.strftime('%Y%m%d-%H%M%S')}.#{$$}",
225 parser = OptionParser.new{|o|
226 o.on('-e', '--executables [EXECS]',
227 "Specify benchmark one or more targets. (exec1; exec2; exec3, ...)"){|e|
228 opt[:execs] = e.split(/;/)
230 o.on('-d', '--directory [DIRECTORY]', "Benchmark suites directory"){|d|
233 o.on('-p', '--pattern [PATTERN]', "Benchmark name pattern"){|p|
236 o.on('-r', '--repeat-count [NUM]', "Repeat count"){|n|
237 opt[:repeat] = n.to_i
239 o.on('-o', '--output-file [FILE]', "Output file"){|o|
242 o.on('-q', '--quiet', "Run without notify information except result table."){|q|
245 o.on('-v', '--verbose'){|v|
251 BenchmarkDriver.benchmark(opt)