@@ -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+
250270impl 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