Class: Vedeu::Logging::Timer
- Inherits:
-
Object
- Object
- Vedeu::Logging::Timer
- Defined in:
- lib/vedeu/logging/timer.rb
Overview
Measure the duration. Used for debugging. The configuration option ‘debug’ must be set to true to enable this functionality.
Instance Attribute Summary collapse
- #message ⇒ String readonly protected
- #started ⇒ Time readonly protected
Class Method Summary collapse
-
.timer(message = '', &block) ⇒ void
Measure the execution time of the code in the given block.
Instance Method Summary collapse
-
#elapsed ⇒ Float
protected
Returns the elapsed time in milliseconds with 3 decimal places.
-
#initialize(message = '') ⇒ Vedeu::Logging::Timer
constructor
Returns a new instance of Vedeu::Logging::Timer.
-
#measure ⇒ void
Write an entry to the log file stating how long a section of code took in milliseconds.
Constructor Details
#initialize(message = '') ⇒ Vedeu::Logging::Timer
Returns a new instance of Vedeu::Logging::Timer.
36 37 38 39 |
# File 'lib/vedeu/logging/timer.rb', line 36 def initialize( = '') @message = @started = Time.now.to_f end |
Instance Attribute Details
#message ⇒ String (readonly, protected)
71 72 73 |
# File 'lib/vedeu/logging/timer.rb', line 71 def @message end |
#started ⇒ Time (readonly, protected)
67 68 69 |
# File 'lib/vedeu/logging/timer.rb', line 67 def started @started end |
Class Method Details
.timer(message = '', &block) ⇒ void
This method returns an undefined value.
Measure the execution time of the code in the given block. The message provided will have ‘ took <time>ms.’ appended.
26 27 28 |
# File 'lib/vedeu/logging/timer.rb', line 26 def timer( = '', &block) new().measure(&block) end |
Instance Method Details
#elapsed ⇒ Float (protected)
Returns the elapsed time in milliseconds with 3 decimal places.
77 78 79 |
# File 'lib/vedeu/logging/timer.rb', line 77 def elapsed ((Time.now.to_f - started) * 1000).round(3) end |
#measure ⇒ void
This method returns an undefined value.
Write an entry to the log file stating how long a section of code took in milliseconds. Useful for debugging performance.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/vedeu/logging/timer.rb', line 46 def measure fail Vedeu::Error::RequiresBlock unless block_given? if Vedeu::Configuration.debug? work = yield Vedeu.log(type: :timer, message: "#{} took #{elapsed}ms.".freeze) work else yield end end |