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



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

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



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

def as_percentage
  return 0 if call_count == 0
  to_percentage(total_call_time / call_count)
end

#as_percentage_of(other_stats) ⇒ Object



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

def as_percentage_of(other_stats)
  return 0 if other_stats.total_call_time == 0
  return to_percentage(total_call_time / other_stats.total_call_time)
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



118
119
120
121
# File 'lib/new_relic/stats.rb', line 118

def calls_per_minute
  return 0 if duration.zero?
  ((call_count / duration.to_f * 6000).round).to_f / 100
end

#calls_per_secondObject



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

def calls_per_second
  round_to_2 calls_per_minute / 60
end

#durationObject



114
115
116
# File 'lib/new_relic/stats.rb', line 114

def duration
  end_time - begin_time
end

#exclusive_time_percentageObject



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

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



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

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



212
213
214
# File 'lib/new_relic/stats.rb', line 212

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

#is_reset?Boolean

Returns:

  • (Boolean)


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

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

#merge(other_stats) ⇒ Object



45
46
47
48
# File 'lib/new_relic/stats.rb', line 45

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

#merge!(other_stats) ⇒ Object



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

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



202
203
204
205
206
207
208
# File 'lib/new_relic/stats.rb', line 202

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



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

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



175
176
177
178
179
180
181
182
183
# File 'lib/new_relic/stats.rb', line 175

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/new_relic/stats.rb', line 56

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



129
130
131
132
133
134
135
136
137
138
# File 'lib/new_relic/stats.rb', line 129

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

#summaryObject

Summary string to facilitate testing



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

def summary
  format = "%m/%d %I:%M%p"
  "[#{Time.at(begin_time).strftime(format)}, #{duration}s. #{call_count} calls; #{to_ms(average_call_time)}ms]"
end

#time_percentageObject

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



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

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



156
157
158
159
160
161
162
163
164
165
166
# File 'lib/new_relic/stats.rb', line 156

def to_s
  s = "Begin=#{begin_time}, "
  s << "Duration=#{duration} s, "
  s << "Count=#{call_count}, "
  s << "Total=#{to_ms(total_call_time)}, "
  s << "Total Exclusive=#{to_ms(total_exclusive_time)}, "
  s << "Avg=#{to_ms(average_call_time)}, "
  s << "Min=#{to_ms(min_call_time)}, "
  s << "Max=#{to_ms(max_call_time)}, "
  s << "StdDev=#{to_ms(standard_deviation)}"
end

#total_call_time_per_minuteObject



126
127
128
# File 'lib/new_relic/stats.rb', line 126

def total_call_time_per_minute
  60.0 * time_percentage
end