qubit-progress 0.8.3

Generic progress reporting abstractions for Qubit Rust libraries
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
// =============================================================================
//    Copyright (c) 2025 - 2026 Haixing Hu.
//
//    SPDX-License-Identifier: Apache-2.0
//
//    Licensed under the Apache License, Version 2.0.
// =============================================================================
//! Criterion benchmarks for the redesigned progress reporting paths.

use std::hint::black_box;
use std::sync::Arc;
use std::sync::Mutex;
use std::thread;
use std::time::Duration;

use criterion::BatchSize;
use criterion::BenchmarkId;
use criterion::Criterion;
use criterion::Throughput;
use criterion::criterion_group;
use criterion::criterion_main;
use qubit_progress::Metric;
use qubit_progress::NoopReporter;
use qubit_progress::Progress;
use qubit_progress::ReporterError;

/// Benchmarks the disabled report fast path.
fn bench_disabled_report(criterion: &mut Criterion) {
    let reporter = NoopReporter;
    let mut progress = Progress::builder(&reporter)
        .metric(Metric::new("entries", "Entries").total(1))
        .start()
        .expect("disabled progress must start");
    let metric = progress
        .metric("entries")
        .expect("configured metric must exist");
    metric.start(1).expect("metric update must succeed");
    metric.complete(1).expect("metric update must succeed");
    criterion.bench_function("disabled_report", |bencher| {
        bencher.iter(|| {
            progress.report().expect("disabled report must succeed");
        });
    });
}

/// Benchmarks an enabled running report with one fully configured metric.
fn bench_enabled_report(criterion: &mut Criterion) {
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    let mut progress = Progress::builder(&reporter)
        .metric(Metric::new("entries", "Entries").total(1))
        .start()
        .expect("enabled progress must start");
    let metric = progress
        .metric("entries")
        .expect("configured metric must exist");
    metric
        .start(black_box(1))
        .expect("metric update must succeed");
    metric.complete(1).expect("metric update must succeed");
    criterion.bench_function("enabled_report", |bencher| {
        bencher.iter(|| {
            progress.report().expect("enabled report must succeed");
        });
    });
}

/// Benchmarks the positive-interval path that skips an undued report closure.
fn bench_not_due_report(criterion: &mut Criterion) {
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    let mut progress = Progress::builder(&reporter)
        .interval(Duration::from_secs(60))
        .metric(Metric::new("entries", "Entries").total(1))
        .start()
        .expect("enabled progress must start");
    criterion.bench_function("not_due_report", |bencher| {
        bencher.iter(|| {
            progress
                .report_if_due()
                .expect("undued report must succeed");
        });
    });
}

/// Benchmarks creation and terminal delivery of a complete multi-metric event.
fn bench_multi_metric_terminal(criterion: &mut Criterion) {
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    criterion.bench_function("multi_metric_terminal", |bencher| {
        bencher.iter(|| {
            let progress = Progress::builder(&reporter)
                .metric(Metric::new("entries", "Entries").total(1))
                .metric(Metric::new("bytes", "Bytes").total(1024))
                .start()
                .expect("progress must start");
            progress
                .metric("entries")
                .expect("configured metric must exist")
                .start(1)
                .and_then(|()| {
                    progress
                        .metric("entries")
                        .expect("configured metric must exist")
                        .succeed(1)
                })
                .expect("metric update must succeed");
            progress
                .metric("bytes")
                .expect("configured metric must exist")
                .start(1024)
                .and_then(|()| {
                    progress
                        .metric("bytes")
                        .expect("configured metric must exist")
                        .complete(1024)
                })
                .expect("metric update must succeed");
            progress.finish().expect("terminal event must report");
        });
    });
}

/// Benchmarks status polling for a disabled automatic reporter.
fn bench_disabled_auto_reporter_status(criterion: &mut Criterion) {
    let reporter = NoopReporter;
    let mut progress = Progress::builder(&reporter)
        .metric(Metric::new("entries", "Entries"))
        .start()
        .expect("disabled progress must start");

    thread::scope(|scope| {
        let auto = progress.spawn_auto_reporter(scope);
        let status = auto.status();
        criterion.bench_function("disabled_auto_reporter_status", |bencher| {
            bencher.iter(|| black_box(status.is_failed()));
        });
        auto.stop().expect("inert reporter must stop cleanly");
    });
}

/// Benchmarks notification calls for a heartbeat-driven automatic reporter.
fn bench_heartbeat_auto_reporter_notification(criterion: &mut Criterion) {
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    let mut progress = Progress::builder(&reporter)
        .interval(Duration::from_secs(60))
        .metric(Metric::new("entries", "Entries"))
        .start()
        .expect("enabled progress must start");

    thread::scope(|scope| {
        let auto = progress.spawn_auto_reporter(scope);
        let notifier = auto.notifier();
        criterion.bench_function(
            "heartbeat_auto_reporter_notification",
            |bencher| {
                bencher.iter(|| notifier.notify());
            },
        );
        auto.stop().expect("heartbeat reporter must stop cleanly");
    });
}

/// Measures the lifecycle cost of one enabled automatic reporter thread.
fn bench_enabled_auto_reporter_spawn_stop(criterion: &mut Criterion) {
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    let mut progress = Progress::builder(&reporter)
        .interval(Duration::from_secs(60))
        .metric(Metric::new("entries", "Entries"))
        .start()
        .expect("enabled progress must start");

    criterion.bench_function("enabled_auto_reporter_spawn_stop", |bencher| {
        bencher.iter(|| {
            thread::scope(|scope| {
                let auto = progress.spawn_auto_reporter(scope);
                auto.stop().expect("auto reporter must stop cleanly");
            });
        });
    });
    progress.cancel().expect("terminal event must report");
}

/// Measures worker fan-in through one automatic reporter and one shared metric.
fn bench_auto_reporter_worker_fan_in(criterion: &mut Criterion) {
    const UPDATES_PER_WORKER: u64 = 256;
    let reporter = |event: &qubit_progress::Event| {
        black_box(event);
        Ok::<(), ReporterError>(())
    };
    let mut group = criterion.benchmark_group("auto_reporter_worker_fan_in");
    for workers in [1usize, 2, 4, 8, 16, 32, 64] {
        let total = UPDATES_PER_WORKER * workers as u64;
        group.throughput(Throughput::Elements(total));
        group.bench_with_input(
            BenchmarkId::new("auto_reporter_worker_fan_in", workers),
            &workers,
            |bencher, &workers| {
                bencher.iter_batched(
                    || {
                        let progress = Progress::builder(&reporter)
                            .interval(Duration::ZERO)
                            .metric(
                                Metric::new("entries", "Entries").total(total),
                            )
                            .start()
                            .expect("enabled progress must start");
                        let metric = progress
                            .metric("entries")
                            .expect("configured metric must exist");
                        metric.start(total).expect("metric start must succeed");
                        (progress, metric)
                    },
                    |(mut progress, metric)| {
                        thread::scope(|scope| {
                            let auto = progress.spawn_auto_reporter(scope);
                            let notifier = auto.notifier();
                            let mut handles = Vec::with_capacity(workers);
                            for _ in 0..workers {
                                let metric = metric.clone();
                                let notifier = notifier.clone();
                                handles.push(scope.spawn(move || {
                                    metric
                                        .complete(UPDATES_PER_WORKER)
                                        .expect("metric update must succeed");
                                    notifier.notify();
                                }));
                            }
                            for handle in handles {
                                handle.join().expect("worker must finish");
                            }
                            auto.stop()
                                .expect("auto reporter must stop cleanly");
                        });
                        black_box(metric.snapshot());
                    },
                    BatchSize::SmallInput,
                );
            },
        );
    }
    group.finish();
}

/// Benchmarks concurrent metric updates after setup has been removed from
/// timing.
fn bench_metric_handle_contention(criterion: &mut Criterion) {
    const UPDATES_PER_WORKER: u64 = 2048;
    let reporter = NoopReporter;
    let mut group = criterion.benchmark_group("metric_handle_contention");
    for workers in [1usize, 2, 4, 8, 16, 32, 64] {
        let total = UPDATES_PER_WORKER * workers as u64;
        group.throughput(Throughput::Elements(total));
        group.bench_with_input(
            BenchmarkId::new("metric_handle_contention", workers),
            &workers,
            |bencher, &workers| {
                bencher.iter_batched(
                    || {
                        let progress = Progress::builder(&reporter)
                            .metric(
                                Metric::new("entries", "Entries").total(total),
                            )
                            .start()
                            .expect("enabled progress must start");
                        let metric = progress
                            .metric("entries")
                            .expect("configured metric must exist");
                        metric.start(total).expect("metric start must succeed");
                        (progress, metric)
                    },
                    |(_progress, metric)| {
                        thread::scope(|scope| {
                            for _ in 0..workers {
                                let metric = metric.clone();
                                scope.spawn(move || {
                                    for _ in 0..UPDATES_PER_WORKER {
                                        metric.complete(1).expect(
                                            "metric update must succeed",
                                        );
                                    }
                                });
                            }
                        });
                        black_box(metric.snapshot());
                    },
                    BatchSize::SmallInput,
                );
            },
        );
    }
    group.finish();
}

/// Counts completed work for the mutex contention baseline.
#[derive(Debug)]
struct MutexMetricCounts {
    /// Configured total used by the same conservation check as `MetricCounts`.
    total: u64,
    /// Work that remains active.
    active: u64,
    /// Terminal work without an explicit classification.
    completed_unclassified: u64,
    /// Terminal work classified as successful.
    succeeded: u64,
    /// Terminal work classified as failed.
    failed: u64,
    /// Terminal work classified as cancelled.
    cancelled: u64,
}

impl MutexMetricCounts {
    /// Creates zeroed counts and applies the initial `start(total)` transition.
    fn new(total: u64) -> Self {
        let mut counts = Self {
            total,
            active: 0,
            completed_unclassified: 0,
            succeeded: 0,
            failed: 0,
            cancelled: 0,
        };
        counts.start(total);
        counts
    }

    /// Applies the same active-work increment used by `MetricHandle::start`.
    fn start(&mut self, count: u64) {
        self.active = self
            .active
            .checked_add(count)
            .expect("mutex metric active count must not overflow");
        self.validate();
    }

    /// Applies the same active-to-completed transition used by
    /// `MetricHandle::complete`.
    fn complete(&mut self, count: u64) {
        self.active = self
            .active
            .checked_sub(count)
            .expect("mutex metric active count must not underflow");
        self.completed_unclassified = self
            .completed_unclassified
            .checked_add(count)
            .expect("mutex metric completed count must not overflow");
        self.validate();
    }

    /// Checks conservation and the configured total after each transition.
    fn validate(&self) {
        let completed = self
            .completed_unclassified
            .checked_add(self.succeeded)
            .and_then(|value| value.checked_add(self.failed))
            .and_then(|value| value.checked_add(self.cancelled))
            .expect("mutex metric completed count must not overflow");
        let occupied = completed
            .checked_add(self.active)
            .expect("mutex metric occupied count must not overflow");
        assert!(
            occupied <= self.total,
            "mutex metric total must not be exceeded"
        );
    }

    /// Returns all counters in the same shape as a metric snapshot.
    fn snapshot(&self) -> (u64, u64, u64, u64, u64) {
        (
            self.active,
            self.completed_unclassified,
            self.succeeded,
            self.failed,
            self.cancelled,
        )
    }
}

/// Benchmarks a mutex-protected update path with setup removed from timing.
fn bench_mutex_metric_contention(criterion: &mut Criterion) {
    const UPDATES_PER_WORKER: u64 = 2048;
    let mut group = criterion.benchmark_group("mutex_metric_contention");
    for workers in [1usize, 2, 4, 8, 16, 32, 64] {
        let total = UPDATES_PER_WORKER * workers as u64;
        group.throughput(Throughput::Elements(total));
        group.bench_with_input(
            BenchmarkId::new("mutex_metric_contention", workers),
            &workers,
            |bencher, &workers| {
                bencher.iter_batched(
                    || Arc::new(Mutex::new(MutexMetricCounts::new(total))),
                    |state| {
                        thread::scope(|scope| {
                            for _ in 0..workers {
                                let state = Arc::clone(&state);
                                scope.spawn(move || {
                                    for _ in 0..UPDATES_PER_WORKER {
                                        let mut state = state
                                            .lock()
                                            .expect("mutex must not poison");
                                        state.complete(1);
                                    }
                                });
                            }
                        });
                        let state =
                            state.lock().expect("mutex must not poison");
                        black_box(state.snapshot());
                    },
                    BatchSize::SmallInput,
                );
            },
        );
    }
    group.finish();
}

criterion_group!(
    progress_benches,
    bench_disabled_report,
    bench_enabled_report,
    bench_not_due_report,
    bench_multi_metric_terminal,
    bench_disabled_auto_reporter_status,
    bench_heartbeat_auto_reporter_notification,
    bench_enabled_auto_reporter_spawn_stop,
    bench_auto_reporter_worker_fan_in,
    bench_metric_handle_contention,
    bench_mutex_metric_contention
);
criterion_main!(progress_benches);