Module: NewRelic::Stats

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

Instance Method Summary collapse

Instance Method Details

#absent?Boolean

Returns:

  • (Boolean)


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

def absent?
  # guess on absent values
  call_count == 0
end

#apdex_scoreObject



229
230
231
232
# File 'lib/new_relic/stats.rb', line 229

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



126
127
128
129
130
131
132
# File 'lib/new_relic/stats.rb', line 126

def as_percentage
  if call_count.zero?
    0
  else
    (total_call_time / call_count) * 100.0
  end
end

#as_percentage_of(other_stats) ⇒ Object



120
121
122
123
# File 'lib/new_relic/stats.rb', line 120

def as_percentage_of(other_stats)
  return 0 if other_stats.total_call_time == 0
  return (total_call_time / other_stats.total_call_time) * 100.0
end

#average_call_timeObject Also known as: average_value, average_response_time



21
22
23
24
# File 'lib/new_relic/stats.rb', line 21

def average_call_time
  return 0 if call_count == 0
  total_call_time / call_count
end

#average_exclusive_timeObject



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

def average_exclusive_time
  return 0 if call_count == 0
  total_exclusive_time / call_count
end

#calls_per_minuteObject Also known as: requests_per_minute



138
139
140
141
142
143
144
# File 'lib/new_relic/stats.rb', line 138

def calls_per_minute
  if duration.zero?
    0
  else
    (call_count / duration.to_f) * 60.0
  end
end

#durationObject



134
135
136
# File 'lib/new_relic/stats.rb', line 134

def duration
  end_time - begin_time
end

#exclusive_time_percentageObject



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

def exclusive_time_percentage
  return 0 if duration == 0
  total_exclusive_time / duration
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



201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/new_relic/stats.rb', line 201

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



225
226
227
# File 'lib/new_relic/stats.rb', line 225

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

#is_reset?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/new_relic/stats.rb', line 105

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

#merge(other_stats) ⇒ Object



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

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

#merge!(other_stats) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/new_relic/stats.rb', line 47

def merge!(other_stats)
  Array(other_stats).each do |s|
    self.total_call_time += s.total_call_time
    self.total_exclusive_time += s.total_exclusive_time
    self.min_call_time = s.min_call_time if (s.min_call_time < min_call_time && s.call_count > 0) || call_count == 0
    self.max_call_time = s.max_call_time if s.max_call_time > max_call_time
    self.call_count += s.call_count
    self.sum_of_squares += s.sum_of_squares if s.sum_of_squares
    self.begin_time = s.begin_time if s.begin_time.to_f < begin_time.to_f || begin_time.to_f == 0.0
    self.end_time = s.end_time if s.end_time.to_f > end_time.to_f
  end

  self
end

#multiply_by(percentage) ⇒ Object

multiply the total time and rate by the given percentage



215
216
217
218
219
220
221
# File 'lib/new_relic/stats.rb', line 215

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



109
110
111
112
113
114
115
116
117
118
# File 'lib/new_relic/stats.rb', line 109

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

#round!Object

round all of the values to n decimal points



188
189
190
191
192
193
194
195
196
# File 'lib/new_relic/stats.rb', line 188

def round!
  self.total_call_time = round_to_3(total_call_time)
  self.total_exclusive_time = round_to_3(total_exclusive_time)
  self.min_call_time = round_to_3(min_call_time)
  self.max_call_time = round_to_3(max_call_time)
  self.sum_of_squares = round_to_3(sum_of_squares)
  self.begin_time = begin_time
  self.end_time = end_time
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



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/new_relic/stats.rb', line 73

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

#standard_deviationObject



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

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_merge!(other_stats) ⇒ Object

merge by adding to average response time

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



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/new_relic/stats.rb', line 32

def sum_merge! (other_stats)
  Array(other_stats).each do |s|
    self.total_call_time += s.total_call_time
    self.total_exclusive_time += s.total_exclusive_time
    self.min_call_time += s.min_call_time
    self.max_call_time += s.max_call_time
    #self.call_count += s.call_count - do not add call count because we are stacking these times on top of each other
    self.sum_of_squares += s.sum_of_squares if s.sum_of_squares
    self.begin_time = s.begin_time if s.begin_time.to_f < begin_time.to_f || begin_time.to_f == 0.0
    self.end_time = s.end_time if s.end_time.to_f > end_time.to_f
  end

  self
end

#summaryObject

Summary string to facilitate testing



182
183
184
185
# File 'lib/new_relic/stats.rb', line 182

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

#time_percentageObject

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



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

def time_percentage
  return 0 if duration == 0
  total_call_time / duration
end

#time_str(value_ms) ⇒ Object



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

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



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

def to_s
  summary
end

#total_call_time_per_minuteObject



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

def total_call_time_per_minute
  60.0 * time_percentage
end