foxy-io 0.3.12

A configuration-driven and hyper-extensible HTTP proxy library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Load testing for Foxy API Gateway

use foxy::Foxy;
use serde_json::json;
use serial_test::serial;
use std::sync::Arc;
use std::time::{Duration, Instant};
use tokio::sync::Semaphore;
use wiremock::matchers::{method, path};
use wiremock::{Mock, MockServer, ResponseTemplate};

mod common;
use common::{TestConfigProvider, TestRoute, init_test_logging};

/// Load test configuration
#[derive(Debug, Clone)]
pub struct LoadTestConfig {
    /// Number of concurrent requests
    pub concurrent_requests: usize,
    /// Total number of requests to send
    pub total_requests: usize,
    /// Duration to run the test
    pub duration: Duration,
    /// Request timeout
    pub request_timeout: Duration,
}

impl Default for LoadTestConfig {
    fn default() -> Self {
        Self {
            concurrent_requests: 10,
            total_requests: 1000,
            duration: Duration::from_secs(30),
            request_timeout: Duration::from_secs(5),
        }
    }
}

/// Load test results
#[derive(Debug)]
pub struct LoadTestResults {
    /// Total requests sent
    pub total_requests: usize,
    /// Successful requests
    pub successful_requests: usize,
    /// Failed requests
    pub failed_requests: usize,
    /// Total duration
    pub total_duration: Duration,
    /// Requests per second
    pub requests_per_second: f64,
    /// Average response time
    pub avg_response_time: Duration,
    /// Minimum response time
    pub min_response_time: Duration,
    /// Maximum response time
    pub max_response_time: Duration,
    /// 95th percentile response time
    pub p95_response_time: Duration,
    /// 99th percentile response time
    pub p99_response_time: Duration,
}

impl LoadTestResults {
    pub fn print_summary(&self) {
        println!("\n=== Load Test Results ===");
        println!("Total Requests: {}", self.total_requests);
        println!("Successful: {}", self.successful_requests);
        println!("Failed: {}", self.failed_requests);
        println!(
            "Success Rate: {:.2}%",
            (self.successful_requests as f64 / self.total_requests as f64) * 100.0
        );
        println!("Duration: {:.2}s", self.total_duration.as_secs_f64());
        println!("Requests/sec: {:.2}", self.requests_per_second);
        println!(
            "Avg Response Time: {:.2}ms",
            self.avg_response_time.as_millis()
        );
        println!(
            "Min Response Time: {:.2}ms",
            self.min_response_time.as_millis()
        );
        println!(
            "Max Response Time: {:.2}ms",
            self.max_response_time.as_millis()
        );
        println!(
            "95th Percentile: {:.2}ms",
            self.p95_response_time.as_millis()
        );
        println!(
            "99th Percentile: {:.2}ms",
            self.p99_response_time.as_millis()
        );
    }
}

/// Run a load test against the proxy
pub async fn run_load_test(proxy_url: &str, path: &str, config: LoadTestConfig) -> LoadTestResults {
    let client = reqwest::Client::builder()
        .timeout(config.request_timeout)
        .build()
        .expect("Failed to create HTTP client");

    let semaphore = Arc::new(Semaphore::new(config.concurrent_requests));
    let mut tasks = Vec::new();
    let mut response_times = Vec::new();

    let start_time = Instant::now();
    let url = format!("{proxy_url}{path}");

    for i in 0..config.total_requests {
        let client = client.clone();
        let url = url.clone();
        let semaphore = semaphore.clone();

        let task = tokio::spawn(async move {
            let _permit = semaphore.acquire().await.unwrap();

            let request_start = Instant::now();
            let result = client.get(&url).send().await;
            let request_duration = request_start.elapsed();

            match result {
                Ok(response) => {
                    let success = response.status().is_success();
                    (success, request_duration, i)
                }
                Err(_) => (false, request_duration, i),
            }
        });

        tasks.push(task);

        // Add small delay to avoid overwhelming the system
        if i % 100 == 0 && i > 0 {
            tokio::time::sleep(Duration::from_millis(10)).await;
        }
    }

    // Wait for all requests to complete
    let mut successful_requests = 0;
    let mut failed_requests = 0;

    for task in tasks {
        match task.await {
            Ok((success, duration, _)) => {
                response_times.push(duration);
                if success {
                    successful_requests += 1;
                } else {
                    failed_requests += 1;
                }
            }
            Err(_) => {
                failed_requests += 1;
            }
        }
    }

    let total_duration = start_time.elapsed();

    // Calculate statistics
    response_times.sort();
    let avg_response_time = Duration::from_nanos(
        (response_times.iter().map(|d| d.as_nanos()).sum::<u128>() / response_times.len() as u128)
            as u64,
    );
    let min_response_time = response_times.first().copied().unwrap_or_default();
    let max_response_time = response_times.last().copied().unwrap_or_default();

    let p95_index = (response_times.len() as f64 * 0.95) as usize;
    let p99_index = (response_times.len() as f64 * 0.99) as usize;
    let p95_response_time = response_times.get(p95_index).copied().unwrap_or_default();
    let p99_response_time = response_times.get(p99_index).copied().unwrap_or_default();

    let requests_per_second = config.total_requests as f64 / total_duration.as_secs_f64();

    LoadTestResults {
        total_requests: config.total_requests,
        successful_requests,
        failed_requests,
        total_duration,
        requests_per_second,
        avg_response_time,
        min_response_time,
        max_response_time,
        p95_response_time,
        p99_response_time,
    }
}

#[tokio::test]
#[ignore]
#[serial]
async fn test_basic_load() {
    init_test_logging();

    // Start a mock upstream server
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/api/test"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"message": "Hello, World!"}))
                .insert_header("content-type", "application/json"),
        )
        .mount(&mock_server)
        .await;

    // Configure the proxy with a specific port for this test
    let config = TestConfigProvider::new("load_test")
        .with_value("server.port", 8080)
        .with_routes(vec![
            TestRoute::new(&mock_server.uri()).with_path("/api/test"),
        ]);

    let foxy = Foxy::loader()
        .with_provider(config)
        .build()
        .await
        .expect("Failed to build Foxy instance");

    let server_handle = tokio::spawn(async move { foxy.start().await });

    // Give the server time to start
    tokio::time::sleep(Duration::from_millis(200)).await;

    // Run load test
    let load_config = LoadTestConfig {
        concurrent_requests: 5,
        total_requests: 100,
        duration: Duration::from_secs(10),
        request_timeout: Duration::from_secs(2),
    };

    let results = run_load_test("http://127.0.0.1:8080", "/api/test", load_config).await;

    results.print_summary();

    // Assertions
    assert!(
        results.successful_requests > 0,
        "Should have some successful requests"
    );
    assert!(
        results.requests_per_second > 0.0,
        "Should have positive RPS"
    );
    assert!(
        results.avg_response_time < Duration::from_secs(1),
        "Average response time should be reasonable"
    );

    // Success rate should be high for a simple test
    let success_rate = results.successful_requests as f64 / results.total_requests as f64;
    assert!(
        success_rate > 0.9,
        "Success rate should be > 90%, got {:.2}%",
        success_rate * 100.0
    );

    server_handle.abort();
}

#[tokio::test]
#[ignore]
#[serial]
async fn test_high_concurrency_load() {
    init_test_logging();

    // Start a mock upstream server
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/api/concurrent"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"timestamp": "2024-01-01T00:00:00Z"}))
                .insert_header("content-type", "application/json"),
        )
        .mount(&mock_server)
        .await;

    // Configure the proxy with a specific port for this test
    let config = TestConfigProvider::new("concurrent_test")
        .with_value("server.port", 8080)
        .with_routes(vec![
            TestRoute::new(&mock_server.uri()).with_path("/api/concurrent"),
        ]);

    let foxy = Foxy::loader()
        .with_provider(config)
        .build()
        .await
        .expect("Failed to build Foxy instance");

    let server_handle = tokio::spawn(async move { foxy.start().await });

    tokio::time::sleep(Duration::from_millis(200)).await;

    // Run high concurrency test
    let load_config = LoadTestConfig {
        concurrent_requests: 50,
        total_requests: 500,
        duration: Duration::from_secs(15),
        request_timeout: Duration::from_secs(3),
    };

    let results = run_load_test("http://127.0.0.1:8080", "/api/concurrent", load_config).await;

    results.print_summary();

    // Assertions for high concurrency
    assert!(
        results.successful_requests > 0,
        "Should handle concurrent requests"
    );
    assert!(
        results.requests_per_second > 10.0,
        "Should maintain reasonable throughput"
    );

    // Allow for some failures under high load
    let success_rate = results.successful_requests as f64 / results.total_requests as f64;
    assert!(
        success_rate > 0.8,
        "Success rate should be > 80% under high load, got {:.2}%",
        success_rate * 100.0
    );

    server_handle.abort();
}

#[tokio::test]
#[ignore]
#[serial]
async fn test_sustained_load() {
    init_test_logging();

    // Start a mock upstream server
    let mock_server = MockServer::start().await;

    Mock::given(method("GET"))
        .and(path("/api/sustained"))
        .respond_with(
            ResponseTemplate::new(200)
                .set_body_json(json!({"data": "sustained load test"}))
                .insert_header("content-type", "application/json"),
        )
        .mount(&mock_server)
        .await;

    // Configure the proxy with a specific port for this test
    let config = TestConfigProvider::new("sustained_test")
        .with_value("server.port", 8080)
        .with_routes(vec![
            TestRoute::new(&mock_server.uri()).with_path("/api/sustained"),
        ]);

    let foxy = Foxy::loader()
        .with_provider(config)
        .build()
        .await
        .expect("Failed to build Foxy instance");

    let server_handle = tokio::spawn(async move { foxy.start().await });

    tokio::time::sleep(Duration::from_millis(200)).await;

    // Run sustained load test
    let load_config = LoadTestConfig {
        concurrent_requests: 20,
        total_requests: 1000,
        duration: Duration::from_secs(30),
        request_timeout: Duration::from_secs(5),
    };

    let results = run_load_test("http://127.0.0.1:8080", "/api/sustained", load_config).await;

    results.print_summary();

    // Assertions for sustained load
    assert!(
        results.successful_requests > 0,
        "Should handle sustained load"
    );
    assert!(
        results.requests_per_second > 5.0,
        "Should maintain throughput over time"
    );

    // Check that response times don't degrade too much
    assert!(
        results.p95_response_time < Duration::from_secs(2),
        "95th percentile should be reasonable: {:?}",
        results.p95_response_time
    );

    server_handle.abort();
}