Class: UltraMarathon::Instrumentation::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/ultra_marathon/instrumentation/profile.rb

Constant Summary collapse

DATETIME_FORMAT =
'%H:%M:%S:%L'.freeze
RAW_TIME_FORMAT =
'%02d:%02d:%02d:%03d'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Profile

Returns a new instance of Profile.

Parameters:

  • name (String)

    name of the instrumented block

  • block (Proc)

    block to be instrumented



13
14
15
16
17
18
19
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 13

def initialize(name, &block)
  @name = name
  # Ruby cannot marshal procs or lambdas, so we need to define a method.
  define_singleton_method :instrumented_block do
    block.call
  end
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



7
8
9
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 7

def end_time
  @end_time
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 7

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



7
8
9
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 7

def start_time
  @start_time
end

Instance Method Details

#<=>(other_profile) ⇒ Integer

Comparison delegated to #total_time

Parameters:

Returns:

  • (Integer)

    #total_time <=> other_profile.total_time



56
57
58
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 56

def <=>(other_profile)
  total_time <=> other_profile.total_time
end

#callObject

Sets #start_time, runs the initialized block, then sets #end_time

Returns:

  • (Object)

    the return value of the initialized block



23
24
25
26
27
28
29
30
31
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 23

def call
  @start_time = Time.now
  begin
    return_value = instrumented_block
  ensure
    @end_time = Time.now
  end
  return_value
end

#eql?(other_profile) ⇒ Boolean

Profiles are considered equal if their names are ‘eql?`

Parameters:

Returns:

  • (Boolean)

    delegates to #name



63
64
65
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 63

def eql?(other_profile)
  name.eql? other_profile.name
end

#formatted_end_timeString

Returns #end_time formatted per DATETIME_FORMAT.

Returns:



49
50
51
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 49

def formatted_end_time
  format_time(end_time)
end

#formatted_start_timeString

Returns #start_time formatted per DATETIME_FORMAT.

Returns:



44
45
46
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 44

def formatted_start_time
  format_time(start_time)
end

#formatted_total_timeString

Returns #total_time formatted per RAW_TIME_FORMAT.

Returns:



39
40
41
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 39

def formatted_total_time
  format_seconds(total_time)
end

#total_timeFloat

Returns the total time in seconds to the nanosecond.

Returns:

  • (Float)

    the total time in seconds to the nanosecond



34
35
36
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 34

def total_time
  @total_time ||= end_time - start_time
end