Class: Drnbench::RequestResponse::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/drnbench/request-response/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Result

Returns a new instance of Result.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/drnbench/request-response/result.rb', line 35

def initialize(params)
  @n_clients = params[:n_clients]
  @duration = params[:duration]
  @n_slow_requests = params[:n_slow_requests] || 5
  @n_fast_requests = params[:n_fast_requests] || 5

  @results = []
  @total_elapsed_time = 0.0
  @elapsed_times = []
  @statuses = {}
end

Instance Attribute Details

#durationObject

Returns the value of attribute duration.



20
21
22
# File 'lib/drnbench/request-response/result.rb', line 20

def duration
  @duration
end

#n_clientsObject (readonly)

Returns the value of attribute n_clients.



19
20
21
# File 'lib/drnbench/request-response/result.rb', line 19

def n_clients
  @n_clients
end

#n_fast_requestsObject (readonly)

Returns the value of attribute n_fast_requests.



19
20
21
# File 'lib/drnbench/request-response/result.rb', line 19

def n_fast_requests
  @n_fast_requests
end

#n_slow_requestsObject (readonly)

Returns the value of attribute n_slow_requests.



19
20
21
# File 'lib/drnbench/request-response/result.rb', line 19

def n_slow_requests
  @n_slow_requests
end

#statusesObject (readonly)

Returns the value of attribute statuses.



19
20
21
# File 'lib/drnbench/request-response/result.rb', line 19

def statuses
  @statuses
end

Class Method Details

.keysObject



23
24
25
26
27
28
29
30
31
32
# File 'lib/drnbench/request-response/result.rb', line 23

def keys
  [
    :n_clients,
    :total_n_requests,
    :queries_per_second,
    :min_elapsed_time,
    :max_elapsed_time,
    :average_elapsed_time,
  ]
end

Instance Method Details

#<<(result) ⇒ Object



47
48
49
# File 'lib/drnbench/request-response/result.rb', line 47

def <<(result)
  push(result)
end

#average_elapsed_timeObject



87
88
89
# File 'lib/drnbench/request-response/result.rb', line 87

def average_elapsed_time
  @average_elapsed_time ||= @total_elapsed_time / @elapsed_times.size
end

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/drnbench/request-response/result.rb', line 63

def empty?
  @results.empty?
end

#fast_requestsObject



126
127
128
129
130
# File 'lib/drnbench/request-response/result.rb', line 126

def fast_requests
  @results.sort do |a, b|
    a[:elapsed_time] <=> b[:elapsed_time]
  end
end

#format_result_for_request_line(result) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/drnbench/request-response/result.rb', line 103

def format_result_for_request_line(result)
  request = result[:request]
  status = result[:status].to_i
  if status.zero?
    status = "#{status}(aborted)"
  end
  index = result[:index]
  index = "#{index}(last)" if result[:last]
  [
    "#{result[:elapsed_time]} sec:",
    request["method"],
    status,
    "<#{result[:client]}>#{index}",
    "http://#{request["host"]}:#{request["port"]}#{request["path"]}",
  ].join(" ")
end

#max_elapsed_timeObject



83
84
85
# File 'lib/drnbench/request-response/result.rb', line 83

def max_elapsed_time
  @max_elapsed_time ||= @elapsed_times.max
end

#min_elapsed_timeObject



79
80
81
# File 'lib/drnbench/request-response/result.rb', line 79

def min_elapsed_time
  @min_elapsed_time ||= @elapsed_times.min
end

#push(result) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/drnbench/request-response/result.rb', line 51

def push(result)
  clear_cached_statistics

  @results << result

  @statuses[result[:status]] ||= 0
  @statuses[result[:status]] += 1

  @elapsed_times << result[:elapsed_time]
  @total_elapsed_time += result[:elapsed_time]
end

#queries_per_secondObject



71
72
73
# File 'lib/drnbench/request-response/result.rb', line 71

def queries_per_second
  @queries_per_second ||= total_n_requests.to_f / @duration
end

#slow_requestsObject



120
121
122
123
124
# File 'lib/drnbench/request-response/result.rb', line 120

def slow_requests
  @results.sort do |a, b|
    b[:elapsed_time] <=> a[:elapsed_time]
  end
end

#status_percentagesObject



75
76
77
# File 'lib/drnbench/request-response/result.rb', line 75

def status_percentages
  @status_percentages ||= prepare_status_percentages
end

#to_sObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/drnbench/request-response/result.rb', line 132

def to_s
  "Total requests: #{total_n_requests} " +
    "(#{queries_per_second} queries per second)\n" +
  "Status:\n" +
  status_percentages.collect do |status, percentage|
    "  #{status}: #{percentage} %"
  end.join("\n") + "\n" +
  "Elapsed time:\n" +
  "  min:     #{min_elapsed_time} sec\n" +
  "  max:     #{max_elapsed_time} sec\n" +
  "  average: #{average_elapsed_time} sec\n" +
  "Top #{@n_fast_requests} fast requests:\n" +
  " [time: method status <client>index url]\n" +
  top_fast_requests.collect do |request|
    "  #{request}"
  end.join("\n") + "\n" +
  "Top #{@n_slow_requests} slow requests:\n" +
  " [time: method status <client>index url]\n" +
  top_slow_requests.collect do |request|
    "  #{request}"
  end.join("\n")
end

#top_fast_requestsObject



97
98
99
100
101
# File 'lib/drnbench/request-response/result.rb', line 97

def top_fast_requests
  fast_requests[0..@n_fast_requests-1].collect do |result|
    format_result_for_request_line(result)
  end
end

#top_slow_requestsObject



91
92
93
94
95
# File 'lib/drnbench/request-response/result.rb', line 91

def top_slow_requests
  slow_requests[0..@n_slow_requests-1].collect do |result|
    format_result_for_request_line(result)
  end
end

#total_n_requestsObject



67
68
69
# File 'lib/drnbench/request-response/result.rb', line 67

def total_n_requests
  @total_n_requests ||= @results.size
end

#valuesObject



155
156
157
158
159
# File 'lib/drnbench/request-response/result.rb', line 155

def values
  self.class.keys.collect do |column|
    send(column)
  end
end