Skip to content

Commit 0acf3d2

Browse files
committed
Remove duplication in adding summaries
1 parent 06642d0 commit 0acf3d2

File tree

1 file changed

+37
-33
lines changed

1 file changed

+37
-33
lines changed

spec/performance/bench.rb

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ def parse_json_file(file_path, tool_name)
5757
raise "Failed to read #{tool_name} results: #{e.message}"
5858
end
5959

60+
def failure_metrics(error)
61+
["FAILED", "FAILED", "FAILED", "FAILED", error.message]
62+
end
63+
64+
def add_summary_line(*parts)
65+
File.open(SUMMARY_TXT, "a") do |f|
66+
f.puts parts.join("\t")
67+
end
68+
end
69+
6070
validate_rate(RATE)
6171
validate_positive_integer(CONNECTIONS, "CONNECTIONS")
6272
validate_positive_integer(MAX_CONNECTIONS, "MAX_CONNECTIONS")
@@ -120,11 +130,12 @@ def server_responding?(uri)
120130
end
121131

122132
# Initialize summary file
123-
File.write(SUMMARY_TXT, "Tool\tRPS\tp50(ms)\tp90(ms)\tp99(ms)\tStatus\n")
133+
File.write(SUMMARY_TXT, "")
134+
add_summary_line("Tool", "RPS", "p50(ms)", "p90(ms)", "p99(ms)", "Status")
124135

125136
# Fortio
126137
if TOOLS.include?("fortio")
127-
begin
138+
fortio_metrics = begin
128139
puts "===> Fortio"
129140

130141
fortio_json = "#{OUTDIR}/fortio.json"
@@ -162,21 +173,20 @@ def server_responding?(uri)
162173
fortio_p50 = (p50_data["Value"] * 1000).round(2)
163174
fortio_p90 = (p90_data["Value"] * 1000).round(2)
164175
fortio_p99 = (p99_data["Value"] * 1000).round(2)
165-
fortio_status = fortio_data["RetCodes"]&.map { |k, v| "#{k}=#{v}" }&.join(",") || "unknown"
166-
File.open(SUMMARY_TXT, "a") do |f|
167-
f.puts "Fortio\t#{fortio_rps}\t#{fortio_p50}\t#{fortio_p90}\t#{fortio_p99}\t#{fortio_status}"
168-
end
169-
rescue StandardError => e
170-
puts "Error: #{e.message}"
171-
File.open(SUMMARY_TXT, "a") do |f|
172-
f.puts "Fortio\tFAILED\tFAILED\tFAILED\tFAILED\t#{e.message}"
173-
end
176+
fortio_status = fortio_data["RetCodes"]&.map { |k, v| "#{k}=#{v}" }&.join(",") || "missing"
177+
178+
[fortio_rps, fortio_p50, fortio_p90, fortio_p99, fortio_status]
179+
rescue StandardError => error
180+
puts "Error: #{error.message}"
181+
failure_metrics(error)
174182
end
183+
184+
add_summary_line("Fortio", *fortio_metrics)
175185
end
176186

177187
# Vegeta
178188
if TOOLS.include?("vegeta")
179-
begin
189+
vegeta_metrics = begin
180190
puts "\n===> Vegeta"
181191

182192
vegeta_bin = "#{OUTDIR}/vegeta.bin"
@@ -207,24 +217,20 @@ def server_responding?(uri)
207217
vegeta_p50 = vegeta_data.dig("latencies", "50th")&./(1_000_000.0)&.round(2) || "missing"
208218
vegeta_p90 = vegeta_data.dig("latencies", "90th")&./(1_000_000.0)&.round(2) || "missing"
209219
vegeta_p99 = vegeta_data.dig("latencies", "99th")&./(1_000_000.0)&.round(2) || "missing"
210-
vegeta_status = vegeta_data["status_codes"]&.map { |k, v| "#{k}=#{v}" }&.join(",") || "unknown"
211-
vegeta_line = [
212-
"Vegeta", vegeta_rps, vegeta_p50, vegeta_p90, vegeta_p99, vegeta_status
213-
].join("\t")
214-
File.open(SUMMARY_TXT, "a") do |f|
215-
f.puts vegeta_line
216-
end
217-
rescue StandardError => e
218-
puts "Error: #{e.message}"
219-
File.open(SUMMARY_TXT, "a") do |f|
220-
f.puts "Vegeta\tFAILED\tFAILED\tFAILED\tFAILED\t#{e.message}"
221-
end
220+
vegeta_status = vegeta_data["status_codes"]&.map { |k, v| "#{k}=#{v}" }&.join(",") || "missing"
221+
222+
[vegeta_rps, vegeta_p50, vegeta_p90, vegeta_p99, vegeta_status]
223+
rescue StandardError => error
224+
puts "Error: #{error.message}"
225+
failure_metrics(error)
222226
end
227+
228+
add_summary_line("Vegeta", *vegeta_metrics)
223229
end
224230

225231
# k6
226232
if TOOLS.include?("k6")
227-
begin
233+
k6_metrics = begin
228234
puts "\n===> k6"
229235

230236
k6_script_file = "#{OUTDIR}/k6_test.js"
@@ -299,15 +305,13 @@ def server_responding?(uri)
299305
k6_status_parts << "other=#{k6_reqs_other}" if k6_reqs_other.positive?
300306
k6_status = k6_status_parts.empty? ? "missing" : k6_status_parts.join(",")
301307

302-
File.open(SUMMARY_TXT, "a") do |f|
303-
f.puts "k6\t#{k6_rps}\t#{k6_p50}\t#{k6_p90}\t#{k6_p99}\t#{k6_status}"
304-
end
305-
rescue StandardError => e
306-
puts "Error: #{e.message}"
307-
File.open(SUMMARY_TXT, "a") do |f|
308-
f.puts "k6\tFAILED\tFAILED\tFAILED\tFAILED\t#{e.message}"
309-
end
308+
[k6_rps, k6_p50, k6_p90, k6_p99, k6_status]
309+
rescue StandardError => error
310+
puts "Error: #{error.message}"
311+
failure_metrics(error)
310312
end
313+
314+
add_summary_line("k6", *k6_metrics)
311315
end
312316

313317
puts "\nSummary saved to #{SUMMARY_TXT}"

0 commit comments

Comments
 (0)