summaryrefslogtreecommitdiff
path: root/benchmark/run.rb
blob: b84d0938280444abd8500d204d39280792753555 (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
#
# YARV benchmark driver
#

require 'yarvutil'
require 'benchmark'
require 'rbconfig'

$yarvonly = false
$rubyonly = false

$results  = []

puts "ruby #{RUBY_VERSION} #{RUBY_PLATFORM}(#{RUBY_RELEASE_DATE})"
puts YARVCore::VERSION + " rev: #{YARVCore::REV} (#{YARVCore::DATE})"
puts YARVCore::OPTS
puts

def bm file
  prog = File.read(file).map{|e| e.rstrip}.join("\n")
  return if prog.empty?

  /[a-z]+_(.+)\.rb/ =~ file
  bm_name = $1
  puts '-----------------------------------------------------------' unless $yarvonly || $rubyonly
  puts "#{bm_name}: "
  
  
puts <<EOS unless $yarvonly || $rubyonly
#{prog}
--
EOS
  #iseq = YARVUtil.parse(File.read(file))
  #vm   = YARVCore::VM.new
  begin
    Benchmark.bm{|x|
    # x.report("yarv"){ YARVUtil.load_bm(file) }
    } unless $yarvonly || $rubyonly

    result = [bm_name]
    result << ruby_exec(file) unless $yarvonly
    result << yarv_exec(file) unless $rubyonly
    $results << result
    
    # puts YARVUtil.parse(File.read(file), file, 1).disasm
    
    # x.report("ruby"){ load(file, false)    }
    # x.report("yarv"){ vm.eval iseq }
  rescue Exception => e
    puts
    puts "** benchmark failure: #{e}"
    puts e.backtrace
  end
end

def exec_command type, file, w
  <<-EOP
  $DRIVER_PATH = '#{File.dirname($0)}'
  $LOAD_PATH.replace $LOAD_PATH | #{$LOAD_PATH.inspect}
  require 'benchmark'
  require 'yarvutil'
  print '#{type}'
  begin
    puts Benchmark.measure{
      #{w}('#{file}')
    }
  rescue Exception => exec_command_error_variable
    puts "\t" + exec_command_error_variable.message
  end
  EOP
end

def benchmark prog
  rubybin = ENV['RUBY'] || File.join(
    Config::CONFIG["bindir"],
    Config::CONFIG["ruby_install_name"] + Config::CONFIG["EXEEXT"])

  #
  tmpfile = Tempfile.new('yarvbench')
  tmpfile.write(prog)
  tmpfile.close

  cmd = "#{rubybin} #{tmpfile.path}"
  result = `#{cmd}`
  puts result
  tmpfile.close(true)
  result
end

def ruby_exec file
  prog = exec_command 'ruby', file, 'load'
  benchmark prog
end

def yarv_exec file
  prog = exec_command 'yarv', file, 'YARVUtil.load_bm'
  benchmark prog
end

if $0 == __FILE__
  ARGV.each{|arg|
    if /\A(--yarv)|(-y)/ =~ arg
      $yarvonly = true
    elsif /\A(--ruby)|(-r)/ =~ arg
      $rubyonly = true
    end
  }
  ARGV.delete_if{|arg|
    /\A-/ =~ arg
  }
  
  if ARGV.empty?
    Dir.glob(File.dirname(__FILE__) + '/bm_*.rb').sort.each{|file|
      bm file
    }
  else
    ARGV.each{|file|
      Dir.glob(File.join(File.dirname(__FILE__), file + '*')){|ef|
        # file = "#{File.dirname(__FILE__)}/#{file}.rb"
        bm ef
      }
    }
  end

  puts
  puts "-- benchmark summary ---------------------------"
  $results.each{|res|
    print res.shift, "\t"
    (res||[]).each{|result|
      /([\d\.]+)/ =~ result
      print $1 + "\t" if $1
    }
    puts
  }
end