benchmark 0.8.0

Nanosecond-precision benchmarking for dev, testing, and production. Zero-overhead core timing when disabled; optional std-powered collectors and zero-dependency metrics (Watch/Timer) for real service observability.
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
//! A zero-dependency, high-performance time measurement library for Rust.
//!
//! This library provides nanosecond precision benchmarking with true zero-overhead
//! when disabled. Designed as a foundational primitive that other libraries can
//! depend on without concern for bloat, version conflicts, or performance impact.
//!
//! # Features
//!
//! - **Zero Dependencies**: Built using only the Rust standard library
//! - **True Zero Overhead**: When disabled, all code compiles away completely
//! - **Thread Safe**: Core functions are pure, with optional thread-safe collection
//! - **Async Compatible**: Works seamlessly with any async runtime
//! - **Simple API**: Just four functions and two macros
//!
//! # Quick Start
//!
//! ```rust
//! use benchmark::{measure, time};
//!
//! // Measure a function
//! let (result, duration) = measure(|| {
//!     // Some expensive computation
//!     42
//! });
//! println!("Computation took {} nanoseconds", duration.as_nanos());
//!
//! // Use the macro for convenience
//! fn expensive_function() -> i32 { 2 + 2 }
//! let (result, duration) = time!(expensive_function());
//! assert_eq!(result, 4);
//! ```
//!
//! # Production Metrics (feature = "metrics")
//!
//! The following examples compile only when `features = ["std", "metrics"]` are enabled.
//!
//! ```rust
//! # #[cfg(all(feature = "std", feature = "metrics"))]
//! use benchmark::{stopwatch, Watch};
//! #
//! # #[cfg(all(feature = "std", feature = "metrics"))]
//! fn main() {
//!     let watch = Watch::new();
//!     stopwatch!(watch, "work", {
//!         // do work
//!     });
//!     let s = &watch.snapshot()["work"];
//!     assert!(s.count >= 1);
//! }
//! # #[cfg(not(all(feature = "std", feature = "metrics")))]
//! # fn main() {}
//! ```

#![cfg_attr(not(feature = "std"), no_std)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![forbid(unsafe_code)]
#![deny(clippy::all)]
#![warn(clippy::pedantic)]
#![allow(clippy::module_name_repetitions)]
#![allow(clippy::must_use_candidate)]

// Core modules
#[cfg(feature = "collector")]
mod collector;
mod duration;
#[cfg(all(feature = "collector", feature = "metrics"))]
mod hist_backend;
#[cfg(all(feature = "collector", feature = "hdr"))]
mod hist_hdr;
#[cfg(feature = "collector")]
pub mod histogram;
mod measurement;
#[cfg(feature = "metrics")]
mod timer;
#[cfg(feature = "trace")]
mod trace;
#[cfg(feature = "metrics")]
mod watch;

// Public exports
#[cfg(feature = "collector")]
pub use collector::{Collector, Stats};
pub use duration::Duration;
pub use measurement::Measurement;
#[cfg(feature = "metrics")]
pub use timer::Timer;
#[cfg(feature = "metrics")]
pub use watch::{Watch, WatchBuilder, WatchStats};

// Re-export macros at crate root
#[doc(hidden)]
pub use crate as benchmark;

// Core timing functionality
#[cfg(feature = "benchmark")]
use std::time::Instant;

/// Measures the execution time of a function.
///
/// Returns a tuple of (result, duration) where result is the return value
/// of the function and duration is how long it took to execute.
///
/// # Examples
/// ```
/// use benchmark::measure;
///
/// let (result, duration) = measure(|| {
///     // Some computation
///     2 + 2
/// });
/// assert_eq!(result, 4);
/// # // Touch duration under benchmark to avoid lints and flakiness
/// # #[cfg(feature = "benchmark")]
/// # let _ = duration.as_nanos();
/// ```
#[cfg(feature = "benchmark")]
#[inline]
pub fn measure<T, F: FnOnce() -> T>(f: F) -> (T, Duration) {
    let start = Instant::now();
    let result = f();
    let duration = Duration::from_nanos(start.elapsed().as_nanos());
    (result, duration)
}

/// Measures the execution time of a function (disabled version).
#[cfg(not(feature = "benchmark"))]
#[inline]
pub fn measure<T, F: FnOnce() -> T>(f: F) -> (T, Duration) {
    (f(), Duration::ZERO)
}

/// Measures the execution time of a function with a name.
///
/// Returns a tuple of (result, measurement) where result is the return value
/// of the function and measurement contains the timing information.
///
/// # Examples
/// ```
/// use benchmark::measure_named;
///
/// let (result, measurement) = measure_named("computation", || {
///     // Some computation
///     2 + 2
/// });
/// assert_eq!(result, 4);
/// assert_eq!(measurement.name, "computation");
/// ```
#[cfg(all(feature = "benchmark", feature = "std"))]
#[inline]
pub fn measure_named<T, F: FnOnce() -> T>(name: &'static str, f: F) -> (T, Measurement) {
    #[cfg(miri)]
    let timestamp = 0;
    #[cfg(not(miri))]
    let timestamp = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0, |d| d.as_nanos());

    let start = Instant::now();
    let result = f();
    let duration = Duration::from_nanos(start.elapsed().as_nanos());

    let measurement = Measurement {
        name,
        duration,
        timestamp,
    };

    (result, measurement)
}

/// Measures the execution time of a function with a name (disabled version).
#[cfg(not(feature = "benchmark"))]
#[inline]
pub fn measure_named<T, F: FnOnce() -> T>(name: &'static str, f: F) -> (T, Measurement) {
    let measurement = Measurement {
        name,
        duration: Duration::ZERO,
        timestamp: 0,
    };
    (f(), measurement)
}

// Macros

/// Times an expression and returns (result, duration).
///
/// When features `benchmark` + `std` are active, the macro inlines timing using
/// `std::time::Instant` so it can be used inside async contexts (supports `await`).
///
/// # Examples
/// ```
/// use benchmark::time;
///
/// let (result, duration) = time!(2 + 2);
/// assert_eq!(result, 4);
/// ```
#[cfg(feature = "benchmark")]
#[macro_export]
macro_rules! time {
    ($expr:expr $(,)?) => {{
        let __start = ::std::time::Instant::now();
        let __out = { $expr };
        let __dur = $crate::Duration::from_nanos(__start.elapsed().as_nanos());
        (__out, __dur)
    }};
}

/// Times an expression and returns (result, duration) - disabled version.
#[cfg(not(feature = "benchmark"))]
#[macro_export]
macro_rules! time {
    ($expr:expr $(,)?) => {{
        ($expr, $crate::Duration::ZERO)
    }};
}

/// Times an expression with a name and returns (result, measurement).
///
/// When features `benchmark` + `std` are active, the macro inlines timing using
/// `std::time::Instant` so it can be used inside async contexts (supports `await`).
///
/// # Examples
/// ```
/// use benchmark::time_named;
///
/// let (result, measurement) = time_named!("addition", 2 + 2);
/// assert_eq!(result, 4);
/// assert_eq!(measurement.name, "addition");
/// ```
#[cfg(feature = "benchmark")]
#[macro_export]
macro_rules! time_named {
    ($name:expr, $expr:expr $(,)?) => {{
        let __name: &'static str = $name;
        let __start = ::std::time::Instant::now();
        let __out = { $expr };
        let __dur = $crate::Duration::from_nanos(__start.elapsed().as_nanos());
        #[cfg(miri)]
        let __ts = 0;
        #[cfg(not(miri))]
        let __ts = ::std::time::SystemTime::now()
            .duration_since(::std::time::UNIX_EPOCH)
            .map_or(0, |d| d.as_nanos());
        let __measurement = $crate::Measurement {
            name: __name,
            duration: __dur,
            timestamp: __ts,
        };
        (__out, __measurement)
    }};
}

/// Times an expression with a name and returns (result, measurement) - disabled version.
#[cfg(not(feature = "benchmark"))]
#[macro_export]
macro_rules! time_named {
    ($name:expr, $expr:expr $(,)?) => {{
        let measurement = $crate::Measurement {
            name: $name,
            duration: $crate::Duration::ZERO,
            timestamp: 0,
        };
        ($expr, measurement)
    }};
}

/// Stopwatch macro for production metrics collection.
///
/// When features `metrics` + `std` are active, this macro creates a `Timer`
/// which starts immediately before evaluating the body, and records the
/// duration when dropped at the end of the scope. Body may contain `await`.
///
/// Disabled path evaluates body with zero overhead.
#[cfg(feature = "metrics")]
#[macro_export]
macro_rules! stopwatch {
    ($watch:expr, $name:expr, { $($body:tt)* } $(,)?) => {{
        let __timer = $crate::Timer::new($watch.clone(), $name);
        { $($body)* }
    }};
}

/// Disabled version of `stopwatch!` when `metrics` is off.
#[cfg(not(all(feature = "metrics", feature = "std")))]
#[macro_export]
macro_rules! stopwatch {
    ($watch:expr, $name:expr, { $($body:tt)* } $(,)?) => {{
        { $($body)* }
    }};
}

/// Micro-benchmark a code block for a number of iterations and return raw per-iteration durations.
///
/// Two forms are supported:
/// - `benchmark_block!({ body })` uses a default of 10,000 iterations
/// - `benchmark_block!(iters, { body })` runs the block `iters` times
///
/// The block may contain `await` and arbitrary statements. When the `benchmark`
/// feature is disabled, the block executes once (to preserve side effects) and
/// the macro returns an empty `Vec` with zero timing overhead.
#[cfg(feature = "benchmark")]
#[macro_export]
macro_rules! benchmark_block {
    ({ $($body:tt)* } $(,)?) => {
        $crate::benchmark_block!(10_000usize, { $($body)* })
    };
    ($iters:expr, { $($body:tt)* } $(,)?) => {{
        let __iters: usize = $iters;
        let mut __samples: ::std::vec::Vec<$crate::Duration> = ::std::vec::Vec::with_capacity(__iters);
        let mut __i = 0usize;
        while __i < __iters {
            let __start = ::std::time::Instant::now();
            { $($body)* }
            let __dur = $crate::Duration::from_nanos(__start.elapsed().as_nanos());
            __samples.push(__dur);
            __i += 1;
        }
        __samples
    }};
}

/// Disabled version of `benchmark_block!` when `benchmark` is off.
#[cfg(not(feature = "benchmark"))]
#[macro_export]
macro_rules! benchmark_block {
    ({ $($body:tt)* } $(,)?) => {{
        { $($body)* }
        ::std::vec::Vec::<$crate::Duration>::new()
    }};
    ($iters:expr, { $($body:tt)* } $(,)?) => {{
        let _ = $iters; // keep param unused warnings away
        { $($body)* }
        ::std::vec::Vec::<$crate::Duration>::new()
    }};
}

/// Macro-benchmark an expression/function for a number of iterations and return
/// the last result together with raw per-iteration `Measurement`s.
///
/// Forms supported:
/// - `benchmark!(name, expr)` uses a default of 10,000 iterations
/// - `benchmark!(name, iters, expr)` runs `expr` `iters` times
/// - `benchmark!(name, { body })` and `benchmark!(name, iters, { body })` also work
///
/// The expression/body may contain `await`. When the `benchmark` feature is
/// disabled, the expression executes once and the macro returns `(Some(output), vec![])`
/// with zero timing overhead.
#[cfg(feature = "benchmark")]
#[macro_export]
macro_rules! benchmark {
    ($name:expr, { $($body:tt)* } $(,)?) => {
        $crate::benchmark!($name, 10_000usize, { $($body)* })
    };
    ($name:expr, $iters:expr, { $($body:tt)* } $(,)?) => {{
        let __name: &'static str = $name;
        let __iters: usize = $iters;
        let mut __measurements: ::std::vec::Vec<$crate::Measurement> = ::std::vec::Vec::with_capacity(__iters);
        let mut __last = None;
        let mut __i = 0usize;
        while __i < __iters {
            let __start = ::std::time::Instant::now();
            let __out = { $($body)* };
            let __dur = $crate::Duration::from_nanos(__start.elapsed().as_nanos());
            #[cfg(miri)]
            let __ts = 0;
            #[cfg(not(miri))]
            let __ts = ::std::time::SystemTime::now()
                .duration_since(::std::time::UNIX_EPOCH)
                .map_or(0, |d| d.as_nanos());
            __measurements.push($crate::Measurement { name: __name, duration: __dur, timestamp: __ts });
            __last = Some(__out);
            __i += 1;
        }
        (__last, __measurements)
    }};
    ($name:expr, $expr:expr $(,)?) => {
        $crate::benchmark!($name, 10_000usize, { $expr })
    };
    ($name:expr, $iters:expr, $expr:expr $(,)?) => {
        $crate::benchmark!($name, $iters, { $expr })
    };
}

/// Disabled version of `benchmark!` when `benchmark` is off.
#[cfg(not(feature = "benchmark"))]
#[macro_export]
macro_rules! benchmark {
    ($name:expr, { $($body:tt)* } $(,)?) => {{
        let _ = $name;
        let __out = { $($body)* };
        (Some(__out), ::std::vec::Vec::<$crate::Measurement>::new())
    }};
    ($name:expr, $iters:expr, { $($body:tt)* } $(,)?) => {{
        let _ = ($name, $iters);
        let __out = { $($body)* };
        (Some(__out), ::std::vec::Vec::<$crate::Measurement>::new())
    }};
    ($name:expr, $expr:expr $(,)?) => {{
        let _ = $name;
        let __out = $expr;
        (Some(__out), ::std::vec::Vec::<$crate::Measurement>::new())
    }};
    ($name:expr, $iters:expr, $expr:expr $(,)?) => {{
        let _ = ($name, $iters);
        let __out = $expr;
        (Some(__out), ::std::vec::Vec::<$crate::Measurement>::new())
    }};
}

// Intentionally no public trace! macro to avoid API surface area.
// Use internal crate::trace::record_event() behind the `trace` feature.