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

#clear_cachesObject



156
157
158
159
160
161
162
163
# File 'lib/kronk/player/benchmark.rb', line 156

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

#deviationObject



83
84
85
86
87
88
89
90
91
92
# File 'lib/kronk/player/benchmark.rb', line 83

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
# 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][0] = @paths[uri][0].round @precision
  @paths[uri][1] = pcount
end

#meanObject



95
96
97
98
# File 'lib/kronk/player/benchmark.rb', line 95

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

#medianObject



101
102
103
104
# File 'lib/kronk/player/benchmark.rb', line 101

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

#percentagesObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/kronk/player/benchmark.rb', line 107

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



135
136
137
138
# File 'lib/kronk/player/benchmark.rb', line 135

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

#slowest_reqsObject



151
152
153
# File 'lib/kronk/player/benchmark.rb', line 151

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

#sumObject



146
147
148
# File 'lib/kronk/player/benchmark.rb', line 146

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

#to_sObject



166
167
168
169
170
171
172
173
174
175
176
177
178
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
# File 'lib/kronk/player/benchmark.rb', line 166

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



141
142
143
# File 'lib/kronk/player/benchmark.rb', line 141

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