Class: UltraMarathon::Instrumentation::Profile

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Profile

Public Instance Methods



8
9
10
11
12
13
14
15
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 8

def initialize(name, &block)
  @name = name
  # Ruby cannot marshal procs or lambdas, so we need to define a method.
  # Binding to self allows us to intercept logging calls.
  define_singleton_method :instrumentated_block do
    block.call
  end
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



4
5
6
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 4

def end_time
  @end_time
end

#instrument_blockObject (readonly)

Returns the value of attribute instrument_block.



4
5
6
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 4

def instrument_block
  @instrument_block
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 4

def name
  @name
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



4
5
6
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 4

def start_time
  @start_time
end

Instance Method Details

#callObject



17
18
19
20
21
22
23
24
25
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 17

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

#format_time(time) ⇒ Object

Private Instance Methods



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

def format_time(time)
  sprintf(TIME_FORMAT, time.hour, time.min, time.sec)
end

#formatted_end_timeObject



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

def formatted_end_time
  format_time(end_time)
end

#formatted_start_timeObject



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

def formatted_start_time
  format_time(start_time)
end

#formatted_total_timeObject



32
33
34
35
36
37
38
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 32

def formatted_total_time
  duration = total_time
  seconds = (duration % 60).floor
  minutes = (duration / 60).floor
  hours   = (duration / 3600).floor
  sprintf(TIME_FORMAT, hours, minutes, seconds)
end

#total_timeObject

returns the total time, in seconds



28
29
30
# File 'lib/ultra_marathon/instrumentation/profile.rb', line 28

def total_time
  (end_time - start_time).to_i
end