~ubuntu-branches/ubuntu/trusty/ruby-benchmark-suite/trusty

« back to all changes in this revision

Viewing changes to .pc/0001-Hardwire-Debian-lib-path.patch/bin/benchmark

  • Committer: Package Import Robot
  • Author(s): Per Andersson
  • Date: 2013-06-09 17:03:30 UTC
  • Revision ID: package-import@ubuntu.com-20130609170330-uyp6jhiyj2tixvbg
Tags: 1.0.0+git.20130122.5bded6-1
Initial release (Closes: #698729)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/env ruby
 
2
 
 
3
require 'optparse'
 
4
require 'tempfile'
 
5
 
 
6
lib_path = File.expand_path("../../lib", __FILE__)
 
7
 
 
8
$:.unshift lib_path
 
9
 
 
10
require 'benchmark/suite'
 
11
require 'benchmark/ips'
 
12
 
 
13
targets = []
 
14
extra = []
 
15
at_end = false
 
16
save = false
 
17
 
 
18
compare_targets = []
 
19
 
 
20
opt = OptionParser.new do |o|
 
21
  o.on("-t", "--target TARGET", String,
 
22
          "Use TARGET to compare against: r:ruby|r19:ruby19|x:rbx|j:jruby") do |t|
 
23
    case t
 
24
    when 'r', 'ruby'
 
25
      targets << 'ruby'
 
26
    when 'r19', 'ruby19'
 
27
      targets << 'ruby19'
 
28
    when 'x', 'rbx', 'rubinius'
 
29
      targets << 'rbx'
 
30
    when 'j', 'jruby'
 
31
      targets << 'jruby'
 
32
    else
 
33
      # + disambiguates execution vs using a file
 
34
      if t[0,1] == "+"
 
35
        targets << t[1..-1]
 
36
      elsif File.exists?(t)
 
37
        begin
 
38
          data = Marshal.load(File.read(t))
 
39
          compare_targets << [t, data]
 
40
        rescue TypeError
 
41
          targets << t
 
42
        end
 
43
      else
 
44
        targets << t
 
45
      end
 
46
    end
 
47
  end
 
48
 
 
49
  o.on("-p", "--profile", "Profile code while benchmarking (rbx only)") do
 
50
    extra << "-Xprofile"
 
51
  end
 
52
 
 
53
  o.on("-e", "--end", "Report all stats after all suitse have run") do
 
54
    at_end = true
 
55
  end
 
56
 
 
57
  o.on("-T OPTION", String, "Add more arguments to each target") do |t|
 
58
    extra << t
 
59
  end
 
60
 
 
61
  o.on("-s", "--save PATH", String, "Save the results to a file") do |t|
 
62
    save = t
 
63
  end
 
64
end
 
65
 
 
66
opt.parse!
 
67
 
 
68
if targets.empty?
 
69
  targets << "ruby"
 
70
end
 
71
 
 
72
if save and targets.size > 1
 
73
  STDOUT.puts "Save mode only available with one target."
 
74
  exit 1
 
75
end
 
76
 
 
77
opts = []
 
78
 
 
79
if at_end
 
80
  opts << "--quiet"
 
81
end
 
82
 
 
83
results = targets.map do |t|
 
84
  tf = Tempfile.new "benchmark"
 
85
  tf.close
 
86
  STDOUT.puts "=== #{t} ===" unless at_end
 
87
 
 
88
  args = extra + ["-I#{lib_path}", "#{lib_path}/benchmark/suite-run.rb"]
 
89
 
 
90
  args += opts
 
91
  args << tf.path
 
92
  args += ARGV
 
93
 
 
94
  cmd, *rest = t.split(/\s+/)
 
95
  args.unshift *rest
 
96
 
 
97
  system cmd, *args
 
98
 
 
99
  if $?.exitstatus != 0
 
100
    puts "Error executing: #{cmd}"
 
101
    nil
 
102
  else
 
103
    tf.open
 
104
    [t, Marshal.load(tf.read)]
 
105
  end
 
106
end
 
107
 
 
108
results.compact!
 
109
 
 
110
if save
 
111
  name, data = results.last
 
112
 
 
113
  File.open save, "w" do |f|
 
114
    f << Marshal.dump(data)
 
115
  end
 
116
 
 
117
  STDOUT.puts "[Saved results to '#{save}']"
 
118
end
 
119
 
 
120
if at_end
 
121
  results.each do |name, suite|
 
122
    STDOUT.puts "=== #{name} ==="
 
123
    suite.display
 
124
  end
 
125
end
 
126
 
 
127
results += compare_targets
 
128
 
 
129
if results.size > 1
 
130
  compared = Hash.new { |h,k| h[k] = [] }
 
131
 
 
132
  results.each do |target, suite|
 
133
    suite.reports.each do |name, reports|
 
134
      reports.each do |rep|
 
135
        compared["#{name}:#{rep.label}"] << [target, rep]
 
136
      end
 
137
    end
 
138
  end
 
139
 
 
140
  STDOUT.puts
 
141
 
 
142
  compared.each do |name, reports|
 
143
    if reports.size > 1
 
144
      STDOUT.puts "Comparing #{name}:"
 
145
 
 
146
      iter = false
 
147
      sorted = reports.sort do |a,b|
 
148
        if a[1].respond_to? :ips
 
149
          iter = true
 
150
          b[1].ips <=> a[1].ips
 
151
        else
 
152
          a[1].runtime <=> b[1].runtime
 
153
        end
 
154
      end
 
155
 
 
156
      best_name, best_report = sorted.shift
 
157
 
 
158
 
 
159
      if iter
 
160
        STDOUT.printf "%20s: %10d i/s\n", best_name, best_report.ips
 
161
      else
 
162
        STDOUT.puts "#{best_name.rjust(20)}: #{best_report.runtime}s"
 
163
      end
 
164
 
 
165
      sorted.each do |entry|
 
166
        name, report = entry
 
167
        if iter
 
168
          x = (best_report.ips.to_f / report.ips.to_f)
 
169
          STDOUT.printf "%20s: %10d i/s - %.2fx slower\n", name, report.ips, x
 
170
        else
 
171
          x = "%.2f" % (report.ips.to_f / best_report.ips.to_f)
 
172
          STDOUT.puts "#{name.rjust(20)}: #{report.runtime}s - #{x}x slower"
 
173
        end
 
174
      end
 
175
    end
 
176
  end
 
177
end