Class: BenchPress::Runnable

Inherits:
Object
  • Object
show all
Defined in:
lib/bench_press/runnable.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, block) ⇒ Runnable

Returns a new instance of Runnable.



16
17
18
19
# File 'lib/bench_press/runnable.rb', line 16

def initialize(name, block)
  @name = name
  @code_block = block
end

Instance Attribute Details

#code_blockObject (readonly)

Returns the value of attribute code_block.



3
4
5
# File 'lib/bench_press/runnable.rb', line 3

def code_block
  @code_block
end

#fastestObject

Returns the value of attribute fastest.



4
5
6
# File 'lib/bench_press/runnable.rb', line 4

def fastest
  @fastest
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/bench_press/runnable.rb', line 3

def name
  @name
end

#percent_slowerObject

Returns the value of attribute percent_slower.



4
5
6
# File 'lib/bench_press/runnable.rb', line 4

def percent_slower
  @percent_slower
end

#run_timeObject (readonly)

Returns the value of attribute run_time.



3
4
5
# File 'lib/bench_press/runnable.rb', line 3

def run_time
  @run_time
end

Class Method Details

.repetitionsObject



7
8
9
# File 'lib/bench_press/runnable.rb', line 7

def repetitions
  @repetitions ||= 1000
end

.repetitions=(times) ⇒ Object



11
12
13
# File 'lib/bench_press/runnable.rb', line 11

def repetitions=(times)
  @repetitions = times
end

Instance Method Details

#fastest?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/bench_press/runnable.rb', line 21

def fastest?
  fastest == true
end

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/bench_press/runnable.rb', line 25

def run
  r,w = IO.pipe
  fork do
    time = Benchmark.realtime do
      self.class.repetitions.times do |i|
        code_block.call(i)
      end
    end
    w.write time
    w.close_write
    exit!
  end
  Process.waitall
  w.close_write
  @run_time = r.read.to_f
end

#summaryObject



42
43
44
45
46
47
48
# File 'lib/bench_press/runnable.rb', line 42

def summary
  if fastest?
    "Fastest"
  else
    "#{percent_slower}% Slower"
  end
end

#to_hashObject



50
51
52
53
54
55
56
57
# File 'lib/bench_press/runnable.rb', line 50

def to_hash
  {
    :name => name,
    :run_time => run_time,
    :summary => summary,
    :fastest => fastest
  }
end