rusmes-loadtest 0.1.2

Multi-protocol load testing tool for RusMES — concurrent SMTP, IMAP, and JMAP load generation with HDR histogram latency reporting and interactive TUI dashboard
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Report generation for load test results

use crate::metrics::LoadTestMetrics;
use anyhow::Result;
use std::fs::File;
use std::io::Write;
use std::path::Path;

/// Report generator
pub struct Reporter;

impl Reporter {
    /// Generate JSON report
    pub fn generate_json(metrics: &LoadTestMetrics, path: &Path) -> Result<()> {
        let report = JsonReport::from_metrics(metrics);
        let json = serde_json::to_string_pretty(&report)?;

        let mut file = File::create(path)?;
        file.write_all(json.as_bytes())?;

        Ok(())
    }

    /// Generate CSV report
    pub fn generate_csv(metrics: &LoadTestMetrics, path: &Path) -> Result<()> {
        let mut file = File::create(path)?;

        writeln!(file, "metric,value")?;
        writeln!(file, "total_requests,{}", metrics.total_requests)?;
        writeln!(file, "successful_requests,{}", metrics.successful_requests)?;
        writeln!(file, "failed_requests,{}", metrics.failed_requests)?;
        writeln!(file, "success_rate,{:.4}", metrics.success_rate())?;
        writeln!(
            file,
            "requests_per_second,{:.2}",
            metrics.requests_per_second()
        )?;
        writeln!(file, "bytes_sent,{}", metrics.bytes_sent)?;
        writeln!(file, "bytes_received,{}", metrics.bytes_received)?;

        if let Some(duration) = metrics.duration() {
            writeln!(file, "duration_secs,{:.2}", duration.as_secs_f64())?;
        }

        let stats = metrics.latency_stats();
        writeln!(
            file,
            "latency_min_ms,{:.2}",
            stats.min.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_max_ms,{:.2}",
            stats.max.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_mean_ms,{:.2}",
            stats.mean.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_p50_ms,{:.2}",
            stats.p50.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_p95_ms,{:.2}",
            stats.p95.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_p99_ms,{:.2}",
            stats.p99.as_secs_f64() * 1000.0
        )?;
        writeln!(
            file,
            "latency_p999_ms,{:.2}",
            stats.p999.as_secs_f64() * 1000.0
        )?;

        Ok(())
    }

    /// Generate HTML report
    pub fn generate_html(metrics: &LoadTestMetrics, path: &Path) -> Result<()> {
        let stats = metrics.latency_stats();
        let duration = metrics
            .duration()
            .map(|d| format!("{:.2}s", d.as_secs_f64()))
            .unwrap_or_else(|| "N/A".to_string());

        let html = format!(
            r#"<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Load Test Report</title>
    <style>
        body {{
            font-family: Arial, sans-serif;
            margin: 20px;
            background-color: #f5f5f5;
        }}
        .container {{
            max-width: 1200px;
            margin: 0 auto;
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        }}
        h1 {{
            color: #333;
            border-bottom: 2px solid #4CAF50;
            padding-bottom: 10px;
        }}
        h2 {{
            color: #555;
            margin-top: 30px;
        }}
        table {{
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
        }}
        th, td {{
            padding: 12px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }}
        th {{
            background-color: #4CAF50;
            color: white;
        }}
        tr:hover {{
            background-color: #f5f5f5;
        }}
        .success {{
            color: #4CAF50;
        }}
        .error {{
            color: #f44336;
        }}
        .metric-value {{
            font-weight: bold;
            font-size: 1.2em;
        }}
    </style>
</head>
<body>
    <div class="container">
        <h1>Load Test Report</h1>

        <h2>Summary</h2>
        <table>
            <tr>
                <th>Metric</th>
                <th>Value</th>
            </tr>
            <tr>
                <td>Duration</td>
                <td class="metric-value">{}</td>
            </tr>
            <tr>
                <td>Total Requests</td>
                <td class="metric-value">{}</td>
            </tr>
            <tr>
                <td>Successful Requests</td>
                <td class="metric-value success">{}</td>
            </tr>
            <tr>
                <td>Failed Requests</td>
                <td class="metric-value error">{}</td>
            </tr>
            <tr>
                <td>Success Rate</td>
                <td class="metric-value">{:.2}%</td>
            </tr>
            <tr>
                <td>Throughput</td>
                <td class="metric-value">{:.2} req/s</td>
            </tr>
        </table>

        <h2>Data Transfer</h2>
        <table>
            <tr>
                <th>Metric</th>
                <th>Value</th>
            </tr>
            <tr>
                <td>Bytes Sent</td>
                <td class="metric-value">{} bytes ({:.2} MB)</td>
            </tr>
            <tr>
                <td>Bytes Received</td>
                <td class="metric-value">{} bytes ({:.2} MB)</td>
            </tr>
        </table>

        <h2>Latency Statistics</h2>
        <table>
            <tr>
                <th>Percentile</th>
                <th>Latency</th>
            </tr>
            <tr>
                <td>Minimum</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>Mean</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>Maximum</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>p50</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>p95</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>p99</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
            <tr>
                <td>p99.9</td>
                <td class="metric-value">{:.2}ms</td>
            </tr>
        </table>

        <h2>Errors</h2>
        <p>Total Errors: {}</p>
        {}
    </div>
</body>
</html>"#,
            duration,
            metrics.total_requests,
            metrics.successful_requests,
            metrics.failed_requests,
            metrics.success_rate() * 100.0,
            metrics.requests_per_second(),
            metrics.bytes_sent,
            metrics.bytes_sent as f64 / 1_000_000.0,
            metrics.bytes_received,
            metrics.bytes_received as f64 / 1_000_000.0,
            stats.min.as_secs_f64() * 1000.0,
            stats.mean.as_secs_f64() * 1000.0,
            stats.max.as_secs_f64() * 1000.0,
            stats.p50.as_secs_f64() * 1000.0,
            stats.p95.as_secs_f64() * 1000.0,
            stats.p99.as_secs_f64() * 1000.0,
            stats.p999.as_secs_f64() * 1000.0,
            metrics.errors.len(),
            if metrics.errors.is_empty() {
                "<p>No errors occurred during the test.</p>".to_string()
            } else {
                let mut error_html = String::from("<ul>");
                for error in metrics.errors.iter().take(20) {
                    error_html.push_str(&format!("<li>{}</li>", error));
                }
                error_html.push_str("</ul>");
                error_html
            }
        );

        let mut file = File::create(path)?;
        file.write_all(html.as_bytes())?;

        Ok(())
    }

    /// Generate Prometheus metrics format
    pub fn generate_prometheus_metrics(metrics: &LoadTestMetrics) -> String {
        let stats = metrics.latency_stats();

        format!(
            "# HELP loadtest_total_requests Total number of requests\n\
             # TYPE loadtest_total_requests counter\n\
             loadtest_total_requests {}\n\
             # HELP loadtest_successful_requests Number of successful requests\n\
             # TYPE loadtest_successful_requests counter\n\
             loadtest_successful_requests {}\n\
             # HELP loadtest_failed_requests Number of failed requests\n\
             # TYPE loadtest_failed_requests counter\n\
             loadtest_failed_requests {}\n\
             # HELP loadtest_success_rate Success rate (0.0-1.0)\n\
             # TYPE loadtest_success_rate gauge\n\
             loadtest_success_rate {:.4}\n\
             # HELP loadtest_requests_per_second Request throughput\n\
             # TYPE loadtest_requests_per_second gauge\n\
             loadtest_requests_per_second {:.2}\n\
             # HELP loadtest_bytes_sent Total bytes sent\n\
             # TYPE loadtest_bytes_sent counter\n\
             loadtest_bytes_sent {}\n\
             # HELP loadtest_bytes_received Total bytes received\n\
             # TYPE loadtest_bytes_received counter\n\
             loadtest_bytes_received {}\n\
             # HELP loadtest_latency_seconds Latency in seconds\n\
             # TYPE loadtest_latency_seconds summary\n\
             loadtest_latency_seconds{{quantile=\"0.5\"}} {:.6}\n\
             loadtest_latency_seconds{{quantile=\"0.95\"}} {:.6}\n\
             loadtest_latency_seconds{{quantile=\"0.99\"}} {:.6}\n\
             loadtest_latency_seconds{{quantile=\"0.999\"}} {:.6}\n\
             loadtest_latency_seconds_sum {:.6}\n\
             loadtest_latency_seconds_count {}\n",
            metrics.total_requests,
            metrics.successful_requests,
            metrics.failed_requests,
            metrics.success_rate(),
            metrics.requests_per_second(),
            metrics.bytes_sent,
            metrics.bytes_received,
            stats.p50.as_secs_f64(),
            stats.p95.as_secs_f64(),
            stats.p99.as_secs_f64(),
            stats.p999.as_secs_f64(),
            stats.mean.as_secs_f64() * metrics.successful_requests as f64,
            metrics.successful_requests,
        )
    }
}

/// JSON report structure
#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct JsonReport {
    duration_secs: f64,
    total_requests: u64,
    successful_requests: u64,
    failed_requests: u64,
    success_rate: f64,
    requests_per_second: f64,
    bytes_sent: u64,
    bytes_received: u64,
    latency: JsonLatencyStats,
    errors: Vec<String>,
}

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct JsonLatencyStats {
    min_ms: f64,
    max_ms: f64,
    mean_ms: f64,
    p50_ms: f64,
    p95_ms: f64,
    p99_ms: f64,
    p999_ms: f64,
}

impl JsonReport {
    fn from_metrics(metrics: &LoadTestMetrics) -> Self {
        let stats = metrics.latency_stats();

        Self {
            duration_secs: metrics.duration().map(|d| d.as_secs_f64()).unwrap_or(0.0),
            total_requests: metrics.total_requests,
            successful_requests: metrics.successful_requests,
            failed_requests: metrics.failed_requests,
            success_rate: metrics.success_rate(),
            requests_per_second: metrics.requests_per_second(),
            bytes_sent: metrics.bytes_sent,
            bytes_received: metrics.bytes_received,
            latency: JsonLatencyStats {
                min_ms: stats.min.as_secs_f64() * 1000.0,
                max_ms: stats.max.as_secs_f64() * 1000.0,
                mean_ms: stats.mean.as_secs_f64() * 1000.0,
                p50_ms: stats.p50.as_secs_f64() * 1000.0,
                p95_ms: stats.p95.as_secs_f64() * 1000.0,
                p99_ms: stats.p99.as_secs_f64() * 1000.0,
                p999_ms: stats.p999.as_secs_f64() * 1000.0,
            },
            errors: metrics.errors.clone(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;
    use tempfile::TempDir;

    #[test]
    fn test_json_report_generation() {
        let temp_dir = TempDir::new().unwrap();
        let report_path = temp_dir.path().join("report.json");

        let mut metrics = LoadTestMetrics::new();
        metrics.record_success(Duration::from_millis(10), 100, 50);
        metrics.record_success(Duration::from_millis(20), 100, 50);

        let result = Reporter::generate_json(&metrics, &report_path);
        assert!(result.is_ok());
        assert!(report_path.exists());

        let content = std::fs::read_to_string(&report_path).unwrap();
        assert!(content.contains("total_requests"));
        assert!(content.contains("latency"));
    }

    #[test]
    fn test_csv_report_generation() {
        let temp_dir = TempDir::new().unwrap();
        let report_path = temp_dir.path().join("report.csv");

        let mut metrics = LoadTestMetrics::new();
        metrics.record_success(Duration::from_millis(10), 100, 50);

        let result = Reporter::generate_csv(&metrics, &report_path);
        assert!(result.is_ok());
        assert!(report_path.exists());

        let content = std::fs::read_to_string(&report_path).unwrap();
        assert!(content.contains("metric,value"));
        assert!(content.contains("total_requests"));
    }

    #[test]
    fn test_html_report_generation() {
        let temp_dir = TempDir::new().unwrap();
        let report_path = temp_dir.path().join("report.html");

        let mut metrics = LoadTestMetrics::new();
        metrics.record_success(Duration::from_millis(10), 100, 50);

        let result = Reporter::generate_html(&metrics, &report_path);
        assert!(result.is_ok());
        assert!(report_path.exists());

        let content = std::fs::read_to_string(&report_path).unwrap();
        assert!(content.contains("<html>"));
        assert!(content.contains("Load Test Report"));
    }

    #[test]
    fn test_prometheus_metrics_generation() {
        let mut metrics = LoadTestMetrics::new();
        metrics.record_success(Duration::from_millis(10), 100, 50);
        metrics.record_success(Duration::from_millis(20), 100, 50);

        let prometheus = Reporter::generate_prometheus_metrics(&metrics);
        assert!(prometheus.contains("loadtest_total_requests"));
        assert!(prometheus.contains("loadtest_latency_seconds"));
        assert!(prometheus.contains("quantile"));
    }
}