harness 0.0.8

Precise and reproducible benchmarking
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
use std::convert::TryFrom;
use std::fmt;
use std::{
    cell::RefCell,
    path::PathBuf,
    sync::Mutex,
    time::{Duration, Instant},
};

use clap::Parser;

use crate::{
    probe::ProbeManager,
    record::{Record, StatPrintFormat},
};

#[derive(Parser, Debug)]
pub struct BenchArgs {
    #[arg(long, default_value = "false")]
    pub bench: bool,
    #[arg(short = 'n', long, default_value = "1")]
    /// Number of iterations to run
    pub iterations: usize,
    /// Enabled probes and their configurations, as a json string.
    #[arg(long, default_value = "{}")]
    pub probes: String,
    #[arg(long)]
    #[doc(hidden)]
    /// Overwrite benchmark name
    pub overwrite_benchmark_name: Option<String>,
    #[arg(long)]
    #[doc(hidden)]
    /// Overwrite crate name
    pub overwrite_crate_name: Option<String>,
    #[arg(long)]
    #[doc(hidden)]
    /// Specify current invocation
    pub current_invocation: Option<usize>,
    #[arg(long)]
    #[doc(hidden)]
    /// Append counter values to csv
    pub output_csv: Option<PathBuf>,
    #[arg(long)]
    #[doc(hidden)]
    /// Specify current build name
    pub current_build: Option<String>,
}

#[derive(Debug, Clone, Copy)]
pub enum Value {
    F64(f64),
    F32(f32),
    Usize(usize),
    Isize(isize),
    U64(u64),
    I64(i64),
    U32(u32),
    I32(i32),
    U16(u16),
    I16(i16),
    U8(u8),
    I8(i8),
    Bool(bool),
}

impl Value {
    pub(crate) fn into_string(self) -> String {
        match self {
            Value::F64(v) => v.to_string(),
            Value::F32(v) => v.to_string(),
            Value::Usize(v) => v.to_string(),
            Value::Isize(v) => v.to_string(),
            Value::U64(v) => v.to_string(),
            Value::I64(v) => v.to_string(),
            Value::U32(v) => v.to_string(),
            Value::I32(v) => v.to_string(),
            Value::U16(v) => v.to_string(),
            Value::I16(v) => v.to_string(),
            Value::U8(v) => v.to_string(),
            Value::I8(v) => v.to_string(),
            Value::Bool(v) => v.to_string(),
        }
    }
}

macro_rules! impl_helper_traits {
    ($variant: ident, $t:ty) => {
        impl From<$t> for Value {
            fn from(v: $t) -> Self {
                Value::$variant(v)
            }
        }

        impl TryFrom<Value> for $t {
            type Error = ();

            fn try_from(v: Value) -> Result<Self, Self::Error> {
                match v {
                    Value::$variant(v) => Ok(v),
                    _ => Err(()),
                }
            }
        }
    };
}

impl_helper_traits!(F64, f64);
impl_helper_traits!(F32, f32);
impl_helper_traits!(Usize, usize);
impl_helper_traits!(Isize, isize);
impl_helper_traits!(U64, u64);
impl_helper_traits!(I64, i64);
impl_helper_traits!(U32, u32);
impl_helper_traits!(I32, i32);
impl_helper_traits!(U16, u16);
impl_helper_traits!(I16, i16);
impl_helper_traits!(U8, u8);
impl_helper_traits!(I8, i8);
impl_helper_traits!(Bool, bool);

impl fmt::Display for Value {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.into_string())
    }
}

pub struct BenchTimer<'a> {
    start_time: std::time::Instant,
    bencher: &'a Bencher,
}

impl<'a> Drop for BenchTimer<'a> {
    fn drop(&mut self) {
        {
            let mut state = self.bencher.state.lock().unwrap();
            assert_eq!(*state, BencherState::Timing);
            *state = BencherState::AfterTiming;
        }
        let elapsed = self.start_time.elapsed();
        self.bencher.timing_end(elapsed);
        let mut lock = self.bencher.elapsed.lock().unwrap();
        assert!(lock.is_none(), "More than one benchmark timer detected");
        *lock = Some(elapsed);
    }
}

#[derive(Debug, PartialEq, Eq)]
enum BencherState {
    BeforeTiming,
    Timing,
    AfterTiming,
}

/// A handle to the benchmark runner
pub struct Bencher {
    bench: String,
    current_iteration: usize,
    max_iterations: usize,
    elapsed: Mutex<Option<Duration>>,
    probes: RefCell<ProbeManager>,
    extra_stats: Mutex<Vec<(String, Value)>>,
    state: Mutex<BencherState>,
}

impl Bencher {
    fn new(bench: String, max_iterations: usize) -> Self {
        Self {
            bench,
            current_iteration: 0,
            max_iterations,
            elapsed: Mutex::new(None),
            probes: RefCell::new(ProbeManager::new()),
            extra_stats: Mutex::new(Vec::new()),
            state: Mutex::new(BencherState::BeforeTiming),
        }
    }

    fn iter_start(&mut self, iteration: usize) {
        self.current_iteration = iteration;
        self.extra_stats.lock().unwrap().clear();
        *self.state.lock().unwrap() = BencherState::BeforeTiming;
        // Erase scratch directory
        let scratch_dir = &*crate::utils::HARNESS_BENCH_SCRATCH_DIR;
        if scratch_dir.exists() {
            std::fs::remove_dir_all(scratch_dir).unwrap();
        }
        std::fs::create_dir_all(scratch_dir).unwrap();
    }

    fn iter_end(&mut self) {
        assert_eq!(*self.state.lock().unwrap(), BencherState::AfterTiming);
    }

    fn timing_begin(&self) {
        let mut probes = self.probes.borrow_mut();
        probes.begin(
            &self.bench,
            self.current_iteration,
            !self.is_timing_iteration(),
        )
    }

    fn timing_end(&self, walltime: Duration) {
        let mut probes = self.probes.borrow_mut();
        probes.end(
            &self.bench,
            self.current_iteration,
            !self.is_timing_iteration(),
            walltime,
        )
    }

    /// Returns true if this is the last iteration
    pub fn is_timing_iteration(&self) -> bool {
        self.current_iteration == self.max_iterations - 1
    }

    /// Indicates the start of the timing phase. Should not be called more than once, or used the same time as `time`.
    ///
    /// Returns a `BenchTimer` object that will automatically stop the timer when it goes out of scope.
    ///
    /// # Example
    ///
    /// ```rust
    /// use harness::{bench, Bencher, black_box};
    ///
    /// const LEN: usize = 10000000;
    ///
    /// #[bench]
    /// fn example(bencher: &Bencher) {
    ///     // Prepare the inputs
    ///     let mut list = black_box((0..LEN).collect::<Vec<_>>());
    ///     // Actual work. For the last timing iteration only this part will be measured.
    ///     let result = {
    ///         let _timer = bencher.start_timing();
    ///         // Do some work here
    ///         list.iter().sum::<usize>()
    ///     };
    ///     // Release the resources and check the result
    ///     assert_eq!(result, LEN * (LEN - 1) / 2)
    /// }
    /// ```
    pub fn start_timing(&self) -> BenchTimer {
        {
            let mut state = self.state.lock().unwrap();
            if *state != BencherState::BeforeTiming {
                panic!("More than one benchmark timing phase detected");
            }
            assert_eq!(*state, BencherState::BeforeTiming);
            *state = BencherState::Timing;
        }
        self.timing_begin();
        BenchTimer {
            start_time: Instant::now(),
            bencher: self,
        }
    }

    /// Marks the whole timing phase. Should not be called more than once, or used the same time as `start_timing`.
    ///
    /// Returns the result of the closure.
    ///
    /// # Example
    ///
    /// ```rust
    /// use harness::{bench, Bencher, black_box};
    ///
    /// const LEN: usize = 10000000;
    ///
    /// #[bench]
    /// fn example(bencher: &Bencher) {
    ///     // Prepare the inputs
    ///     let mut list = black_box((0..LEN).collect::<Vec<_>>());
    ///     // Actual work. For the last timing iteration only this part will be measured.
    ///     let result = bencher.time(|| {
    ///         // Do some work here
    ///         list.iter().sum::<usize>()
    ///     });
    ///     // Release the resources and check the result
    ///     assert_eq!(result, LEN * (LEN - 1) / 2)
    /// }
    pub fn time<R, F: FnOnce() -> R>(&self, f: F) -> R {
        let _timer = self.start_timing();
        f()
    }

    /// Adds a custom statistic to the benchmark results
    ///
    /// Please ensure you are collecting the statistics in a cheap way during the timing phase,
    /// and call this function only after the timing phase.
    pub fn add_stat(&self, name: impl AsRef<str>, value: impl Into<Value>) {
        self.extra_stats
            .lock()
            .unwrap()
            .push((name.as_ref().to_owned(), value.into()));
    }

    /// Returns the wall-clock time of the last timing phase.
    /// Returns `None` if the timing phase has not finished yet.
    pub fn get_walltime(&self) -> Option<Duration> {
        *self.elapsed.lock().unwrap()
    }

    /// Returns the value of a counter.
    pub fn get_raw_counter_value(&self, name: impl AsRef<str>) -> Option<Value> {
        self.probes.borrow().get_value(name.as_ref())
    }
}

pub struct SingleBenchmarkRunner {
    args: BenchArgs,
    bench_name: String,
    crate_name: String,
    bencher: Bencher,
    benchmark: fn(&Bencher),
    is_single_shot: bool,
}

impl SingleBenchmarkRunner {
    #[doc(hidden)]
    pub fn new(fname: &str, benchmark: fn(&Bencher), is_single_shot: bool) -> Self {
        let args = BenchArgs::parse();
        let fname = std::path::PathBuf::from(fname);
        let name = fname.file_stem().unwrap().to_str().unwrap().to_owned();
        let bench_name = if let Some(n) = args.overwrite_benchmark_name.as_ref() {
            n.clone()
        } else {
            name
        };
        let crate_name = if let Some(n) = args.overwrite_crate_name.as_ref() {
            n.clone()
        } else {
            "harness".to_owned()
        };
        Self {
            args: BenchArgs::parse(),
            bench_name: bench_name.clone(),
            crate_name,
            bencher: Bencher::new(bench_name, if is_single_shot { 1 } else { args.iterations }),
            benchmark,
            is_single_shot,
        }
    }

    fn dump_counters(&self, iteration: usize, is_timing_iteration: bool) {
        let probe_stats = self
            .bencher
            .probes
            .borrow()
            .get_counter_values(std::mem::take(
                &mut *self.bencher.extra_stats.lock().unwrap(),
            ));
        let record = Record {
            name: &self.bench_name,
            csv: self.args.output_csv.as_ref(),
            invocation: self.args.current_invocation,
            build: self.args.current_build.as_ref(),
            format: StatPrintFormat::Yaml,
            iteration,
            is_timing_iteration,
            stats: probe_stats,
        };
        record.dump_values();
    }

    fn run_once_impl(&mut self, iteration: usize) -> f32 {
        self.bencher.iter_start(iteration);
        (self.benchmark)(&self.bencher);
        self.bencher.iter_end();
        // Return execution time
        let elapsed = self.bencher.elapsed.lock().unwrap().take();
        assert!(elapsed.is_some(), "No benchmark timer detected");
        let elapsed = elapsed.unwrap();
        elapsed.as_micros() as f32 / 1000.0
    }

    fn run_iterative(&mut self, iterations: usize) {
        for i in 0..iterations {
            let is_timing_iteration = i == iterations - 1;
            let (start_label, end_label) = if !is_timing_iteration {
                (
                    format!("warmup {} ", i + 1),
                    format!("completed warmup {}", i + 1),
                )
            } else {
                ("".to_owned(), "PASSED".to_owned())
            };
            eprintln!(
                "===== {} {} starting {}=====",
                self.crate_name, self.bench_name, start_label
            );
            let elapsed = self.run_once_impl(i);
            eprintln!(
                "===== {} {} {} in {:.1} msec =====",
                self.crate_name, self.bench_name, end_label, elapsed
            );
            self.dump_counters(i, is_timing_iteration);
        }
    }

    #[doc(hidden)]
    pub fn run(&mut self) -> anyhow::Result<()> {
        // Initialize probes
        self.bencher.probes.borrow_mut().init(&self.args.probes);
        // Run the benchmark
        let iterations = if self.is_single_shot {
            eprintln!("Harness: Single-shot run.");
            1
        } else {
            self.args.iterations
        };
        self.run_iterative(iterations);
        // Destroy probes
        self.bencher.probes.borrow_mut().deinit();
        Ok(())
    }
}