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
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(&block) ⇒ 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.
30 31 32 33 |
# File 'lib/vedeu/logging/timer.rb', line 30 def initialize( = '') @message = @started = Vedeu.clock_time end |
Instance Attribute Details
#message ⇒ String (readonly, protected)
66 67 68 |
# File 'lib/vedeu/logging/timer.rb', line 66 def @message end |
#started ⇒ Time (readonly, protected)
62 63 64 |
# File 'lib/vedeu/logging/timer.rb', line 62 def started @started end |
Class Method Details
.timer(message = '', &block) ⇒ void
This method returns an undefined value.
20 21 22 |
# File 'lib/vedeu/logging/timer.rb', line 20 def timer( = '', &block) new().measure(&block) end |
Instance Method Details
#elapsed ⇒ Float (protected)
Returns the elapsed time in milliseconds with 3 decimal places.
72 73 74 |
# File 'lib/vedeu/logging/timer.rb', line 72 def elapsed ((Vedeu.clock_time - started) * 1000).round(3) end |
#measure(&block) ⇒ 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.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vedeu/logging/timer.rb', line 41 def measure(&block) fail Vedeu::Error::RequiresBlock unless block_given? if Vedeu.config.debug? work = yield Vedeu.log(type: :timer, message: "#{} took #{elapsed}ms.") work else yield end end |