|
| 1 | +""" |
| 2 | +Stress test execution functions for testing the OpenAPI client. |
| 3 | +""" |
| 4 | + |
| 5 | +""" |
| 6 | + StressTestConfig |
| 7 | +
|
| 8 | +Configuration for stress tests. |
| 9 | +
|
| 10 | +# Fields |
| 11 | +- `duration::Int` - Test duration in seconds |
| 12 | +- `concurrency::Int` - Number of concurrent tasks |
| 13 | +- `payload_size::Int` - POST payload size in bytes |
| 14 | +- `httplib::Symbol` - HTTP backend to use (:http or :downloads) |
| 15 | +- `target_url::String` - Base URL of the echo server |
| 16 | +""" |
| 17 | +mutable struct StressTestConfig |
| 18 | + duration::Int |
| 19 | + concurrency::Int |
| 20 | + payload_size::Int |
| 21 | + httplib::Symbol |
| 22 | + target_url::String |
| 23 | + |
| 24 | + function StressTestConfig(; |
| 25 | + duration=30, |
| 26 | + concurrency=100, |
| 27 | + payload_size=1024, |
| 28 | + httplib=:http, |
| 29 | + target_url="http://127.0.0.1:8082", |
| 30 | + ) |
| 31 | + return new(duration, concurrency, payload_size, httplib, target_url) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +function generate_payload(size::Int) |
| 36 | + data = "x" ^ max(1, size) |
| 37 | + return Main.StressTestClient.EchoPostRequest(; data = data) |
| 38 | +end |
| 39 | + |
| 40 | +""" |
| 41 | + run_get_stress_test(api::DefaultApi, config::StressTestConfig, metrics::StressMetrics) |
| 42 | +
|
| 43 | +Run a GET stress test for the specified duration and concurrency. |
| 44 | +
|
| 45 | +# Arguments |
| 46 | +- `api::DefaultApi` - API instance to use |
| 47 | +- `config::StressTestConfig` - Test configuration |
| 48 | +- `metrics::StressMetrics` - Metrics container to record results |
| 49 | +""" |
| 50 | +function run_get_stress_test(api::Main.StressTestClient.DefaultApi, config::StressTestConfig, metrics::StressMetrics) |
| 51 | + @info("Starting GET stress test") |
| 52 | + |
| 53 | + metrics.start_time = time() |
| 54 | + |
| 55 | + @sync begin |
| 56 | + for task_idx in 1:config.concurrency |
| 57 | + @async begin |
| 58 | + task_start = time() |
| 59 | + local requests_made = 0 |
| 60 | + |
| 61 | + while time() - task_start < config.duration |
| 62 | + try |
| 63 | + t0 = time() |
| 64 | + result, resp = Main.StressTestClient.echo_get(api) |
| 65 | + duration = time() - t0 |
| 66 | + |
| 67 | + if resp.status == 200 |
| 68 | + record_success(metrics, duration) |
| 69 | + else |
| 70 | + record_error(metrics, "HTTP-$(resp.status)") |
| 71 | + end |
| 72 | + |
| 73 | + requests_made += 1 |
| 74 | + catch ex |
| 75 | + error_type = string(typeof(ex)) |
| 76 | + # Remove module prefix for cleaner output |
| 77 | + error_type = split(error_type, ".")[end] |
| 78 | + record_error(metrics, error_type) |
| 79 | + end |
| 80 | + end |
| 81 | + end |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + metrics.end_time = time() |
| 86 | +end |
| 87 | + |
| 88 | +""" |
| 89 | + run_post_stress_test(api::DefaultApi, config::StressTestConfig, metrics::StressMetrics) |
| 90 | +
|
| 91 | +Run a POST stress test for the specified duration and concurrency. |
| 92 | +
|
| 93 | +# Arguments |
| 94 | +- `api::DefaultApi` - API instance to use |
| 95 | +- `config::StressTestConfig` - Test configuration |
| 96 | +- `metrics::StressMetrics` - Metrics container to record results |
| 97 | +""" |
| 98 | +function run_post_stress_test(api::Main.StressTestClient.DefaultApi, config::StressTestConfig, metrics::StressMetrics) |
| 99 | + @info("Starting POST stress test") |
| 100 | + payload = generate_payload(config.payload_size) |
| 101 | + metrics.start_time = time() |
| 102 | + |
| 103 | + @sync begin |
| 104 | + for task_idx in 1:config.concurrency |
| 105 | + @async begin |
| 106 | + task_start = time() |
| 107 | + local requests_made = 0 |
| 108 | + |
| 109 | + while time() - task_start < config.duration |
| 110 | + try |
| 111 | + t0 = time() |
| 112 | + result, resp = Main.StressTestClient.echo_post(api, payload) |
| 113 | + duration = time() - t0 |
| 114 | + |
| 115 | + if resp.status == 200 |
| 116 | + record_success(metrics, duration) |
| 117 | + else |
| 118 | + record_error(metrics, "HTTP-$(resp.status)") |
| 119 | + end |
| 120 | + |
| 121 | + requests_made += 1 |
| 122 | + catch ex |
| 123 | + error_type = string(typeof(ex)) |
| 124 | + # Remove module prefix for cleaner output |
| 125 | + error_type = split(error_type, ".")[end] |
| 126 | + record_error(metrics, error_type) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + metrics.end_time = time() |
| 134 | +end |
0 commit comments