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



249
250
251
252
# File 'lib/new_relic/stats.rb', line 249

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



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

def as_percentage
  average_call_time * 100.0
end

#as_percentage_of(other_stats) ⇒ Object



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

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



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

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



169
170
171
# File 'lib/new_relic/stats.rb', line 169

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

#exclusive_time_percentageObject



201
202
203
# File 'lib/new_relic/stats.rb', line 201

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

#fraction_of(s) ⇒ Object

calculate this set of stats to be a percentage fraction of the provided stats, which has an overlapping time window. used as a key part of the split algorithm



222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/new_relic/stats.rb', line 222

def fraction_of(s)
  min_end = (end_time < s.end_time ? end_time : s.end_time)
  max_begin = (begin_time > s.begin_time ? begin_time : s.begin_time)
  percentage = (min_end - max_begin) / s.duration

  self.total_exclusive_time = s.total_exclusive_time * percentage
  self.total_call_time = s.total_call_time * percentage
  self.min_call_time = s.min_call_time
  self.max_call_time = s.max_call_time
  self.call_count = s.call_count * percentage
  self.sum_of_squares = (s.sum_of_squares || 0) * percentage
end

#get_apdexObject

returns s,t,f



245
246
247
# File 'lib/new_relic/stats.rb', line 245

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

#is_reset?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'lib/new_relic/stats.rb', line 145

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



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

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



236
237
238
239
240
241
242
# File 'lib/new_relic/stats.rb', line 236

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



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

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

#split(rollup_begin_time, rollup_period) ⇒ Object

split into an array of timeslices whose time boundaries start on (begin_time + (n * duration)) and whose end time ends on (begin_time * (n + 1) * duration), except for the first and last elements, whose begin time and end time are the begin and end times of this stats instance, respectively. Yield to caller for the code that creates the actual stats instance



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/new_relic/stats.rb', line 113

def split(rollup_begin_time, rollup_period)
  rollup_begin_time = rollup_begin_time.to_f
  rollup_begin_time += ((self.begin_time - rollup_begin_time) / rollup_period).floor * rollup_period

  current_begin_time = self.begin_time
  current_end_time = rollup_begin_time + rollup_period

  return [self] if current_end_time >= self.end_time

  timeslices = []
  while current_end_time < self.end_time do
    ts = yield(current_begin_time, current_end_time)
    if ts
      ts.fraction_of(self)
      timeslices << ts
    end
    current_begin_time = current_end_time
    current_end_time = current_begin_time + rollup_period
  end

  if self.end_time > current_begin_time
    percentage = rollup_period / self.duration + (self.begin_time - rollup_begin_time) / rollup_period
    ts = yield(current_begin_time, self.end_time)
    if ts
      ts.fraction_of(self)
      timeslices << ts
    end
  end

  timeslices
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



184
185
186
187
188
189
190
191
192
193
# File 'lib/new_relic/stats.rb', line 184

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



214
215
216
217
# File 'lib/new_relic/stats.rb', line 214

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.



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

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



209
210
211
# File 'lib/new_relic/stats.rb', line 209

def to_s
  summary
end

#total_call_time_per_minuteObject



180
181
182
# File 'lib/new_relic/stats.rb', line 180

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