statslicer 0.10.0

Guacamole provides a linearly-seekable random number generator.
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
#![doc = include_str!("../README.md")]

use std::cmp::Ordering;
use std::fmt::{Display, Formatter};
use std::path::PathBuf;
use std::str::FromStr;
use std::time::{Duration, Instant};

mod moments;
mod t_test;

pub use moments::Moments;
pub use t_test::{compute_difference, summarize};

/////////////////////////////////////////////// Type ///////////////////////////////////////////////

/// Type captures the type of a [Parameter].
#[derive(Debug, Eq, PartialEq, Ord, PartialOrd)]
pub enum Type {
    Unit,
    Integer,
    Float,
    Bool,
    Text,
}

impl Display for Type {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Self::Unit => write!(f, "unit"),
            Self::Integer => write!(f, "int"),
            Self::Float => write!(f, "float"),
            Self::Bool => write!(f, "bool"),
            Self::Text => write!(f, "text"),
        }
    }
}

impl FromStr for Type {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "unit" => Ok(Type::Unit),
            "int" => Ok(Type::Integer),
            "integer" => Ok(Type::Integer),
            "u64" => Ok(Type::Integer),
            "float" => Ok(Type::Float),
            "f64" => Ok(Type::Float),
            "bool" => Ok(Type::Bool),
            "text" => Ok(Type::Text),
            "str" => Ok(Type::Text),
            "string" => Ok(Type::Text),
            _ => Err(format!("invalid type: {s}")),
        }
    }
}

///////////////////////////////////////////// Parameter ////////////////////////////////////////////

/// Parameter binds a typed value to an experiment.
#[derive(Clone, Debug)]
pub enum Parameter {
    Unit,
    Integer(u64),
    Float(f64),
    Bool(bool),
    Text(String),
}

impl Parameter {
    pub fn ty(&self) -> Type {
        match self {
            Self::Unit => Type::Unit,
            Self::Integer(_) => Type::Integer,
            Self::Float(_) => Type::Float,
            Self::Bool(_) => Type::Bool,
            Self::Text(_) => Type::Text,
        }
    }

    pub fn cast_float(&self) -> Parameter {
        match self {
            Self::Integer(x) => Self::Float(*x as f64),
            Self::Float(x) => Self::Float(*x),
            Self::Unit => self.clone(),
            Self::Bool(_) => self.clone(),
            Self::Text(_) => self.clone(),
        }
    }
}

impl Display for Parameter {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Self::Unit => Ok(()),
            Self::Integer(i) => write!(fmt, "{i}"),
            Self::Float(f) => write!(fmt, "{f}"),
            Self::Bool(b) => write!(fmt, "{}", if *b { "true" } else { "false" }),
            Self::Text(t) => write!(fmt, "{t}"),
        }
    }
}

impl Eq for Parameter {}

impl PartialEq for Parameter {
    fn eq(&self, other: &Parameter) -> bool {
        self.cmp(other).is_eq()
    }
}

impl Ord for Parameter {
    fn cmp(&self, other: &Parameter) -> Ordering {
        let ty_cmp = self.ty().cmp(&other.ty());
        if !ty_cmp.is_eq() {
            ty_cmp
        } else {
            match (self, other) {
                (Self::Unit, Self::Unit) => Ordering::Equal,
                (Self::Integer(x), Self::Integer(y)) => x.cmp(y),
                (Self::Float(x), Self::Float(y)) => x.total_cmp(y),
                (Self::Bool(x), Self::Bool(y)) => x.cmp(y),
                (Self::Text(x), Self::Text(y)) => x.cmp(y),
                _ => unreachable!(),
            }
        }
    }
}

impl PartialOrd for Parameter {
    fn partial_cmp(&self, other: &Parameter) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl From<()> for Parameter {
    fn from(_: ()) -> Self {
        Self::Unit
    }
}

impl From<u64> for Parameter {
    fn from(x: u64) -> Self {
        Self::Integer(x)
    }
}

impl From<f64> for Parameter {
    fn from(x: f64) -> Self {
        Self::Float(x)
    }
}

impl From<bool> for Parameter {
    fn from(x: bool) -> Self {
        Self::Bool(x)
    }
}

impl From<String> for Parameter {
    fn from(x: String) -> Self {
        Self::Text(x)
    }
}

impl FromStr for Parameter {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.is_empty() {
            Ok(Parameter::Unit)
        } else if let Ok(i) = u64::from_str(s) {
            Ok(Parameter::Integer(i))
        } else if let Ok(f) = f64::from_str(s) {
            Ok(Parameter::Float(f))
        } else if s == "true" || s == "yes" {
            Ok(Parameter::Bool(true))
        } else if s == "false" || s == "no" {
            Ok(Parameter::Bool(false))
        } else {
            Ok(Parameter::Text(s.to_string()))
        }
    }
}

//////////////////////////////////////////// Parameters ////////////////////////////////////////////

/// Parameters provides the parameters for an experiment/benchmark.
pub trait Parameters: Default {
    /// Return the parameters as a list of name, parameter.
    fn params(&self) -> Vec<(&'static str, Parameter)>;

    /// Return the canonical parameter string for these parameters, suitable for passing to
    /// UntypedParameters::from_str.
    fn parameter_string(&self) -> String {
        let mut s = String::new();
        for (name, param) in self.params() {
            if !s.is_empty() {
                s.push(',');
            }
            s += &format!("{name}={param}");
        }
        s
    }
}

///////////////////////////////////////// UnboundParameters ////////////////////////////////////////

/// Unbound parameters convers from a string like foo,bar,baz,quux.
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct UnboundParameters {
    values: Vec<String>,
}

impl UnboundParameters {
    /// Whether there are parameters.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Number of bound parameters.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Iterate over the values.
    pub fn iter(&self) -> impl Iterator<Item = &'_ str> + '_ {
        self.values.iter().map(|s| s.as_str())
    }

    /// Has a parameter.
    pub fn has(&self, name: &str) -> bool {
        self.values.iter().any(|v| *v == name)
    }

    /// Project the provided UntypedParameters to an UntypedParameters that contains the same
    /// parameters as this struct, in order.
    pub fn project(&self, untyped: &UntypedParameters) -> Result<UntypedParameters, String> {
        let mut projected = vec![];
        for name in self.values.iter() {
            if let Some(p) = untyped.get(name) {
                projected.push((name.clone(), p));
            } else {
                return Err(format!(
                    "cannot unify {self} with {untyped}: missing {name}"
                ));
            }
        }
        Ok(UntypedParameters { values: projected })
    }

    /// Add the unbound parameter to this.
    pub fn push(&mut self, name: String) {
        self.values.push(name);
    }
}

impl Display for UnboundParameters {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        write!(fmt, "{}", self.values.join(","))
    }
}

impl From<&UntypedParameters> for UnboundParameters {
    fn from(params: &UntypedParameters) -> Self {
        let values = params.values.iter().map(|p| p.0.clone()).collect();
        Self { values }
    }
}

impl FromStr for UnboundParameters {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut values = vec![];
        for x in s.split(',') {
            if x.is_empty() {
                continue;
            }
            values.push(x.to_string())
        }
        Ok(Self { values })
    }
}

///////////////////////////////////////// UntypedParameters ////////////////////////////////////////

/// Untyped parameters converts from a string like foo=1,bar=3.14,baz=true,quux.  The input is
/// untyped, and the result is free-form, as opposed to a type like what implements [Parameters].
#[derive(Clone, Debug, Default, Eq, PartialEq, Ord, PartialOrd)]
pub struct UntypedParameters {
    values: Vec<(String, Parameter)>,
}

impl UntypedParameters {
    /// Create an untyped parameters list from one element.
    pub fn one(s: String, p: Parameter) -> Self {
        let values = vec![(s, p)];
        Self { values }
    }

    /// Whether there are parameters.
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Number of bound parameters.
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Iterate over the values.
    pub fn iter(&self) -> impl Iterator<Item = (String, Parameter)> + '_ {
        self.values.iter().cloned()
    }

    /// Get a parameter by name.
    pub fn get(&self, name: &str) -> Option<Parameter> {
        for v in self.values.iter() {
            if v.0 == name {
                return Some(v.1.clone());
            }
        }
        None
    }

    /// Cast all integer parameters to float, leaving the rest as-is.
    pub fn cast_float(&self) -> Self {
        let values = self
            .values
            .iter()
            .map(|p| (p.0.clone(), p.1.cast_float()))
            .collect();
        Self { values }
    }
}

impl Display for UntypedParameters {
    fn fmt(&self, fmt: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
        for (idx, value) in self.values.iter().enumerate() {
            if idx > 0 {
                write!(fmt, ",")?;
            }
            if value.1 == Parameter::Unit {
                write!(fmt, "{}", value.0)?;
            } else {
                write!(fmt, "{}={}", value.0, value.1)?;
            }
        }
        Ok(())
    }
}

impl FromStr for UntypedParameters {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut values = vec![];
        for x in s.split(',') {
            if x.is_empty() {
                continue;
            }
            if x.contains('=') {
                let pieces = x.splitn(2, '=').collect::<Vec<_>>();
                if pieces.len() != 2 {
                    return Err(format!("error parsing {x}: too many = signs"));
                }
                let name = pieces[0];
                let val = Parameter::from_str(pieces[1])
                    .map_err(|err| format!("could not parse parameter {}: {:?}", pieces[1], err))?;
                values.push((name.to_string(), val));
            } else {
                values.push((x.to_string(), Parameter::Unit));
            }
        }
        Ok(Self { values })
    }
}

///////////////////////////////////// experiment_and_parameters ////////////////////////////////////

pub fn experiment_and_parameters(s: &str) -> Result<(&str, UntypedParameters), String> {
    let pieces = s.rsplitn(2, ':').collect::<Vec<_>>();
    if pieces.len() != 2 {
        return Err(format!("don't know how to make cdf: {s}"));
    }
    let params = UntypedParameters::from_str(pieces[0]).expect("don't know how to make cdf");
    let pieces = pieces[1].rsplitn(2, '/').collect::<Vec<_>>();
    if pieces.is_empty() {
        return Err(format!("don't know how to make cdf: {s}"));
    }
    Ok((pieces[0], params))
}

///////////////////////////////////////////// black_box ////////////////////////////////////////////

/// Try to prevent the compiler from eliminating code.
// Copied from criterion under the Apache 2.0 or MIT licenses.
pub fn black_box<T>(dummy: T) -> T {
    unsafe {
        let ret = std::ptr::read_volatile(&dummy);
        std::mem::forget(dummy);
        ret
    }
}

/////////////////////////////////////////////// cycle //////////////////////////////////////////////

pub struct Cycle<T>(Vec<T>, usize);

impl<T> Cycle<T> {
    pub fn new(t: Vec<T>) -> Self {
        Self(t, 0)
    }
}

impl<T: Copy> Iterator for Cycle<T> {
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        if self.1 >= self.0.len() {
            self.1 = 0;
        }
        let answer = self.0.get(self.1).copied();
        self.1 += 1;
        answer
    }
}

///////////////////////////////////////// increment_indices ////////////////////////////////////////

pub fn increment_indices(indices: &mut [usize], limits: &[usize]) {
    assert_eq!(indices.len(), limits.len());
    for ((x, index), limit) in
        std::iter::zip(indices.iter_mut().enumerate().rev(), limits.iter().rev())
    {
        *index += 1;
        if x > 0 && *index >= *limit {
            *index = 0;
        } else {
            break;
        }
    }
}

//////////////////////////////////////////// benchmark! ////////////////////////////////////////////

/// A macro for defining a benchmark sweep.
#[macro_export]
macro_rules! benchmark {
    (name = $name:ident; $params:ident { $($param:ident in $set:expr,)* } $(,)? $bench:path $(,)?) => {
        fn $name(options: &$crate::BenchmarkOptions, filter: Option<String>) {
            let mut indices = vec![];
            let mut limits = vec![];
            $(
                indices.push(0);
                limits.push($set.iter().count());
                let $param = $set;
            )*
            while !indices.is_empty() && indices[0] < limits[0] {
                let mut count = 0;
                let mut params = $params::default();
                $(
                    params.$param = $param[indices[count]];
                    count += 1;
                )*
                $crate::increment_indices(&mut indices, &limits);
                let benchmark_name = format!("{}:{}", stringify!($name), params.parameter_string());
                if let Some(filter) = filter.as_ref() {
                    if !benchmark_name.contains(filter) {
                        continue;
                    }
                }
                if !options.quiet {
                    eprintln!("executing {}", benchmark_name);
                }
                $crate::benchmark_main(stringify!($name), options, &params, $bench);
            }
        }
    };
    (name = $name:ident; $params:ident { $($param:ident in $set:expr),* } $(,)? $bench:ident $(,)?) => {
        benchmark! { name = $name; $params { $($param in $set),* } $bench }
    };
}

////////////////////////////////////////// benchmark_main //////////////////////////////////////////

pub fn benchmark_main<P: Parameters, F: FnMut(&P, &mut Bencher)>(
    name: &str,
    options: &BenchmarkOptions,
    params: &P,
    mut f: F,
) {
    let output = if options.added_params.is_empty() {
        options.output_prefix.clone() + name + ":" + &params.parameter_string() + ".dat"
    } else {
        options.output_prefix.clone()
            + name
            + ":"
            + &params.parameter_string()
            + ","
            + &options.added_params
            + ".dat"
    };
    let output = PathBuf::from(output);
    let parent = output
        .parent()
        .map(PathBuf::from)
        .unwrap_or(PathBuf::from("."));
    if !parent.exists() {
        eprintln!(
            "output directory does not exist: {}",
            parent.to_string_lossy()
        );
        std::process::exit(1);
    }
    if options.noclobber && output.exists() {
        if !options.quiet {
            eprintln!("benchmark exists; moving on");
        }
        return;
    }
    let mut hist = sig_fig_histogram::Histogram::new(options.sig_figs);
    const SEED_FACTOR: u64 = 4294967291u64;
    let mut seed = SEED_FACTOR;
    let size = if let Some(benchmark_size) = options.benchmark_size.as_ref() {
        *benchmark_size
    } else {
        let mut size = 16;
        while size < usize::MAX {
            let mut b = Bencher::new(size, seed, false);
            seed = seed.wrapping_mul(SEED_FACTOR);
            f(params, &mut b);
            if b.elapsed > Duration::from_millis(options.target_time) {
                break;
            }
            size = size.saturating_add(size >> 2);
        }
        if size == usize::MAX {
            eprintln!("could not determine an appropriate size for this benchmark");
            std::process::exit(1);
        }
        size
    };
    if !options.quiet {
        eprintln!("sizing benchmark at {size}");
    }
    let warm_up = Duration::from_secs(options.warm_up);
    if !options.quiet {
        eprintln!("warming up for {}s", options.warm_up);
    }
    let start = Instant::now();
    while start.elapsed() < warm_up {
        let mut b = Bencher::new(size, seed, false);
        seed = seed.wrapping_mul(SEED_FACTOR);
        f(params, &mut b);
    }
    for i in 0..options.iterations {
        if !options.quiet
            && i > 0
            && options.iterations > 100
            && i % (options.iterations / 100) == 0
        {
            eprintln!(
                "done {} iterations ({}%)",
                i,
                i / (options.iterations / 100)
            );
        }
        let mut b = Bencher::new(size, seed, true);
        seed = seed.wrapping_mul(SEED_FACTOR);
        f(params, &mut b);
        let elapsed: u64 = (b.elapsed.as_nanos())
            .try_into()
            .expect("expect operations to take fewer than 834 days");
        assert!(elapsed < 1 << 55);
        hist.observe(elapsed as f64 / size as f64)
            .expect("histogram should never fail");
    }
    let output = std::fs::OpenOptions::new()
        .create(true)
        .truncate(true)
        .write(true)
        .open(output)
        .expect("output file should open");
    hist.dump(output).expect("histogram should always dump");
    if !options.quiet {
        eprintln!("done {} iterations (100%)", options.iterations);
    }
}

///////////////////////////////////////////// benchmark ////////////////////////////////////////////

/// A stub used to run the benchmarks, so that profilers can zero counters on entry and count them
/// on exit.
#[inline(never)]
pub fn benchmark<F: FnOnce()>(f: F) {
    f();
}

////////////////////////////////////////////// Bencher /////////////////////////////////////////////

pub struct Bencher {
    size: usize,
    seed: u64,
    real: bool,
    elapsed: Duration,
}

impl Bencher {
    fn new(size: usize, seed: u64, real: bool) -> Self {
        let elapsed = Duration::ZERO;
        Self {
            size,
            seed,
            real,
            elapsed,
        }
    }

    pub fn run<F: FnOnce()>(&mut self, f: F) {
        let start = Instant::now();
        if self.real {
            benchmark(f);
        } else {
            f();
        }
        self.elapsed = start.elapsed();
    }

    pub fn seed(&self) -> u64 {
        self.seed
    }

    pub fn size(&self) -> usize {
        self.size
    }
}

///////////////////////////////////////// BenchmarkOptions /////////////////////////////////////////

/// Options for the benchmark.
#[derive(Debug, Eq, PartialEq, arrrg_derive::CommandLine)]
pub struct BenchmarkOptions {
    /// Run the benchmark.
    #[arrrg(flag, "Run the benchmark.")]
    pub bench: bool,
    /// Run the benchmark quietly.
    #[arrrg(flag, "Run the benchmark quietly.")]
    pub quiet: bool,
    /// Do not overwrite existing results.
    #[arrrg(flag, "Do not overwrite existing results.")]
    pub noclobber: bool,
    /// A seed for random data.
    #[arrrg(optional, "Guacamole seed for random data.")]
    pub seed: u64,
    /// Number of seconds to spend warming up on the benchmark.
    #[arrrg(optional, "Seconds to spend warming up the benchmark.")]
    pub warm_up: u64,
    /// Number of milliseconds to target for each iteration.
    #[arrrg(optional, "Milliseconds to target for each iteration.")]
    pub target_time: u64,
    /// Number of iterations to execute.
    #[arrrg(optional, "Iterations to run.")]
    pub iterations: u64,
    /// Size of the benchmark (overrides --target-time).
    #[arrrg(optional, "Size of the benchmark.")]
    pub benchmark_size: Option<usize>,
    /// Number of significant figures to use in the output.
    #[arrrg(optional, "Significant figures to use.")]
    pub sig_figs: i32,
    /// Added parameters.
    #[arrrg(optional, "Added parameters.")]
    pub added_params: String,
    /// The output prefix for the benchmark.  Will be joined with
    /// "{benchmark_name}:{untyped_parameters}".
    #[arrrg(optional, "Output prefix for histograms.")]
    pub output_prefix: String,
}

impl Default for BenchmarkOptions {
    fn default() -> Self {
        Self {
            bench: false,
            quiet: false,
            noclobber: false,
            seed: 0,
            warm_up: 5,
            target_time: 100,
            iterations: 1000,
            benchmark_size: None,
            sig_figs: 3,
            added_params: "".to_string(),
            output_prefix: "exp/".to_string(),
        }
    }
}

////////////////////////////////////////// statslicer_main /////////////////////////////////////////

/// The macro for creating main functions.
#[macro_export]
macro_rules! statslicer_main {
    ($($name:ident),* $(,)?) => {
        fn main() {
            use arrrg::CommandLine;
            let mut args: Vec<String> = std::env::args().collect();
            if args.len() > 2 && args[args.len() - 1] == "--bench" {
                args.pop();
                args.insert(1, "--bench".to_string());
            }
            let usage = format!("USAGE: {} [PARAMETERS]", args[0]);
            let args: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
            let (params, mut free) =
                $crate::BenchmarkOptions::from_arguments_relaxed(&usage, &args[1..]);
            if free.len() > 1 {
                eprintln!("benchmark takes at most one positional argument");
                std::process::exit(1);
            }
            let filter = free.pop();
            if !params.bench {
                std::process::exit(0);
            }
            if !(1..=4).contains(&params.sig_figs) {
                eprintln!("significant figures must be [0, 5)");
                std::process::exit(1);
            }
            $($name(&params, filter.clone());)*
        }
    };
}

/////////////////////////////////////////////// tests //////////////////////////////////////////////

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn type_integer() {
        assert_eq!("int", Type::Integer.to_string());
        assert_eq!(Ok(Type::Integer), Type::from_str("int"));
        assert_eq!(Ok(Type::Integer), Type::from_str("integer"));
        assert_eq!(Ok(Type::Integer), Type::from_str("u64"));
    }

    #[test]
    fn type_float() {
        assert_eq!("float", Type::Float.to_string());
        assert_eq!(Ok(Type::Float), Type::from_str("float"));
        assert_eq!(Ok(Type::Float), Type::from_str("f64"));
    }

    #[test]
    fn type_bool() {
        assert_eq!("bool", Type::Bool.to_string());
        assert_eq!(Ok(Type::Bool), Type::from_str("bool"));
    }

    #[test]
    fn type_text() {
        assert_eq!("text", Type::Text.to_string());
        assert_eq!(Ok(Type::Text), Type::from_str("text"));
        assert_eq!(Ok(Type::Text), Type::from_str("str"));
        assert_eq!(Ok(Type::Text), Type::from_str("string"));
    }

    #[test]
    fn type_error() {
        assert!(Type::from_str("foo").is_err());
    }

    #[test]
    fn param_integer() {
        assert_eq!(Ok(Parameter::Integer(0)), Parameter::from_str("0"));
        assert_eq!(Ok(Parameter::Integer(42)), Parameter::from_str("42"));
    }

    #[test]
    #[allow(clippy::approx_constant)]
    fn param_float() {
        assert_eq!(Ok(Parameter::Float(3.14)), Parameter::from_str("3.14"));
        assert_eq!(Ok(Parameter::Float(2.72)), Parameter::from_str("2.72"));
    }

    #[test]
    fn param_bool() {
        assert_eq!(Ok(Parameter::Bool(true)), Parameter::from_str("true"));
        assert_eq!(Ok(Parameter::Bool(false)), Parameter::from_str("false"));
    }

    #[test]
    fn param_text() {
        assert_eq!(
            Ok(Parameter::Text("foo".to_string())),
            Parameter::from_str("foo")
        );
        assert_eq!(
            Ok(Parameter::Text("bar".to_string())),
            Parameter::from_str("bar")
        );
    }

    #[test]
    fn untyped_parameters() {
        assert_eq!(
            Ok(UntypedParameters {
                values: vec![
                    ("foo".to_string(), Parameter::Integer(42)),
                    ("bar".to_string(), Parameter::Float(2.72)),
                    ("baz".to_string(), Parameter::Bool(true)),
                    ("quux".to_string(), Parameter::Text("ins".to_string())),
                    ("zed".to_string(), Parameter::Unit)
                ]
            }),
            UntypedParameters::from_str("foo=42,bar=2.72,baz=true,quux=ins,zed")
        );
    }
}