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) ⇒ Timer

A timer that tracks preparation and main execution times separately. Times can be passed directly or recorded using record_prep and record_main.

Parameters:

  • prep (Float, nil) (defaults to: nil)

    the preparation time in seconds

  • main (Float, nil) (defaults to: nil)

    the main execution time in seconds



21
22
23
24
# File 'lib/reviewer/shell/timer.rb', line 21

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

Instance Attribute Details

#mainObject

Returns the value of attribute main.



13
# File 'lib/reviewer/shell/timer.rb', line 13

attr_reader :prep, :main

#prepFloat?

Returns the preparation time in seconds.

Returns:

  • (Float, nil)

    the preparation time in seconds



13
14
15
# File 'lib/reviewer/shell/timer.rb', line 13

def prep
  @prep
end

Instance Method Details

#main_secondsFloat

The main execution time rounded to two decimal places

Returns:

  • (Float)

    main time in seconds



46
# File 'lib/reviewer/shell/timer.rb', line 46

def main_seconds = main.round(2)

#prep_percentInteger?

The percentage of total time spent on preparation

Returns:

  • (Integer, nil)

    percentage (0-100) or nil if not prepped



66
67
68
69
70
# File 'lib/reviewer/shell/timer.rb', line 66

def prep_percent
  return nil unless prepped?

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

#prep_secondsFloat

The preparation time rounded to two decimal places

Returns:

  • (Float)

    prep time in seconds



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

def prep_seconds = prep.round(2)

#prepped?Boolean

Whether both prep and main times have been recorded

Returns:

  • (Boolean)

    true if both phases were timed



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

def prepped? = [prep, main].all?

#record_mainFloat

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



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

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

#record_prepFloat

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



30
# File 'lib/reviewer/shell/timer.rb', line 30

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

#totalFloat

The total time (prep + main) without rounding

Returns:

  • (Float)

    total time in seconds



56
# File 'lib/reviewer/shell/timer.rb', line 56

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

#total_secondsFloat

The total execution time (prep + main) rounded to two decimal places

Returns:

  • (Float)

    total time in seconds



51
# File 'lib/reviewer/shell/timer.rb', line 51

def total_seconds = total.round(2)