lib/stud/benchmark.rb in stud-0.0.10 vs lib/stud/benchmark.rb in stud-0.0.11
- old
+ new
@@ -8,32 +8,41 @@
module Stud
module Benchmark
def self.run(iterations=1, &block)
timer = Metriks::Timer.new
+ start = Time.now
iterations.times { timer.time(&block) }
- return Results.new(timer)
+ duration = Time.now - start
+ return Results.new(timer, duration)
end # def run
def self.runtimed(seconds=10, &block)
timer = Metriks::Timer.new
expiration = Time.now + seconds
+
+ start = Time.now
timer.time(&block) while Time.now < expiration
- return Results.new(timer)
+ duration = Time.now - start
+ return Results.new(timer, duration)
end # def runtimed
def self.cputimed(seconds=10, &block)
timer = Metriks::Timer.new
expiration = Time.now + seconds
+ start_usage = Stud::Benchmark::RUsage.get
while Time.now < expiration
start = Stud::Benchmark::RUsage.get
block.call
finish = Stud::Benchmark::RUsage.get
cputime = (finish.user + finish.system) - (start.user + start.system)
timer.update(cputime)
end # while not expired
- return Results.new(timer)
+ finish_usage = Stud::Benchmark::RUsage.get
+ duration = (finish_usage.user + finish_usage.system) \
+ - (start_usage.user + start_usage.system)
+ return Results.new(timer, duration)
end # self.cpu
class Results
include Enumerable
# Stolen from https://github.com/holman/spark/blob/master/spark
@@ -51,12 +60,13 @@
#"\x1b[38;5;#{232 + 12 + 2 * shade}m#{tick}\x1b[0m"
#end
#tick
#end.flatten
- def initialize(data)
+ def initialize(data, duration)
@data = data
+ @duration = duration
end # def initialize
def environment
# Older rubies don't have the RUBY_ENGINE defiend
engine = (RUBY_ENGINE rescue "ruby")
@@ -75,9 +85,13 @@
return @data.min
end
def max
return @data.max
+ end
+
+ def rate
+ return @data.count / @duration
end
def mean
return @data.mean
end # def mean