Class: UltraMarathon::Instrumentation::Store

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_members = [], options = {}) ⇒ Store

Returns a new instance of Store.

Parameters:

  • new_members (Array, Set) (defaults to: [])
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :prefix (String) — default: ''

    will prefix the name of every name passed into #instrument



11
12
13
14
15
16
# File 'lib/ultra_marathon/instrumentation/store.rb', line 11

def initialize(new_members=[], options={})
  super(new_members)
  @options = {
    prefix: ''
  }.merge(options)
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/ultra_marathon/instrumentation/store.rb', line 5

def options
  @options
end

Instance Method Details

#[](name) ⇒ UltraMarathon::Instrumentation::Profile?

Access a profile by name. Instrumentations shouldn’t have to know about their fully qualified name, so the unprefixed version should be passed

Parameters:

Returns:



41
42
43
44
45
46
# File 'lib/ultra_marathon/instrumentation/store.rb', line 41

def [](name)
  full_name = full_name(name)
  detect do |profile|
    profile.name == full_name
  end
end

#instrument(name, &block) ⇒ Object

Instruments given block, setting its start time and end time Stores the resulting profile in in itself

Parameters:

  • name (String)

    name of the instrumented block

  • block (Proc)

    block to instrument

Returns:

  • (Object)

    return value of the instrumented block



23
24
25
26
27
28
# File 'lib/ultra_marathon/instrumentation/store.rb', line 23

def instrument(name, &block)
  profile = Profile.new(full_name(name), &block)
  return_value = profile.call
  self.add(profile)
  return_value
end

#mean_runtimeFloat

Returns the mean time for all profiles.

Returns:

  • (Float)

    the mean time for all profiles



55
56
57
# File 'lib/ultra_marathon/instrumentation/store.rb', line 55

def mean_runtime
  total_time / size
end

#medianUltraMarthon::Instrumentation::Profile

Returns the profile in the middle of the pack per UltraMarthon::Instrumentation::Profile#total_time.

Returns:

  • (UltraMarthon::Instrumentation::Profile)

    the profile in the middle of the pack per UltraMarthon::Instrumentation::Profile#total_time



62
63
64
# File 'lib/ultra_marathon/instrumentation/store.rb', line 62

def median
  to_a[size / 2]
end

#merge!(other_store) ⇒ self

Adds all profiles from the other_store

Parameters:

Returns:

  • (self)

    the other_store



79
80
81
82
83
84
# File 'lib/ultra_marathon/instrumentation/store.rb', line 79

def merge!(other_store)
  other_store.each do |member|
    add(member)
  end
  self
end

#prefixString

The passed in prefix

Returns:



32
33
34
# File 'lib/ultra_marathon/instrumentation/store.rb', line 32

def prefix
  options[:prefix]
end

#standard_deviationFloat

Please forgive me Mr. Brooks, I had to Google it

Returns:

  • (Float)

    the standard deviation from the mean



69
70
71
72
73
74
# File 'lib/ultra_marathon/instrumentation/store.rb', line 69

def standard_deviation
  sum_of_squares = total_times.reduce(0) do |sum, total_time|
    sum + (mean_runtime - total_time) ** 2
  end
  Math.sqrt(sum_of_squares / size)
end

#total_timeFloat

Accumulated total time for all stored profiles

Returns:

  • (Float)


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

def total_time
  total_times.reduce(0.0, :+)
end