Skip to content

Commit c0d0f45

Browse files
committed
add a load test script
1 parent 758016d commit c0d0f45

File tree

3 files changed

+118
-0
lines changed

3 files changed

+118
-0
lines changed

script/load_test

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/usr/bin/env ruby
2+
# frozen_string_literal: true
3+
4+
require "net/http"
5+
require "json"
6+
require "uri"
7+
8+
# Configuration
9+
TARGET_URL = "http://0.0.0.0:8080/webhooks/hello"
10+
REQUEST_COUNT = 10_000 # Total number of requests to send
11+
EMPTY_JSON_BODY = "{}"
12+
13+
# Parse the target URL
14+
uri = URI.parse(TARGET_URL)
15+
16+
# Initialize statistics tracking
17+
response_times = []
18+
success_count = 0
19+
error_count = 0
20+
21+
puts "Starting load test..."
22+
puts "Target: #{TARGET_URL}"
23+
puts "Requests: #{REQUEST_COUNT}"
24+
puts "Payload: #{EMPTY_JSON_BODY}"
25+
puts ""
26+
27+
# Perform the load test
28+
REQUEST_COUNT.times do |i|
29+
start_time = Time.now
30+
31+
begin
32+
# Create HTTP connection
33+
http = Net::HTTP.new(uri.host, uri.port)
34+
http.use_ssl = false if uri.scheme == "http"
35+
36+
# Create POST request
37+
request = Net::HTTP::Post.new(uri.path)
38+
request["Content-Type"] = "application/json"
39+
request.body = EMPTY_JSON_BODY
40+
41+
# Send request and measure time
42+
response = http.request(request)
43+
end_time = Time.now
44+
45+
response_time_ms = ((end_time - start_time) * 1000).round(2)
46+
response_times << response_time_ms
47+
48+
if response.code.to_i >= 200 && response.code.to_i < 300
49+
success_count += 1
50+
else
51+
error_count += 1
52+
end
53+
54+
# Progress indicator
55+
if (i + 1) % 100 == 0
56+
puts "Completed #{i + 1}/#{REQUEST_COUNT} requests"
57+
end
58+
59+
rescue => e
60+
end_time = Time.now
61+
response_time_ms = ((end_time - start_time) * 1000).round(2)
62+
response_times << response_time_ms
63+
error_count += 1
64+
puts "Error on request #{i + 1}: #{e.message}"
65+
end
66+
end
67+
68+
puts ""
69+
puts "Load test completed!"
70+
puts ""
71+
72+
# Calculate statistics
73+
if response_times.any?
74+
sorted_times = response_times.sort
75+
average_time = (response_times.sum / response_times.length).round(2)
76+
min_time = sorted_times.first
77+
max_time = sorted_times.last
78+
median_time = if sorted_times.length.odd?
79+
sorted_times[sorted_times.length / 2]
80+
else
81+
((sorted_times[sorted_times.length / 2 - 1] + sorted_times[sorted_times.length / 2]) / 2.0).round(2)
82+
end
83+
84+
# Calculate percentiles
85+
p95_index = (sorted_times.length * 0.95).ceil - 1
86+
p99_index = (sorted_times.length * 0.99).ceil - 1
87+
p95_time = sorted_times[p95_index]
88+
p99_time = sorted_times[p99_index]
89+
90+
puts "=== RESULTS SUMMARY ==="
91+
puts "Total requests: #{REQUEST_COUNT}"
92+
puts "Successful requests: #{success_count}"
93+
puts "Failed requests: #{error_count}"
94+
puts "Success rate: #{((success_count.to_f / REQUEST_COUNT) * 100).round(2)}%"
95+
puts ""
96+
puts "=== RESPONSE TIME STATISTICS (ms) ==="
97+
puts "Average: #{average_time} ms"
98+
puts "Minimum: #{min_time} ms"
99+
puts "Maximum: #{max_time} ms"
100+
puts "Median: #{median_time} ms"
101+
puts "95th percentile: #{p95_time} ms"
102+
puts "99th percentile: #{p99_time} ms"
103+
else
104+
puts "No response times recorded!"
105+
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
path: /hello
2+
handler: Hello
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# frozen_string_literal: true
2+
3+
class Hello < Hooks::Plugins::Handlers::Base
4+
def call(payload:, headers:, config:)
5+
{
6+
status: "success",
7+
handler: self.class.name,
8+
timestamp: Time.now.iso8601
9+
}
10+
end
11+
end

0 commit comments

Comments
 (0)