/usr/lib/ruby/vendor_ruby/benchmark/ips.rb is in ruby-benchmark-ips 1.2.0+git.20130609.e47e416-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 | # encoding: utf-8
require 'benchmark/timing'
require 'benchmark/compare'
module Benchmark
class IPSReport
VERSION = "1.1.0"
def initialize(label, us, iters, ips, ips_sd, cycles)
@label = label
@microseconds = us
@iterations = iters
@ips = ips
@ips_sd = ips_sd
@measurement_cycle = cycles
end
attr_reader :label, :microseconds, :iterations, :ips, :ips_sd, :measurement_cycle
def seconds
@microseconds.to_f / 1_000_000.0
end
def stddev_percentage
100.0 * (@ips_sd.to_f / @ips.to_f)
end
alias_method :runtime, :seconds
def body
left = "%10.1f (±%.1f%%) i/s" % [ips, stddev_percentage]
left.ljust(20) + (" - %10d in %10.6fs" % [@iterations, runtime])
end
def header
@label.rjust(20)
end
def to_s
"#{header} #{body}"
end
def display
$stdout.puts to_s
end
end
class IPSJob
class Entry
def initialize(label, action)
@label = label
if action.kind_of? String
compile action
@action = self
@as_action = true
else
unless action.respond_to? :call
raise ArgumentError, "invalid action, must respond to #call"
end
@action = action
if action.respond_to? :arity and action.arity > 0
@call_loop = true
else
@call_loop = false
end
@as_action = false
end
end
attr_reader :label, :action
def as_action?
@as_action
end
def call_times(times)
return @action.call(times) if @call_loop
act = @action
i = 0
while i < times
act.call
i += 1
end
end
def compile(str)
m = (class << self; self; end)
code = <<-CODE
def call_times(__total);
__i = 0
while __i < __total
#{str};
__i += 1
end
end
CODE
m.class_eval code
end
end
def initialize
@list = []
@compare = false
end
attr_reader :compare
def compare!
@compare = true
end
#
# Registers the given label and block pair in the job list.
#
def item(label="", str=nil, &blk) # :yield:
if blk and str
raise ArgumentError, "specify a block and a str, but not both"
end
action = str || blk
raise ArgumentError, "no block or string" unless action
@list.push Entry.new(label, action)
self
end
alias_method :report, :item
# An array of 2-element arrays, consisting of label and block pairs.
attr_reader :list
end
def ips(time=5, warmup=2)
suite = nil
sync, $stdout.sync = $stdout.sync, true
if defined? Benchmark::Suite and Suite.current
suite = Benchmark::Suite.current
end
quiet = suite && !suite.quiet?
job = IPSJob.new
yield job
reports = []
timing = {}
$stdout.puts "Calculating -------------------------------------" unless quiet
job.list.each do |item|
suite.warming item.label, warmup if suite
Timing.clean_env
unless quiet
if item.label.size > 20
$stdout.print "#{item.label}\n#{' ' * 20}"
else
$stdout.print item.label.rjust(20)
end
end
before = Time.now
target = Time.now + warmup
warmup_iter = 0
while Time.now < target
item.call_times(1)
warmup_iter += 1
end
after = Time.now
warmup_time = (after.to_f - before.to_f) * 1_000_000.0
# calculate the time to run approx 100ms
cycles_per_100ms = ((100_000 / warmup_time) * warmup_iter).to_i
cycles_per_100ms = 1 if cycles_per_100ms <= 0
timing[item] = cycles_per_100ms
$stdout.printf "%10d i/100ms\n", cycles_per_100ms unless quiet
suite.warmup_stats warmup_time, cycles_per_100ms if suite
end
$stdout.puts "-------------------------------------------------" unless quiet
job.list.each do |item|
unless quiet
if item.label.size > 20
$stdout.print "#{item.label}\n#{' ' * 20}"
else
$stdout.print item.label.rjust(20)
end
end
Timing.clean_env
suite.running item.label, time if suite
iter = 0
target = Time.now + time
measurements = []
cycles_per_100ms = timing[item]
while Time.now < target
before = Time.now
item.call_times cycles_per_100ms
after = Time.now
# If for some reason the timing said this too no time (O_o)
# then ignore the iteration entirely and start another.
#
m = ((after.to_f - before.to_f) * 1_000_000.0)
next if m <= 0.0
iter += cycles_per_100ms
measurements << m
end
measured_us = measurements.inject(0) { |a,i| a + i }
all_ips = measurements.map { |i| cycles_per_100ms.to_f / (i.to_f / 1_000_000) }
avg_ips = Timing.mean(all_ips)
sd_ips = Timing.stddev(all_ips).round
rep = IPSReport.new(item.label, measured_us, iter, avg_ips, sd_ips, cycles_per_100ms)
$stdout.puts " #{rep.body}" unless quiet
suite.add_report rep, caller(1).first if suite
reports << rep
end
$stdout.sync = sync
if job.compare
Benchmark.compare(*reports)
end
return reports
end
module_function :ips
end
|