Module: NewRelic::Stats

Included in:
StatsBase
Defined in:
lib/new_relic/stats.rb

Instance Method Summary collapse

Instance Method Details

#absent?Boolean

a stat is absent if its call count equals zero

Returns:

  • (Boolean)


6
7
8
# File 'lib/new_relic/stats.rb', line 6

def absent?
  call_count == 0
end

#apdex_scoreObject



196
197
198
199
# File 'lib/new_relic/stats.rb', line 196

def apdex_score
  s, t, f = get_apdex
  (s.to_f + (t.to_f / 2)) / (s+t+f).to_f
end

#as_percentageObject

the stat total_call_time is a percent



128
129
130
# File 'lib/new_relic/stats.rb', line 128

def as_percentage
  average_call_time * 100.0
end

#as_percentage_of(other_stats) ⇒ Object



123
124
125
# File 'lib/new_relic/stats.rb', line 123

def as_percentage_of(other_stats)
  checked_calculation(total_call_time, other_stats.total_call_time) * 100.0
end

#average_call_timeObject Also known as: average_value, average_response_time



31
32
33
# File 'lib/new_relic/stats.rb', line 31

def average_call_time
  checked_calculation(total_call_time, call_count)
end

#average_exclusive_timeObject



34
35
36
# File 'lib/new_relic/stats.rb', line 34

def average_exclusive_time
  checked_calculation(total_exclusive_time, call_count)
end

#calls_per_minuteObject Also known as: requests_per_minute



139
140
141
# File 'lib/new_relic/stats.rb', line 139

def calls_per_minute
  checked_calculation(call_count, duration) * 60
end

#checked_calculation(numerator, denominator) ⇒ Object

makes sure we aren’t dividing by zero



23
24
25
26
27
28
29
# File 'lib/new_relic/stats.rb', line 23

def checked_calculation(numerator, denominator)
  if denominator.nil? || denominator == 0
    0.0
  else
    numerator.to_f / denominator
  end
end

#durationObject



132
133
134
# File 'lib/new_relic/stats.rb', line 132

def duration
  end_time ? (end_time - begin_time) : 0.0
end

#exclusive_time_percentageObject



164
165
166
# File 'lib/new_relic/stats.rb', line 164

def exclusive_time_percentage
  checked_calculation(total_exclusive_time, duration)
end

#expand_min_max_to(other) ⇒ Object



82
83
84
85
# File 'lib/new_relic/stats.rb', line 82

def expand_min_max_to(other)
    self.min_call_time = other.min_call_time if min_time_less?(other)
    self.max_call_time = other.max_call_time if other.max_call_time > max_call_time
end

#get_apdexObject

returns s,t,f



192
193
194
# File 'lib/new_relic/stats.rb', line 192

def get_apdex
  [@call_count, @total_call_time.to_i, @total_exclusive_time.to_i]
end

#is_reset?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/new_relic/stats.rb', line 108

def is_reset?
  call_count == 0 && total_call_time == 0.0 && total_exclusive_time == 0.0
end

#merge(other_stats) ⇒ Object



102
103
104
105
# File 'lib/new_relic/stats.rb', line 102

def merge(other_stats)
  stats = self.clone
  stats.merge!(other_stats)
end

#merge!(other_stats) ⇒ Object



94
95
96
97
98
99
100
# File 'lib/new_relic/stats.rb', line 94

def merge!(other_stats)
  Array(other_stats).each do |other|
    merge_attributes(other)
  end

  self
end

#merge_attributes(other) ⇒ Object



87
88
89
90
91
92
# File 'lib/new_relic/stats.rb', line 87

def merge_attributes(other)
  update_totals(other)
  expand_min_max_to(other)
  self.call_count += other.call_count
  update_boundaries(other)
end

#midpointObject



136
137
138
# File 'lib/new_relic/stats.rb', line 136

def midpoint
  begin_time + (duration/2)
end

#min_time_less?(other) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/new_relic/stats.rb', line 78

def min_time_less?(other)
  (other.min_call_time < min_call_time && other.call_count > 0) || call_count == 0
end

#multiply_by(percentage) ⇒ Object

multiply the total time and rate by the given percentage



183
184
185
186
187
188
189
# File 'lib/new_relic/stats.rb', line 183

def multiply_by(percentage)
  self.total_call_time = total_call_time * percentage
  self.call_count = call_count * percentage
  self.sum_of_squares = sum_of_squares * percentage

  self
end

#resetObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/new_relic/stats.rb', line 112

def reset
  self.call_count = 0
  self.total_call_time = 0.0
  self.total_exclusive_time = 0.0
  self.min_call_time = 0.0
  self.max_call_time = 0.0
  self.sum_of_squares = 0.0
  self.begin_time = Time.at(0)
  self.end_time = Time.at(0)
end

#should_replace_begin_time?(other) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/new_relic/stats.rb', line 68

def should_replace_begin_time?(other)
  other.begin_time.to_f < begin_time.to_f || begin_time.to_f == 0.0
end

#should_replace_end_time?(other) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/new_relic/stats.rb', line 64

def should_replace_end_time?(other)
  end_time.to_f < other.end_time.to_f
end

#stack_min_max_from(other) ⇒ Object



54
55
56
57
# File 'lib/new_relic/stats.rb', line 54

def stack_min_max_from(other)
  self.min_call_time += other.min_call_time
  self.max_call_time += other.max_call_time
end

#standard_deviationObject



147
148
149
150
151
152
153
154
155
156
# File 'lib/new_relic/stats.rb', line 147

def standard_deviation
  return 0 if call_count < 2 || self.sum_of_squares.nil?

  # Convert sum of squares into standard deviation based on
  # formula for the standard deviation for the entire population
  x = self.sum_of_squares - (self.call_count * (self.average_value**2))
  return 0 if x <= 0

  Math.sqrt(x / self.call_count)
end

#sum_attributes(other) ⇒ Object



47
48
49
50
51
52
# File 'lib/new_relic/stats.rb', line 47

def sum_attributes(other)
  update_totals(other)
  stack_min_max_from(other)
  self.call_count = [self.call_count, other.call_count].max
  update_boundaries(other)
end

#sum_merge!(other_stats) ⇒ Object

merge by adding to average response time

  • used to compose multiple metrics e.g. dispatcher time + mongrel queue time



40
41
42
43
44
45
# File 'lib/new_relic/stats.rb', line 40

def sum_merge! (other_stats)
  Array(other_stats).each do |other|
    self.sum_attributes(other)
  end
  self
end

#summaryObject

Summary string to facilitate testing



177
178
179
180
# File 'lib/new_relic/stats.rb', line 177

def summary
  format = "%m/%d/%y %I:%M%p"
  "[#{Time.at(begin_time.to_f).utc.strftime(format)} UTC, #{'%2.3fs' % duration.to_f}; #{'%2i' % call_count.to_i} calls #{'%4i' % average_call_time.to_f}s]"
end

#time_percentageObject

returns the time spent in this component as a percentage of the total time window.



160
161
162
# File 'lib/new_relic/stats.rb', line 160

def time_percentage
  checked_calculation(total_call_time, duration)
end

#time_str(value_ms) ⇒ Object

outputs a useful human-readable time given a value in milliseconds



11
12
13
14
15
16
17
18
19
20
# File 'lib/new_relic/stats.rb', line 11

def time_str(value_ms)
  case
  when value_ms >= 10000
    "%.1f s" % (value_ms / 1000.0)
  when value_ms >= 5000
    "%.2f s" % (value_ms / 1000.0)
  else
    "%.0f ms" % value_ms
  end
end

#to_sObject



172
173
174
# File 'lib/new_relic/stats.rb', line 172

def to_s
  summary
end

#total_call_time_per_minuteObject



143
144
145
# File 'lib/new_relic/stats.rb', line 143

def total_call_time_per_minute
  60.0 * time_percentage
end

#update_boundaries(other) ⇒ Object



59
60
61
62
# File 'lib/new_relic/stats.rb', line 59

def update_boundaries(other)
  self.begin_time = other.begin_time if should_replace_begin_time?(other)
  self.end_time = other.end_time if should_replace_end_time?(other)
end

#update_totals(other) ⇒ Object



72
73
74
75
76
# File 'lib/new_relic/stats.rb', line 72

def update_totals(other)
  self.total_call_time      += other.total_call_time
  self.total_exclusive_time += other.total_exclusive_time
  self.sum_of_squares       += other.sum_of_squares
end