Class: Reviewer::Shell::Timer

Inherits:
Object
  • Object
show all
Defined in:
lib/reviewer/shell/timer.rb

Overview

Provides a structured interface for measuring realtime main while running comamnds

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(prep: nil, main: nil) ⇒ self

A ‘Smart’ timer that understands preparation time and main time and can easily do the math

to help determine what percentage of time was prep. The times can be passed in directly or
recorded using the `record_prep` and `record_main` methods

Parameters:

  • prep: (defaults to: nil)

    nil [Float] the amount of time in seconds the preparation command ran

  • main: (defaults to: nil)

    nil [Float] the amount of time in seconds the primary command ran



18
19
20
21
# File 'lib/reviewer/shell/timer.rb', line 18

def initialize(prep: nil, main: nil)
  @prep = prep
  @main = main
end

Instance Attribute Details

#mainObject

Returns the value of attribute main.



9
10
11
# File 'lib/reviewer/shell/timer.rb', line 9

def main
  @main
end

#prepObject

Returns the value of attribute prep.



9
10
11
# File 'lib/reviewer/shell/timer.rb', line 9

def prep
  @prep
end

Instance Method Details

#main_secondsObject



43
44
45
# File 'lib/reviewer/shell/timer.rb', line 43

def main_seconds
  main.round(2)
end

#prep_percentObject



51
52
53
54
55
# File 'lib/reviewer/shell/timer.rb', line 51

def prep_percent
  return nil unless prepped?

  (prep / total.to_f * 100).round
end

#prep_secondsObject



39
40
41
# File 'lib/reviewer/shell/timer.rb', line 39

def prep_seconds
  prep.round(2)
end

#prepped?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/reviewer/shell/timer.rb', line 61

def prepped?
  !(prep.nil? || main.nil?)
end

#record_main(&block) ⇒ Float

Records the execution time for the block and assigns it to the ‘main` time

Parameters:

  • &block (Block)

    the commands to be timed

Returns:

  • (Float)

    the execution time for the main command



35
36
37
# File 'lib/reviewer/shell/timer.rb', line 35

def record_main(&block)
  @main = record(&block)
end

#record_prep(&block) ⇒ Float

Records the execution time for the block and assigns it to the ‘prep` time

Parameters:

  • &block (Block)

    the commands to be timed

Returns:

  • (Float)

    the execution time for the preparation



27
28
29
# File 'lib/reviewer/shell/timer.rb', line 27

def record_prep(&block)
  @prep = record(&block)
end

#totalObject



57
58
59
# File 'lib/reviewer/shell/timer.rb', line 57

def total
  [prep, main].compact.sum
end

#total_secondsObject



47
48
49
# File 'lib/reviewer/shell/timer.rb', line 47

def total_seconds
  total.round(2)
end