From d2561fc166dc56f2dd70bc087e29b76db2e90d3a Mon Sep 17 00:00:00 2001 From: Matteo Merli Date: Mon, 23 Nov 2026 16:37:35 -0800 Subject: [PATCH] pulsar-perf reports throughput and latency every 10 seconds, as Java's does `pulsar-perf produce` and `pulsar-perf consume` print a line every 10 seconds, in Java's format: the messages so far, the msg/s and Mbit/s since the last report, and the latency percentiles of an HdrHistogram of that interval, reset after each report. The producer's latency is the send's, past the warm-up; the consumer's runs from the publish time, in whole milliseconds. The reports come from a thread of their own, as Java's main thread prints them: a timer on the runtime cost the consume loop about 6% of its throughput. All output now goes through `tracing`, at INFO unless `RUST_LOG` says otherwise, with the client's own logs. --- Cargo.lock | 2 + docs/8-quality.md | 9 +- tools/perf/Cargo.toml | 2 + tools/perf/src/main.rs | 306 ++++++++++++++++++++++++++++++++++------- 4 files changed, 262 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 981a0cf..84fc162 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2899,6 +2899,8 @@ dependencies = [ "compio", "futures-util", "hdrhistogram", + "tracing", + "tracing-subscriber", ] [[package]] diff --git a/docs/8-quality.md b/docs/8-quality.md index 8b50ddd..6c8488a 100644 --- a/docs/8-quality.md +++ b/docs/8-quality.md @@ -111,10 +111,11 @@ Two harnesses measure the client, and a recorded baseline is what later rounds c batch of 1000 small JSON messages, and the consumer decompresses it and splits it into its messages. - **End to end**, `tools/perf`: this client's `pulsar-perf`, which `cargo build --release` builds. `pulsar-perf produce` and `pulsar-perf consume` measure the producer's and the Queue consumer's throughput and latency against a broker. - Their options, their payloads and their summary lines are those of Java's `pulsar-perf`, which drives the V5 client - for a `topic://` topic, so that the two compare line for line. `tools/perf/baseline.sh` runs both, each in a - container of its own on the broker's Docker network, against a standalone broker of its own for each run, both - bounded by a 64 MiB client memory limit, the only bound a V5 producer has. + Their options, their payloads, their reports every 10 seconds and their summary lines are those of Java's + `pulsar-perf`, which drives the V5 client for a `topic://` topic, so that the two compare line for line. + `tools/perf/baseline.sh` runs both, each in a container of its own on the broker's Docker network, against a + standalone broker of its own for each run, both bounded by a 64 MiB client memory limit, the only bound a V5 + producer has. Still to measure: loop counts, with producers spread over the thread-safe client's loops, and pulsar-rs, which has no scalable topics, so `persistent://` topics through the migration bridge, from a harness on tokio. An OpenMessaging diff --git a/tools/perf/Cargo.toml b/tools/perf/Cargo.toml index 29876b7..6212102 100644 --- a/tools/perf/Cargo.toml +++ b/tools/perf/Cargo.toml @@ -31,6 +31,8 @@ clap = { version = "4.6", features = ["derive"] } compio = { workspace = true, features = ["runtime", "time"] } futures-util = { version = "0.3", default-features = false, features = ["std", "async-await-macro"] } hdrhistogram = { version = "7.5", default-features = false } +tracing = "0.1" +tracing-subscriber = { version = "0.3", features = ["env-filter"] } [lints] workspace = true diff --git a/tools/perf/src/main.rs b/tools/perf/src/main.rs index 659a31a..5f960de 100644 --- a/tools/perf/src/main.rs +++ b/tools/perf/src/main.rs @@ -18,17 +18,21 @@ //! `pulsar-perf`: the producer's and the Queue consumer's throughput and latency against a broker (docs/8-quality.md //! ยง6). //! -//! Its options and its summary lines are those of Java's `pulsar-perf produce` and `pulsar-perf consume`, so that the -//! two clients compare line for line on the same broker: +//! Its options, its reports every 10 seconds and its summary lines are those of Java's `pulsar-perf produce` and +//! `pulsar-perf consume`, so that the two clients compare line for line on the same broker: //! //! ```text //! pulsar-perf produce -u pulsar://localhost:6650 -s 1024 -m 1000000 -r 5000000 topic://public/default/perf //! pulsar-perf consume -u pulsar://localhost:6650 -m 1000000 topic://public/default/perf //! ``` +use std::convert::Infallible; +use std::io::IsTerminal; use std::pin::pin; +use std::sync::mpsc::{self, RecvTimeoutError}; +use std::sync::{Arc, Mutex}; use std::task::Poll; -use std::time::{Duration, Instant}; +use std::time::{Duration, Instant, SystemTime}; use apache_pulsar::{ BatchingPolicy, CompressionPolicy, CompressionType, MessageId, Producer, ProducerBuilder, PulsarClient, @@ -38,10 +42,22 @@ use clap::{Parser, Subcommand, ValueEnum}; use futures_util::future::{Either, select}; use futures_util::stream::{FuturesUnordered, StreamExt}; use hdrhistogram::Histogram; +use tracing_subscriber::EnvFilter; +use tracing_subscriber::filter::LevelFilter; /// What the client counts against its memory limit for a message, besides its payload. const PER_MESSAGE_OVERHEAD: u64 = 1024; +/// How often a run prints its throughput and latency since the last report, as pulsar-perf does. +const REPORT_INTERVAL: Duration = Duration::from_secs(10); + +/// The highest send latency the histograms tell apart, in microseconds: a minute. +const MAX_SEND_LATENCY_MICROS: u64 = 60_000_000; + +/// The highest latency of a received message, from its publish time, the histogram tells apart, in milliseconds: ten +/// days, as pulsar-perf's. +const MAX_RECEIVE_LATENCY_MILLIS: u64 = 10 * 24 * 3600 * 1000; + #[derive(Parser)] #[command(about = "Throughput and latency of the producer and the Queue consumer, as Java's pulsar-perf measures them")] struct Args { @@ -144,6 +160,11 @@ impl Codec { fn main() { let args = Args::parse(); + // The client's logs and this tool's, at INFO unless `RUST_LOG` says otherwise. + tracing_subscriber::fmt() + .with_env_filter(EnvFilter::builder().with_default_directive(LevelFilter::INFO.into()).from_env_lossy()) + .with_ansi(std::io::stdout().is_terminal()) + .init(); let runtime = compio::runtime::Runtime::new().expect("a runtime"); let result = runtime.block_on(async { match args.command { @@ -152,7 +173,7 @@ fn main() { } }); if let Err(error) = result { - eprintln!("perf: {error}"); + tracing::error!("{error}"); std::process::exit(1); } } @@ -197,11 +218,11 @@ impl Produce { .send_timeout(Duration::ZERO) } - /// Sends the messages, and prints their throughput and latency. + /// Sends the messages, and prints their throughput and latency every 10 seconds and over the run. async fn drive(&self, producer: &Producer, Api>) { let payload = letters(self.size); let keys: Vec = (0..self.keys).map(|i| format!("key-{i}")).collect(); - let mut latencies = Histogram::::new_with_bounds(1, 60_000_000, 3).expect("a histogram"); + let mut latencies = Histogram::::new_with_bounds(1, MAX_SEND_LATENCY_MICROS, 3).expect("a histogram"); // As many in flight as the memory limit holds, by default: the limit binds as it does Java's V5 producer, // and the loop waits for completions, letting the producer's task run, where it would wait for room. let max_outstanding = match self.max_outstanding { @@ -212,60 +233,66 @@ impl Produce { let start = Instant::now(); let warmup = start + Duration::from_secs_f64(self.warmup_seconds); let end = (self.test_duration > 0).then(|| start + Duration::from_secs(self.test_duration)); + let report = Arc::new(Mutex::new(Report::new(start, MAX_SEND_LATENCY_MICROS, produced))); let mut sent = 0; let mut record = |(sent, latency, result): (Instant, Duration, apache_pulsar::Result)| { result.expect("a send failed"); - if sent >= warmup { - latencies.saturating_record(latency.as_micros() as u64); + let micros = (sent >= warmup).then_some(latency.as_micros() as u64); + if let Some(micros) = micros { + latencies.saturating_record(micros); } + report.lock().expect("the report").record(self.size, micros); }; - while (self.messages == 0 || sent < self.messages) && end.is_none_or(|end| Instant::now() < end) { - let i = sent; - sent += 1; - // Ahead of the rate: wait, taking completions meanwhile. - let at = (self.rate > 0).then(|| start + Duration::from_secs_f64(i as f64 / self.rate as f64)); - if let Some(at) = at.filter(|at| *at > Instant::now()) { - let mut sleep = pin!(compio::time::sleep_until(at)); - loop { - match select(sleep.as_mut(), in_flight.next()).await { - Either::Left(_) => break, - Either::Right((Some(done), _)) => record(done), - // Nothing in flight: only the time is left to wait for. - Either::Right((None, _)) => { - sleep.as_mut().await; - break; + reporting(&report, async { + while (self.messages == 0 || sent < self.messages) && end.is_none_or(|end| Instant::now() < end) { + let i = sent; + sent += 1; + // Ahead of the rate: wait, taking completions meanwhile. + let at = (self.rate > 0).then(|| start + Duration::from_secs_f64(i as f64 / self.rate as f64)); + if let Some(at) = at.filter(|at| *at > Instant::now()) { + let mut sleep = pin!(compio::time::sleep_until(at)); + loop { + match select(sleep.as_mut(), in_flight.next()).await { + Either::Left(_) => break, + Either::Right((Some(done), _)) => record(done), + // Nothing in flight: only the time is left to wait for. + Either::Right((None, _)) => { + sleep.as_mut().await; + break; + } } } } + while in_flight.len() >= max_outstanding { + record(in_flight.next().await.expect("sends in flight")); + } + let mut message = producer.new_message().value(payload.clone()); + if !keys.is_empty() { + message = message.key(keys[i as usize % keys.len()].clone()); + } + // Polled once at once, so that it is queued now, in call order. Its latency runs from there to its + // completion. + let mut send = Box::pin(async move { + let sent = Instant::now(); + let result = message.send().await; + (sent, sent.elapsed(), result) + }); + match futures_util::poll!(send.as_mut()) { + Poll::Ready(done) => record(done), + Poll::Pending => in_flight.push(send), + } } - while in_flight.len() >= max_outstanding { - record(in_flight.next().await.expect("sends in flight")); - } - let mut message = producer.new_message().value(payload.clone()); - if !keys.is_empty() { - message = message.key(keys[i as usize % keys.len()].clone()); - } - // Polled once at once, so that it is queued now, in call order. Its latency runs from there to its - // completion. - let mut send = Box::pin(async move { - let sent = Instant::now(); - let result = message.send().await; - (sent, sent.elapsed(), result) - }); - match futures_util::poll!(send.as_mut()) { - Poll::Ready(done) => record(done), - Poll::Pending => in_flight.push(send), + while let Some(done) = in_flight.next().await { + record(done); } - } - while let Some(done) = in_flight.next().await { - record(done); - } + }) + .await; let elapsed = start.elapsed().as_secs_f64(); let rate = sent as f64 / elapsed; let mbits = rate * self.size as f64 * 8.0 / 1024.0 / 1024.0; - println!("Aggregated throughput stats --- {sent} records sent --- {rate:.3} msg/s --- {mbits:.3} Mbit/s"); + tracing::info!("Aggregated throughput stats --- {sent} records sent --- {rate:.3} msg/s --- {mbits:.3} Mbit/s"); let ms = |micros: u64| micros as f64 / 1000.0; - println!( + tracing::info!( "Aggregated latency stats --- Latency: mean: {:7.3} ms - med: {:7.3} - 95pct: {:7.3} - 99pct: {:7.3} - \ 99.9pct: {:7.3} - 99.99pct: {:7.3} - Max: {:7.3}", latencies.mean() / 1000.0, @@ -304,19 +331,31 @@ impl Consume { .receiver_queue_size(self.receiver_queue_size) } - /// Receives and acknowledges the messages, and prints their throughput. + /// Receives and acknowledges the messages, and prints their throughput and latency every 10 seconds, and their + /// throughput over the run. async fn drive(&self, consumer: &QueueConsumer, Api>) -> apache_pulsar::Result<()> { let mut bytes = 0; let start = Instant::now(); - for _ in 0..self.messages { - let message = consumer.receive().await?; - bytes += message.size(); - consumer.acknowledge(message.id()).await?; - } + let report = Arc::new(Mutex::new(Report::new(start, MAX_RECEIVE_LATENCY_MILLIS, received))); + reporting(&report, async { + for _ in 0..self.messages { + let message = consumer.receive().await?; + bytes += message.size(); + // From the publish time, in whole milliseconds, as pulsar-perf's: none on a clock behind the broker's. + let millis = SystemTime::now() + .duration_since(message.publish_time()) + .ok() + .map(|latency| latency.as_millis() as u64); + report.lock().expect("the report").record(message.size(), millis); + consumer.acknowledge(message.id()).await?; + } + Ok::<_, apache_pulsar::Error>(()) + }) + .await?; let elapsed = start.elapsed().as_secs_f64(); let rate = self.messages as f64 / elapsed; let mbits = bytes as f64 * 8.0 / 1024.0 / 1024.0 / elapsed; - println!( + tracing::info!( "Aggregated throughput stats --- {} records received --- {rate:.3} msg/s --- {mbits:.3} Mbit/s", self.messages ); @@ -324,6 +363,121 @@ impl Consume { } } +/// What a run did since its last report, which it prints every 10 seconds, as pulsar-perf does. +struct Report { + /// The run's messages so far. + total: u64, + /// The messages since the last report, their bytes, and their latencies. + messages: u64, + bytes: u64, + latencies: Histogram, + /// When the last report was, or the run started. + last: Instant, + /// The report's line: of the run's messages so far, the messages and the Mbit per second since the last report, + /// and their latencies. + line: fn(u64, f64, f64, &Histogram) -> String, +} + +impl Report { + fn new(start: Instant, max_latency: u64, line: fn(u64, f64, f64, &Histogram) -> String) -> Self { + let latencies = Histogram::new_with_bounds(1, max_latency, 3).expect("a histogram"); + Report { total: 0, messages: 0, bytes: 0, latencies, last: start, line } + } + + /// A message done, of `bytes`, with its latency unless it is left out. + fn record(&mut self, bytes: usize, latency: Option) { + self.total += 1; + self.messages += 1; + self.bytes += bytes as u64; + if let Some(latency) = latency { + self.latencies.saturating_record(latency); + } + } + + /// When the next report falls due. + fn next(&self) -> Instant { + self.last + REPORT_INTERVAL + } + + /// The report's line, if it is due at `now`, which the next report counts from. + fn due(&mut self, now: Instant) -> Option { + let elapsed = now.saturating_duration_since(self.last); + if elapsed < REPORT_INTERVAL { + return None; + } + let seconds = elapsed.as_secs_f64(); + let rate = self.messages as f64 / seconds; + let mbits = self.bytes as f64 * 8.0 / 1024.0 / 1024.0 / seconds; + let line = (self.line)(self.total, rate, mbits, &self.latencies); + self.messages = 0; + self.bytes = 0; + self.latencies.reset(); + self.last = now; + Some(line) + } +} + +/// A producer's report, as pulsar-perf prints it, of latencies in microseconds. A failed send ends the run, so no +/// report has one. +fn produced(total: u64, rate: f64, mbits: f64, latencies: &Histogram) -> String { + let ms = |micros: u64| micros as f64 / 1000.0; + format!( + "Throughput produced: {total:7} msg --- {rate:8.1} msg/s --- {mbits:8.1} Mbit/s --- failure {:8.1} msg/s --- \ + Latency: mean: {:7.3} ms - med: {:7.3} - 95pct: {:7.3} - 99pct: {:7.3} - 99.9pct: {:7.3} - 99.99pct: {:7.3} \ + - Max: {:7.3}", + 0.0, + latencies.mean() / 1000.0, + ms(latencies.value_at_quantile(0.5)), + ms(latencies.value_at_quantile(0.95)), + ms(latencies.value_at_quantile(0.99)), + ms(latencies.value_at_quantile(0.999)), + ms(latencies.value_at_quantile(0.9999)), + ms(latencies.max()), + ) +} + +/// A consumer's report, as pulsar-perf prints it, of latencies in milliseconds. +fn received(total: u64, rate: f64, mbits: f64, latencies: &Histogram) -> String { + format!( + "Throughput received: {total:7} msg --- {rate:.3} msg/s --- {mbits:.3} Mbit/s --- Latency: mean: {:.3} ms - \ + med: {} - 95pct: {} - 99pct: {} - 99.9pct: {} - 99.99pct: {} - Max: {}", + latencies.mean(), + latencies.value_at_quantile(0.5), + latencies.value_at_quantile(0.95), + latencies.value_at_quantile(0.99), + latencies.value_at_quantile(0.999), + latencies.value_at_quantile(0.9999), + latencies.max(), + ) +} + +/// Runs `work` to its end, logging `report` every 10 seconds meanwhile, from a thread of its own, as pulsar-perf's +/// main thread does: a timer on the runtime would cost every turn of the work's loop, and would wait for the work to. +async fn reporting(report: &Arc>, work: impl Future) -> T { + let (done, finished) = mpsc::channel::(); + let reports = { + let report = Arc::clone(report); + std::thread::spawn(move || { + loop { + let next = report.lock().expect("the report").next(); + match finished.recv_timeout(next.saturating_duration_since(Instant::now())) { + Ok(never) => match never {}, + Err(RecvTimeoutError::Timeout) => { + if let Some(line) = report.lock().expect("the report").due(Instant::now()) { + tracing::info!("{line}"); + } + } + Err(RecvTimeoutError::Disconnected) => break, + } + } + }) + }; + let output = work.await; + drop(done); + reports.join().expect("the reports' thread"); + output +} + /// A size in bytes, with an optional `K`, `M` or `G` suffix of powers of 1024, as pulsar-perf's `-ml` takes. fn bytes(text: &str) -> Result { let (number, unit) = match text.char_indices().find(|(_, c)| !c.is_ascii_digit()) { @@ -366,6 +520,52 @@ mod tests { assert!(bytes("64X").is_err() && bytes("M").is_err()); } + #[test] + fn producers_report_every_10_seconds() { + let start = Instant::now(); + let mut report = Report::new(start, MAX_SEND_LATENCY_MICROS, produced); + for micros in [100, 200, 300, 400] { + report.record(128 * 1024, Some(micros)); + } + // In the warm-up: counted, without its latency. + report.record(128 * 1024, None); + assert_eq!(report.due(start + Duration::from_secs(9)), None); + assert_eq!( + report.due(start + Duration::from_secs(10)).as_deref(), + Some( + "Throughput produced: 5 msg --- 0.5 msg/s --- 0.5 Mbit/s --- failure 0.0 msg/s --- \ + Latency: mean: 0.250 ms - med: 0.200 - 95pct: 0.400 - 99pct: 0.400 - 99.9pct: 0.400 - \ + 99.99pct: 0.400 - Max: 0.400" + ) + ); + assert_eq!(report.due(start + Duration::from_secs(19)), None, "10 seconds from the last report"); + assert_eq!( + report.due(start + Duration::from_secs(20)).as_deref(), + Some( + "Throughput produced: 5 msg --- 0.0 msg/s --- 0.0 Mbit/s --- failure 0.0 msg/s --- \ + Latency: mean: 0.000 ms - med: 0.000 - 95pct: 0.000 - 99pct: 0.000 - 99.9pct: 0.000 - \ + 99.99pct: 0.000 - Max: 0.000" + ), + "an interval without messages" + ); + } + + #[test] + fn consumers_report_every_10_seconds() { + let start = Instant::now(); + let mut report = Report::new(start, MAX_RECEIVE_LATENCY_MILLIS, received); + for millis in [5, 10, 20] { + report.record(1024, Some(millis)); + } + assert_eq!( + report.due(start + Duration::from_secs(10)).as_deref(), + Some( + "Throughput received: 3 msg --- 0.300 msg/s --- 0.002 Mbit/s --- Latency: mean: 11.667 ms - \ + med: 10 - 95pct: 20 - 99pct: 20 - 99.9pct: 20 - 99.99pct: 20 - Max: 20" + ) + ); + } + #[test] fn payloads_are_uppercase_letters() { let payload = letters(1000);