Skip to content

Commit 77fc224

Browse files
committed
fix(perf): report TCP benchmark metrics accurately
Signed-off-by: Drew Newberry <anewberry@nvidia.com>
1 parent 9721e67 commit 77fc224

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

‎crates/openshell-sandbox/src/bin/seccomp-perf.rs‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
//! Microbenchmark entry point for the production seccomp network broker.
55
6+
#[cfg(target_os = "linux")]
67
use std::net::SocketAddr;
78

9+
#[cfg(target_os = "linux")]
810
use clap::builder::{PossibleValue, PossibleValuesParser};
11+
#[cfg(target_os = "linux")]
912
use clap::{Parser, Subcommand, ValueEnum as _};
13+
#[cfg(target_os = "linux")]
1014
use openshell_sandbox::perf::{BenchmarkOptions, Layer, Protocol};
1115

16+
#[cfg(target_os = "linux")]
1217
#[derive(Debug, Parser)]
1318
#[command(
1419
about = "Measure native and seccomp-filtered socket performance",
@@ -33,6 +38,7 @@ struct Cli {
3338
command: Option<Command>,
3439
}
3540

41+
#[cfg(target_os = "linux")]
3642
fn protocol_value_parser() -> PossibleValuesParser {
3743
let mut values = vec![PossibleValue::new("all")];
3844
values.extend(
@@ -43,6 +49,7 @@ fn protocol_value_parser() -> PossibleValuesParser {
4349
PossibleValuesParser::new(values)
4450
}
4551

52+
#[cfg(target_os = "linux")]
4653
#[derive(Debug, Subcommand)]
4754
enum Command {
4855
#[command(hide = true)]
@@ -62,6 +69,7 @@ enum Command {
6269
},
6370
}
6471

72+
#[cfg(target_os = "linux")]
6573
fn main() -> anyhow::Result<()> {
6674
let cli = Cli::parse();
6775
if let Some(Command::Worker {
@@ -98,3 +106,8 @@ fn main() -> anyhow::Result<()> {
98106
}
99107
Ok(())
100108
}
109+
110+
#[cfg(not(target_os = "linux"))]
111+
fn main() -> anyhow::Result<()> {
112+
anyhow::bail!("the seccomp performance harness requires Linux")
113+
}

‎crates/openshell-sandbox/src/perf.rs‎

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,15 +238,35 @@ pub fn run_worker(
238238
capability_scope: protocol.capability_scope().to_string(),
239239
elapsed_ms: seconds * 1_000.0,
240240
operations_per_second: operations as f64 / seconds,
241-
throughput_mbit_per_second: operations as f64 * payload_bytes as f64 * 8.0
242-
/ seconds
243-
/ 1_000_000.0,
241+
throughput_mbit_per_second: throughput_mbit_per_second(
242+
protocol,
243+
operations,
244+
payload_bytes,
245+
seconds,
246+
),
244247
latency_ns_p50: percentile(&samples, 50),
245248
latency_ns_p95: percentile(&samples, 95),
246249
latency_ns_p99: percentile(&samples, 99),
247250
})
248251
}
249252

253+
/// Report application bytes transferred by the measured operation. A TCP
254+
/// stream operation writes one payload and reads its echo, so both directions
255+
/// count. A connect-only operation transfers no application payload.
256+
#[allow(clippy::cast_precision_loss)]
257+
fn throughput_mbit_per_second(
258+
protocol: Protocol,
259+
operations: u64,
260+
payload_bytes: usize,
261+
seconds: f64,
262+
) -> f64 {
263+
let bytes_per_operation = match protocol {
264+
Protocol::TcpConnect => 0,
265+
Protocol::TcpStream => payload_bytes.saturating_mul(2),
266+
};
267+
operations as f64 * bytes_per_operation as f64 * 8.0 / seconds / 1_000_000.0
268+
}
269+
250270
impl Protocol {
251271
const fn capability_scope(self) -> &'static str {
252272
match self {
@@ -326,4 +346,18 @@ mod tests {
326346
assert_eq!(percentile(&samples, 95), 95);
327347
assert_eq!(percentile(&samples, 99), 99);
328348
}
349+
350+
#[test]
351+
fn tcp_connect_never_reports_payload_throughput() {
352+
let small_payload = throughput_mbit_per_second(Protocol::TcpConnect, 1_000, 64, 1.0);
353+
let large_payload = throughput_mbit_per_second(Protocol::TcpConnect, 1_000, 65_507, 1.0);
354+
assert!(small_payload.abs() < f64::EPSILON);
355+
assert!(large_payload.abs() < f64::EPSILON);
356+
}
357+
358+
#[test]
359+
fn tcp_stream_counts_request_and_echo_bytes() {
360+
let throughput = throughput_mbit_per_second(Protocol::TcpStream, 1_000, 64, 1.0);
361+
assert!((throughput - 1.024).abs() < f64::EPSILON);
362+
}
329363
}

0 commit comments

Comments
 (0)