Class: Kronk::Player::Benchmark::ResultSet

Inherits:
Object
  • Object
show all
Defined in:
lib/kronk/player/benchmark.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultSet

Returns a new instance of ResultSet.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/kronk/player/benchmark.rb', line 25

def initialize
  @times     = Hash.new(0)
  @count     = 0
  @r5XX      = 0
  @r4XX      = 0
  @r3XX      = 0
  @err_count = 0

  @precision = 3

  @slowest = nil
  @fastest = nil

  @paths = {}

  @total_bytes = 0
  @byterate    = 0

  @total_time = 0
end

Instance Attribute Details

#byterateObject (readonly)

Returns the value of attribute byterate.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def byterate
  @byterate
end

#countObject (readonly)

Returns the value of attribute count.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def count
  @count
end

#err_countObject

Returns the value of attribute err_count.



20
21
22
# File 'lib/kronk/player/benchmark.rb', line 20

def err_count
  @err_count
end

#fastestObject (readonly)

Returns the value of attribute fastest.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def fastest
  @fastest
end

#precisionObject (readonly)

Returns the value of attribute precision.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def precision
  @precision
end

#slowestObject (readonly)

Returns the value of attribute slowest.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def slowest
  @slowest
end

#total_bytesObject (readonly)

Returns the value of attribute total_bytes.



22
23
24
# File 'lib/kronk/player/benchmark.rb', line 22

def total_bytes
  @total_bytes
end

#total_timeObject

Returns the value of attribute total_time.



20
21
22
# File 'lib/kronk/player/benchmark.rb', line 20

def total_time
  @total_time
end

Instance Method Details

#add_result(resp) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/kronk/player/benchmark.rb', line 47

def add_result resp
  time = (resp.time * 1000).round

  @times[time] += 1
  @count += 1

  case resp.code[0, 1]
  when "5" then @r5XX += 1
  when "4" then @r4XX += 1
  when "3" then @r3XX += 1
  end

  @slowest = time if !@slowest || @slowest < time
  @fastest = time if !@fastest || @fastest > time

  log_req resp.request, time if resp.request

  @total_bytes += resp.total_bytes

  @byterate = (@byterate * (@count-1) + resp.byterate) / @count
end

#clean_req_logObject



84
85
86
87
88
# File 'lib/kronk/player/benchmark.rb', line 84

def clean_req_log
  if @paths.length > 500
    order_reqs[500..-1].each{|(uri,_)| @paths.delete uri }
  end
end

#clear_cachesObject



169
170
171
172
173
174
175
176
# File 'lib/kronk/player/benchmark.rb', line 169

def clear_caches
  @percentages  = nil
  @slowest_reqs = nil
  @sum          = nil
  @mean         = nil
  @median       = nil
  @deviation    = nil
end

#deviationObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/kronk/player/benchmark.rb', line 91

def deviation
  return 0 if @count == 0
  return @deviation if @deviation

  mdiff = @times.to_a.inject(0) do |sum, (time, count)|
            sum + ((time-self.mean)**2) * count
          end

  @deviation = ((mdiff / @count)**0.5).round @precision
end

#log_req(req, time) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/kronk/player/benchmark.rb', line 70

def log_req req, time
  uri = req.uri.dup
  uri.query = nil
  uri = "#{req.http_method} #{uri.to_s}"

  @paths[uri] ||= [0, 0]
  pcount = @paths[uri][1] + 1
  @paths[uri][0] = (@paths[uri][0] * @paths[uri][1] + time) / pcount
  @paths[uri][1] = pcount

  clean_req_log
end

#meanObject



103
104
105
106
# File 'lib/kronk/player/benchmark.rb', line 103

def mean
  return 0 if @count == 0
  @mean ||= (self.sum / @count).round @precision
end

#medianObject



109
110
111
112
# File 'lib/kronk/player/benchmark.rb', line 109

def median
  return 0 if @count == 0
  @median ||= ((@slowest + @fastest) / 2).round @precision
end

#order_reqsObject



159
160
161
# File 'lib/kronk/player/benchmark.rb', line 159

def order_reqs
  @paths.to_a.sort{|x,y| y[1][0] <=> x[1][0]}
end

#percentagesObject



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
# File 'lib/kronk/player/benchmark.rb', line 115

def percentages
  return @percentages if @percentages

  @percentages = {}

  perc_list   = [50, 66, 75, 80, 90, 95, 98, 99]
  times_count = 0
  target_perc = perc_list.first

  i = 0
  @times.keys.sort.each do |time|
    times_count += @times[time]

    if target_perc <= (100 * times_count / @count)
      @percentages[target_perc] = time
      i += 1
      target_perc = perc_list[i]

      break unless target_perc
    end
  end

  perc_list.each{|l| @percentages[l] ||= self.slowest }
  @percentages[100] = self.slowest
  @percentages
end

#req_per_secObject



143
144
145
146
# File 'lib/kronk/player/benchmark.rb', line 143

def req_per_sec
  return 0 if @count == 0
  (@count / @total_time).round @precision
end

#slowest_reqsObject



164
165
166
# File 'lib/kronk/player/benchmark.rb', line 164

def slowest_reqs
  @slowest_reqs ||= order_reqs[0..9]
end

#sumObject



154
155
156
# File 'lib/kronk/player/benchmark.rb', line 154

def sum
  @sum ||= @times.inject(0){|sum, (time,count)| sum + time * count}
end

#to_sObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/kronk/player/benchmark.rb', line 179

def to_s
  clear_caches

  out = <<-STR

Completed:     #{@count}
300s:          #{@r3XX}
400s:          #{@r4XX}
500s:          #{@r5XX}
Errors:        #{@err_count}
Req/Sec:       #{self.req_per_sec}
Total Bytes:   #{@total_bytes}
Transfer Rate: #{self.transfer_rate} Kbytes/sec

Connection Times (ms)
  Min:       #{self.fastest || 0}
  Mean:      #{self.mean}
  [+/-sd]:   #{self.deviation}
  Median:    #{self.median}
  Max:       #{self.slowest || 0}

Request Percentages (ms)
   50%    #{self.percentages[50]}
   66%    #{self.percentages[66]}
   75%    #{self.percentages[75]}
   80%    #{self.percentages[80]}
   90%    #{self.percentages[90]}
   95%    #{self.percentages[95]}
   98%    #{self.percentages[98]}
   99%    #{self.percentages[99]}
  100%    #{self.percentages[100]} (longest request)
  STR

  unless slowest_reqs.empty?
    out << "
Avg. Slowest Requests (ms, count)
#{slowest_reqs.map{|arr| "  #{arr[1].inspect}  #{arr[0]}"}.join "\n" }"
  end

  out
end

#transfer_rateObject



149
150
151
# File 'lib/kronk/player/benchmark.rb', line 149

def transfer_rate
  ((@total_bytes / 1000) / @total_time).round @precision
end