speed-cli 1.0.0

Comprehensive multi-protocol network performance testing CLI (TCP, UDP, QUIC, HTTP/1.1, HTTP/2, h2c, HTTP/3)
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
use colored::*;
use eyre::{Result, WrapErr as _};
use std::fs;
use std::net::SocketAddr;
use std::sync::Arc;
use tokio_util::sync::CancellationToken;
use tracing::{error, info, trace};

use clap::Parser;
use speed_cli::ClientMode;
use speed_cli::cli::{Cli, Commands};
use speed_cli::constants::MAX_HTTP_UPLOAD_SIZE;
use speed_cli::control::{
    ControlServerConfig, EnabledProtocols, PortOverrides, ServerManifest, ServerRuntime,
    TestTransport, bind_all, perform_handshake, run_control_server,
};
use speed_cli::performance::http::HttpVersion;
use speed_cli::performance::http::client::run_http_test;
use speed_cli::performance::quic::client::run_quic_client;
use speed_cli::performance::suite::{SuiteConfig, default_connections, run_suite};
use speed_cli::performance::tcp::client::run_tcp_client;
use speed_cli::performance::udp::client::run_udp_client;
use speed_cli::report::{
    DEFAULT_TCP_READ_BUFFER, HttpTestConfig, QuicTestConfig, TcpTestConfig, TestReport,
    UdpTestConfig,
};
use speed_cli::utils::export::{export_report, export_report_html};
use speed_cli::utils::file::can_write;
use speed_cli::utils::import::import_report_cbor;
use speed_cli::utils::progress::with_progress_counter;
use speed_cli::utils::tls::TlsMaterial;

#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
    let cli = Cli::parse();
    speed_cli::utils::logging::init(cli.verbose, cli.quiet, cli.color);
    speed_cli::performance::engine::progress::set_enabled(!cli.quiet);
    trace!("Parsed CLI arguments: {cli:#?}");

    match cli.command {
        Commands::Client {
            server,
            control_port,
            duration,
            warmup,
            protocol,
            export,
            connections,
            test_type,
            test_sizes,
            chunk_size,
            accounting,
            target_rate_mbps,
        } => {
            if warmup >= duration {
                return Err(eyre::eyre!(
                    "--warmup ({warmup}s) must be less than --duration ({duration}s)"
                ));
            }
            let warmup = std::time::Duration::from_secs(warmup);
            let accounting = match accounting {
                speed_cli::cli::AccountingArg::Goodput => {
                    speed_cli::report::ThroughputAccounting::Goodput
                }
                speed_cli::cli::AccountingArg::Wire => {
                    speed_cli::report::ThroughputAccounting::Wire
                }
            };
            let target_rate_bps: u64 = target_rate_mbps.saturating_mul(1_000_000);

            let mode = protocol;

            // Report rows are keyed by payload size in insertion order; sort the
            // user-supplied sizes ascending so the report is deterministic
            // regardless of the order given on the command line. Empty stays
            // empty — defaults are applied downstream and are already ascending.
            let mut test_sizes = test_sizes;
            test_sizes.sort_unstable();

            // Verify export file path is writable.
            if let Some(export) = &export {
                if let Some(parent) = export.parent()
                    && !parent.as_os_str().is_empty()
                {
                    fs::create_dir_all(parent)?;
                }
                if !can_write(export)? {
                    return Err(eyre::eyre!(
                        "Export file is not writable: {}",
                        export.display()
                    ));
                }
            }

            // Control handshake: discover ports + verify compatibility.
            let handshake = perform_handshake(&server, control_port).await?;

            let transport = match mode {
                ClientMode::TCP => TestTransport::TcpRaw,
                ClientMode::UDP => TestTransport::UdpBlaster,
                ClientMode::QUIC => TestTransport::QuicRaw,
                ClientMode::HTTP1 => TestTransport::Http1,
                ClientMode::H2C => TestTransport::H2c,
                ClientMode::HTTP2 => TestTransport::Http2Tls,
                ClientMode::HTTP3 => TestTransport::Http3,
            };
            let (host, port) = handshake.endpoint(transport)?;

            let report: TestReport = match mode {
                ClientMode::TCP => {
                    let config = TcpTestConfig::new(
                        host,
                        Some(port),
                        duration,
                        connections,
                        test_type,
                        test_sizes,
                    )
                    .with_warmup(warmup)
                    .with_accounting(accounting);
                    run_tcp_client(config).await?
                }
                ClientMode::UDP => {
                    let config = UdpTestConfig::new(
                        host,
                        Some(port),
                        duration,
                        connections,
                        test_type,
                        test_sizes,
                    )
                    .with_warmup(warmup)
                    .with_accounting(accounting)
                    .with_target_rate_bps(target_rate_bps);
                    run_udp_client(config).await?
                }
                ClientMode::QUIC => {
                    let config = QuicTestConfig::new(
                        host,
                        Some(port),
                        duration,
                        connections,
                        test_type,
                        test_sizes,
                    )
                    .with_warmup(warmup)
                    .with_accounting(accounting);
                    run_quic_client(config).await?
                }
                ClientMode::HTTP1 | ClientMode::HTTP2 | ClientMode::H2C | ClientMode::HTTP3 => {
                    let http_version = match mode {
                        ClientMode::HTTP1 => HttpVersion::HTTP1,
                        ClientMode::HTTP2 => HttpVersion::HTTP2,
                        ClientMode::H2C => HttpVersion::H2C,
                        ClientMode::HTTP3 => HttpVersion::HTTP3,
                        _ => unreachable!(),
                    };
                    let config = HttpTestConfig::new(
                        host,
                        Some(port),
                        duration,
                        connections,
                        test_type,
                        test_sizes,
                        chunk_size,
                        http_version,
                    )
                    .with_warmup(warmup)
                    .with_accounting(accounting);
                    run_http_test(config).await?
                }
            };

            println!("{}", "Client test completed.".green().bold());
            println!("{report:#}");

            if let Some(export) = &export {
                with_progress_counter("Exporting test results", export_report(&report, export))
                    .await
                    .wrap_err_with(|| format!("exporting results to {}", export.display()))?;
                println!(
                    "{}",
                    format!("Results exported to {}", export.to_string_lossy()).cyan()
                );
            }
        }

        Commands::Server {
            all,
            protocols,
            bind,
            control_port,
            tcp_port,
            udp_port,
            http1_port,
            h2c_port,
            https_port,
            http3_port,
            quic_port,
            cert,
            key,
        } => {
            use speed_cli::cli::ServerProtocol;
            let has = |p: ServerProtocol| all || protocols.contains(&p);
            let enabled = EnabledProtocols {
                tcp: has(ServerProtocol::Tcp),
                udp: has(ServerProtocol::Udp),
                http: has(ServerProtocol::Http),
                https: has(ServerProtocol::Https),
                http3: has(ServerProtocol::Http3),
                quic: has(ServerProtocol::Quic),
            };
            if !enabled.tcp
                && !enabled.udp
                && !enabled.http
                && !enabled.https
                && !enabled.http3
                && !enabled.quic
            {
                return Err(eyre::eyre!(
                    "At least one protocol must be enabled. Use --all or --protocol <p>."
                ));
            }

            println!("{}", "Starting server mode...".blue().bold());

            // Resolve TLS material once; shared by HTTPS, HTTP/3, raw QUIC.
            let tls = match (cert, key) {
                (Some(cert), Some(key)) => {
                    if !cert.exists() || !key.exists() {
                        return Err(eyre::eyre!(
                            "Certificate and key files must exist: {} and {}",
                            cert.display(),
                            key.display()
                        ));
                    }
                    TlsMaterial::from_pem_files(&cert, &key)?
                }
                (None, None) => TlsMaterial::self_signed()?,
                _ => {
                    return Err(eyre::eyre!(
                        "Both --cert and --key must be specified together"
                    ));
                }
            };

            let cancel = CancellationToken::new();
            let rt = ServerRuntime {
                bind,
                enable_cors: true,
                max_upload_size: MAX_HTTP_UPLOAD_SIZE,
                buffer_size: DEFAULT_TCP_READ_BUFFER,
                tls,
            };
            let overrides = PortOverrides {
                tcp: tcp_port,
                udp: udp_port,
                http1: http1_port,
                h2c: h2c_port,
                https: https_port,
                http3: http3_port,
                quic: quic_port,
            };

            // Bind every test listener up front so the manifest carries
            // the real (often ephemeral) ports.
            let bound = bind_all(&rt, enabled, overrides).await?;
            let manifest = Arc::new(ServerManifest::new(bound.entries.clone()));

            println!(
                "{}",
                format!("Control endpoint: http://{bind}:{control_port}/manifest")
                    .green()
                    .bold()
            );
            for entry in &manifest.listeners {
                println!(
                    "  {:<7} -> {}:{}",
                    entry.transport.label().bright_white().bold(),
                    bind,
                    entry.port.to_string().yellow()
                );
            }

            let mut handles = bound.spawn(&rt, &cancel);

            // Control endpoint last, once the manifest is final.
            let control_addr = SocketAddr::new(bind, control_port);
            handles.push((
                "Control",
                tokio::spawn(run_control_server(
                    ControlServerConfig {
                        bind_addr: control_addr,
                        manifest,
                    },
                    cancel.clone(),
                )),
            ));

            // Signal handler: SIGINT (all platforms), SIGTERM on Unix.
            let cancel_for_signal = cancel.clone();
            tokio::spawn(async move {
                #[cfg(unix)]
                {
                    use tokio::signal::unix::{SignalKind, signal};
                    let mut sigterm = match signal(SignalKind::terminate()) {
                        Ok(s) => s,
                        Err(e) => {
                            error!("Failed to install SIGTERM handler: {e}");
                            return;
                        }
                    };
                    tokio::select! {
                        res = tokio::signal::ctrl_c() => {
                            if let Err(e) = res {
                                error!("ctrl_c handler error: {e}");
                                return;
                            }
                            println!("\n{}", "Received SIGINT, shutting down gracefully...".yellow().bold());
                        }
                        _ = sigterm.recv() => {
                            println!("\n{}", "Received SIGTERM, shutting down gracefully...".yellow().bold());
                        }
                    }
                }
                #[cfg(not(unix))]
                {
                    if let Err(e) = tokio::signal::ctrl_c().await {
                        error!("ctrl_c handler error: {e}");
                        return;
                    }
                    println!(
                        "\n{}",
                        "Received SIGINT, shutting down gracefully..."
                            .yellow()
                            .bold()
                    );
                }
                cancel_for_signal.cancel();
            });

            println!(
                "{}",
                format!(
                    "Started servers: {}",
                    handles
                        .iter()
                        .map(|(name, _)| *name)
                        .collect::<Vec<_>>()
                        .join(", ")
                )
                .blue()
                .bold()
            );

            let results = futures::future::join_all(
                handles
                    .into_iter()
                    .map(|(name, handle)| async move { (name, handle.await) }),
            )
            .await;

            let mut any_failed = false;
            for (name, result) in results {
                match result {
                    Ok(Ok(())) => info!("{name} server completed successfully"),
                    Ok(Err(e)) => {
                        error!("{name} server failed: {e}");
                        any_failed = true;
                    }
                    Err(e) => {
                        error!("{name} server task panicked: {e}");
                        any_failed = true;
                    }
                }
            }
            if any_failed {
                return Err(eyre::eyre!("one or more server listeners failed"));
            }
        }

        Commands::Report { file, export_html } => {
            if !file.exists() {
                return Err(eyre::eyre!(
                    "Report file does not exist: {}",
                    file.display()
                ));
            }
            if !file.is_file() {
                return Err(eyre::eyre!("Report path is not a file: {}", file.display()));
            }
            let ext = file
                .extension()
                .and_then(|s| s.to_str())
                .map(|s| s.to_ascii_lowercase());
            match ext.as_deref() {
                Some("html") => {
                    return Err(eyre::eyre!(
                        "HTML reports are for browsers, not re-import: {}",
                        file.display()
                    ));
                }
                Some("cbor") | None => {
                    let report = with_progress_counter(
                        "Loading report from CBOR file",
                        import_report_cbor(&file),
                    )
                    .await?;
                    match export_html {
                        None => println!("{report:#}"),
                        Some(html_file) => {
                            with_progress_counter(
                                "Exporting report to HTML",
                                export_report_html(&report, &html_file),
                            )
                            .await
                            .wrap_err_with(|| {
                                format!("exporting HTML report to {}", html_file.display())
                            })?;
                            println!(
                                "{}",
                                format!("HTML report exported to {}", html_file.display()).cyan()
                            );
                        }
                    }
                }
                Some(other) => {
                    return Err(eyre::eyre!(
                        "Unsupported report extension `.{other}`: only `.cbor` is accepted ({})",
                        file.display()
                    ));
                }
            }
        }

        Commands::Suite {
            server,
            control_port,
            duration,
            warmup,
            connections,
            udp_target_rate_mbps,
            no_tls,
            accounting,
            export,
        } => {
            if warmup >= duration {
                return Err(eyre::eyre!(
                    "--warmup ({warmup}s) must be less than per-phase --duration ({duration}s)"
                ));
            }
            // Unset → auto-derive from the client's core count (capped).
            let connections = connections.unwrap_or_else(default_connections);
            let cfg = SuiteConfig {
                control_port,
                phase_duration: std::time::Duration::from_secs(duration),
                warmup: std::time::Duration::from_secs(warmup),
                connections,
                udp_target_rate_mbps,
                accounting: match accounting {
                    speed_cli::cli::AccountingArg::Goodput => {
                        speed_cli::report::ThroughputAccounting::Goodput
                    }
                    speed_cli::cli::AccountingArg::Wire => {
                        speed_cli::report::ThroughputAccounting::Wire
                    }
                },
                include_tls: !no_tls,
                // `server` plus the shared I/O-size defaults come from `new`.
                ..SuiteConfig::new(server)
            };

            if let Some(export) = &export {
                if let Some(parent) = export.parent()
                    && !parent.as_os_str().is_empty()
                {
                    fs::create_dir_all(parent)?;
                }
                if !can_write(export)? {
                    return Err(eyre::eyre!(
                        "Export file is not writable: {}",
                        export.display()
                    ));
                }
            }

            let suite = run_suite(cfg).await?;

            println!("{}", "Suite completed.".green().bold());
            println!("{suite}");

            if let Some(export) = &export {
                let mut buf = Vec::new();
                ciborium::into_writer(&suite, &mut buf)
                    .map_err(|e| eyre::eyre!("CBOR encode: {e}"))?;
                tokio::fs::write(export, &buf).await?;
                println!(
                    "{}",
                    format!("Suite report exported to {}", export.display()).cyan()
                );
            }
        }

        Commands::Completions { shell } => {
            use clap::CommandFactory as _;
            clap_complete::generate(
                shell,
                &mut Cli::command(),
                "speed-cli",
                &mut std::io::stdout(),
            );
        }

        Commands::Man { out_dir } => {
            use clap::CommandFactory as _;
            std::fs::create_dir_all(&out_dir)?;
            clap_mangen::generate_to(Cli::command(), &out_dir)?;
        }
    }

    Ok(())
}