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
/*
 * Copyright 2018 Akshay Nanavati <akshay.nanavati1@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#![feature(asm)]

extern crate clap;
#[macro_use]
extern crate lazy_static;

use clap::{App, Arg, ArgMatches};
use std::mem;
use std::sync::{Once, ONCE_INIT};
use std::time::{Duration, Instant};

const DEFAULT_DURATION_RUN_UNTIL: &str = "1000000000";
static HEADER: Once = ONCE_INIT;
lazy_static! {
    static ref CLI_CONFIG: ArgMatches<'static> = App::new("pew-benchmark")
        .version(env!("CARGO_PKG_VERSION"))
        .author(env!("CARGO_PKG_AUTHORS"))
        .about(env!("CARGO_PKG_DESCRIPTION"))
        .arg(
            Arg::with_name("filter")
                .short("f")
                .long("filter")
                .value_name("FILTER")
                .help("Only run benchmarks that contain this string")
                .takes_value(true),
        )
        .arg(
            Arg::with_name("run_until")
                .short("r")
                .long("run_until")
                .value_name("RUN_UNTIL")
                .help("Run benchmarks till this time (in ns) and then output average")
                .takes_value(true)
                .default_value(DEFAULT_DURATION_RUN_UNTIL),
        )
        .get_matches();
}

fn get_duration_run_until() -> u64 {
    CLI_CONFIG
        .value_of("run_until")
        .unwrap()
        .parse::<u64>()
        .unwrap()
}

fn should_run_bm(bm_name: &String) -> bool {
    match CLI_CONFIG.value_of("filter") {
        None => true,
        Some(s) => bm_name.contains(s),
    }
}

fn duration_as_nano(duration: &Duration) -> u64 {
    duration.as_secs() * 1_000_000_000 + duration.subsec_nanos() as u64
}

fn range_generator<T>(i: T) -> T {
    i
}

fn compose<T: 'static, U: 'static>(f: Box<Fn(u64) -> T>, g: fn(T) -> U) -> Box<Fn(u64) -> U> {
    Box::new(move |i: u64| g(f(i)))
}

/// The benchmark state
///
/// At a high level, it allows one to pause/resume the timer and also access an argument for this
/// run of the benchmark.
///
/// `T` will either be `u64` in the case a generator is not specified, or a user defined `T: Clone
/// + Default` if a generator(s) is defined (where `T` is the return type of the final specified
/// generator).
pub struct State<T> {
    instant: Instant,
    time_elapsed: Duration,
    is_paused: bool,
    input: T,
}

impl<T> State<T> {
    fn new(input: T) -> State<T> {
        State {
            instant: Instant::now(),
            time_elapsed: Duration::new(0, 0),
            is_paused: false,
            input: input,
        }
    }

    /// Pauses the benchmark timer. Useful to do any initialization work, etc.
    /// The state begins in a running (unpaused) state.
    ///
    /// # Examples
    ///
    /// ```
    /// use pew::{self, State};
    /// use std::vec::Vec;
    ///
    /// fn bm_simple(state: &mut State<u64>) {
    ///     state.pause();
    ///     let mut vec = Vec::new();
    ///     vec.push(1);
    ///     state.resume();
    ///     pew::do_not_optimize(vec.get(1));
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the state is already paused.
    pub fn pause(&mut self) {
        self.time_elapsed += self.instant.elapsed();
        if self.is_paused {
            panic!("Calling pause on an already paused benchmark");
        }
        self.is_paused = true;
    }

    /// Resumes the benchmark timer. Useful after any initialization work, etc.
    /// The state begins in a running (unpaused) state.
    ///
    /// #Examples
    ///
    /// ```
    /// use pew::{self, State};
    /// use std::vec::Vec;
    ///
    /// fn bm_simple(state: &mut State<u64>) {
    ///     state.pause();
    ///     let mut vec = Vec::new();
    ///     vec.push(1);
    ///     state.resume();
    ///     pew::do_not_optimize(vec.get(1));
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the state is already paused.
    pub fn resume(&mut self) {
        if !self.is_paused {
            panic!("Calling resume on an already running benchmark");
        }
        self.is_paused = false;
        self.instant = Instant::now();
    }

    fn finish(&self) -> Duration {
        let elapsed = self.instant.elapsed();
        if self.is_paused {
            panic!("Calling finish on a paused state");
        }
        self.time_elapsed + elapsed
    }
}

impl<T: Default> State<T> {
    /// Returns the input. Either `u64` (if no generator is specified) or a user specified
    /// `T: Clone + Default` where `T` is the return type of the last generator.
    ///
    /// # Examples
    ///
    /// ```
    /// use pew::{self, State};
    /// use std::vec::Vec;
    ///
    /// fn bm_simple(state: &mut State<Vec<u64>>) {
    ///     let mut vec = state.get_input();
    ///     let n = vec.len();
    ///     for i in 0..n {
    ///         vec.push(i as u64);
    ///     }
    /// }
    /// ```
    ///
    /// # Panics
    ///
    /// Panics if the state is paused.
    pub fn get_input(&mut self) -> T {
        self.pause();
        let input = mem::replace(&mut self.input, T::default());
        self.resume();
        input
    }
}

/// The main Benchmark struct
///
/// A benchmark consists of the following:
///
/// - `name: &'static str` - will be prefixed to the output (see `run`)
/// - One or more `bench: fn(&mut State<T>)` which are the actual benchmark. This is the actual
/// benchmark which is timed
/// - A range which consists of a `lower_bound`, an `upper_bound`, and a `mul` factor. Will run the
/// benchmark once for each `i in [lower_bound, upperbound]` such that `i` is initialized to
/// `lower_bound` and gets multiplied by `mul`. Defaults to:
///   - `lower_bound = 1`
///   - `upper_bound = 1 << 20`
///   - `mul = 2`
/// - `generator: fn(u64) -> T` (optional) -  rather than passing `i` from above as input to the
/// benchmark, T produced by this method is passed in instead. For each `i`, `gen(i)` is called
/// once for each `bench` in this `benchmark`. The result of this is then cloned and passed into
/// `bench` each time it is run.
///
/// # Examples
///
/// ```
/// #[macro_use]
/// extern crate pew;
/// use pew::Benchmark;
///
/// fn get_vec(n: u64) -> Vec<u64> {
///     let mut vec = Vec::new();
///     for i in 0..n {
///         vec.push(i as u64);
///     }
///     return vec;
/// }
///
/// fn bm_vector_gen(state: &mut pew::State<Vec<u64>>) {
///     let mut vec = state.get_input();
///     let n = vec.len() as u64;
///     for _ in 0..n {
///         pew::do_not_optimize(vec.pop());
///     }
/// }
///
/// fn main() {
///     Benchmark::with_name("range_bench")
///         .with_range(1 << 5, 1 << 5, 2)
///         .with_generator(get_vec)
///         .with_bench(pew_bench!(bm_vector_gen))
///         .run();
/// }
/// ```
///
/// This will output:
///
/// ```txt
/// Name,Time (ns)
/// gen_bench/f/1024,103674
/// gen_bench/f/4096,412499
/// gen_bench/f/16384,1634809
/// gen_bench/f/65536,7168879
/// gen_bench/f/262144,27419824
/// gen_bench/f/1048576,109010591
/// ```
///
/// When running the benchmark, you can pass the `--filter` flag. This will only run benchmarks
/// who's name contains the substring passed in to `--filter`.
///
/// See `examples/` for more examples.
pub struct Benchmark<T: 'static + Clone> {
    name: &'static str,
    fns: Vec<(&'static str, fn(&mut State<T>))>,
    range: (u64, u64, u64),
    generator: Box<Fn(u64) -> T>,
}

impl Benchmark<u64> {
    /// Generates a new benchmark with name
    pub fn with_name(name: &'static str) -> Self {
        Benchmark {
            name,
            fns: Vec::new(),
            range: (1, 1 << 20, 2),
            generator: Box::new(range_generator),
        }
    }
}

impl<T: Clone> Benchmark<T> {
    /// Sets the `lower_bound` for this benchmark
    pub fn with_lower_bound(mut self, lb: u64) -> Self {
        self.range.0 = lb;
        self
    }

    /// Sets the `upper_bound` for this benchmark
    pub fn with_upper_bound(mut self, ub: u64) -> Self {
        self.range.1 = ub;
        self
    }

    /// Sets the `mul` for this benchmark
    pub fn with_mul(mut self, mul: u64) -> Self {
        self.range.2 = mul;
        self
    }

    /// Sets the entire range for this benchmark
    pub fn with_range(mut self, lb: u64, ub: u64, mul: u64) -> Self {
        self.range = (lb, ub, mul);
        self
    }

    /// Sets a generator for this benchmark
    ///
    /// Multiple generators can be specified, each of which will be `fn(T) -> U`. These will be
    /// composed with the previous generators.
    ///
    /// Note that calling this function will wipe out all previously set `bench`es. Therefore, this
    /// function should be called before calling `with_bench`.
    pub fn with_generator<U: Clone>(self, gen: fn(T) -> U) -> Benchmark<U> {
        Benchmark {
            name: self.name,
            fns: Vec::new(),
            range: self.range,
            generator: compose(self.generator, gen),
        }
    }

    /// Specifies a benchmark method
    ///
    /// This must be called one or more times before calling `run`. All functions in this suite
    /// will have the same range and generator(s).
    ///
    /// This accepts a tuple with the benchmark name and the function. If the function is an ident
    /// and you want the benchmark name to match the function, use `pew_bench!`.
    pub fn with_bench(mut self, t: (&'static str, fn(&mut State<T>))) -> Self {
        self.fns.push(t);
        self
    }

    /// Runs the benchmark
    ///
    /// Prints the result as a csv with the following format:
    ///
    /// - Header which will be exactly `Name,Time(ns)` (this will be printed once for the whole
    /// program, not once per call to run).
    /// - Rows where
    ///   - `name` will be a slash separated concatenation of the benchmark name, the function
    ///   name, and i
    ///   - `time` will be the time in nanoseconds for running the benchmark
    ///
    /// # Panics
    ///
    /// Panics if no bench methods are specified.
    pub fn run(self) {
        if self.fns.len() == 0 {
            panic!("Cannot call run on an empty benchmark");
        }

        let (lb, ub, mul) = self.range;
        let mut i = lb;
        let gen = &self.generator;
        while i <= ub {
            let input = gen(i);
            for (name, f) in &self.fns {
                let bm_name = format!("{}/{}/{}", self.name, name, i);
                if should_run_bm(&bm_name) {
                    let mut runs = 0;
                    let mut total_duration = 0;
                    while total_duration < get_duration_run_until() {
                        let mut state = State::new(input.clone());
                        f(&mut state);
                        total_duration += duration_as_nano(&state.finish());
                        runs += 1;
                    }

                    HEADER.call_once(|| {
                        println!("Name,Time (ns)");
                    });

                    println!("{},{}", bm_name, total_duration / runs);
                }
                i *= mul;
            }
        }
    }
}

/// This method forces the compiler to not optimize the return statement of a benchmark.
///
/// # Examples
///
/// ``` use pew::{self, State};
///
/// fn bm_simple(state: &mut State<u64>) { pew::do_not_optimize(5 + 10); } ```
///
/// TODO: Conditionally compile this only on nightly so the rest of the benchmark crate can be used
/// in stable.
pub fn do_not_optimize<T>(val: T) {
    let mut _v = val;
    unsafe {
        asm!("" : "+r" (&_v) : : : "volatile");
    }
}

/// This method forces the compiler to not optimize writes to memory in
/// a benchmark.
///
/// # Examples
///
/// ```
/// use pew::{self, State};
/// use std::vec::Vec;
///
/// fn bm_simple(state: &mut State<u64>) {
///     let mut vec = Vec::new();
///     vec.push(1);
///     vec.push(2);
///     pew::clobber();
/// }
/// ```
///
/// TODO: Conditionally compile this only on nightly so the rest of the
/// benchmark crate can be used in stable.
pub fn clobber() {
    unsafe {
        asm!("" : : : "memory" : "volatile");
    }
}

/// A convenience macro for stringifying a benchmark function
///
/// Effectively turns an identifier `f` into `(stringify!(f), f)`.
///
/// The result of this should be passed into `Benchmark::<T>::with_bench`.
#[macro_export]
macro_rules! pew_bench {
    ($f:ident) => {
        (stringify!($f), $f)
    };
}