Class: Vedeu::Logging::Timer

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = '') ⇒ Vedeu::Logging::Timer

Returns a new instance of Vedeu::Logging::Timer.

Parameters:

  • message (String) (defaults to: '')


30
31
32
33
# File 'lib/vedeu/logging/timer.rb', line 30

def initialize(message = '')
  @message = message
  @started = Vedeu.clock_time
end

Instance Attribute Details

#messageString (readonly, protected)

Returns:

  • (String)


68
69
70
# File 'lib/vedeu/logging/timer.rb', line 68

def message
  @message
end

#startedTime (readonly, protected)

Returns:

  • (Time)


64
65
66
# File 'lib/vedeu/logging/timer.rb', line 64

def started
  @started
end

Class Method Details

.timer(message = '', &block) ⇒ void

This method returns an undefined value.

Parameters:

  • message (String) (defaults to: '')
  • block (Proc)

Raises:



20
21
22
# File 'lib/vedeu/logging/timer.rb', line 20

def timer(message = '', &block)
  new(message).measure(&block)
end

Instance Method Details

#elapsedFloat (private)

Returns the elapsed time in milliseconds with 3 decimal places.

Returns:

  • (Float)


76
77
78
# File 'lib/vedeu/logging/timer.rb', line 76

def elapsed
  ((Vedeu.clock_time - started) * 1000).round(3)
end

#measure(&block) ⇒ void|NilClass

Write an entry to the log file stating how long a section of code took in milliseconds when debugging is enabled and a block was Useful for debugging performance.

Parameters:

  • block (Proc)

Returns:

  • (void|NilClass)

    The return value of the executed block if given, or nil if debugging is disabled.

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/vedeu/logging/timer.rb', line 43

def measure(&block)
  raise Vedeu::Error::RequiresBlock unless block_given?

  if Vedeu.config.debug?
    work = yield

    Vedeu.log(type:    :timer,
              message: "#{message} took #{elapsed}ms.")

    work

  else
    yield

  end
end