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
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(
clippy::all,
clippy::pedantic,
clippy::cargo,
rust_2018_idioms,
future_incompatible,
unused,
unused_lifetimes,
unused_qualifications,
unused_results,
anonymous_parameters,
deprecated_in_future,
elided_lifetimes_in_paths,
explicit_outlives_requirements,
keyword_idents,
macro_use_extern_crate,
missing_doc_code_examples,
private_doc_tests,
single_use_lifetimes,
trivial_casts,
trivial_numeric_casts,
unreachable_pub,
unsafe_code,
variant_size_differences
)]
#![cfg_attr(feature = "std", warn(missing_debug_implementations,))]
use criterion::{
AxisScale, Bencher, Criterion, ParameterizedBenchmark, PlotConfiguration, Throughput,
};
use lazy_static::lazy_static;
use num_cpus;
use rayon::ThreadPoolBuilder;
use std::convert::TryInto;
lazy_static! {
static ref THREADS: Vec<usize> = (0..=num_cpus::get().trailing_zeros())
.map(|log| 1_usize << log)
.collect();
}
pub fn log_size_bench<F>(crit: &mut Criterion, id: &str, sizes: &'static [usize], mut f: F)
where
F: FnMut(&mut Bencher<'_>, usize) + 'static,
{
let _ = crit.bench(
id,
ParameterizedBenchmark::new(id, move |bench, &&size| f(bench, size), sizes)
.sample_size(10)
.throughput(|&&s| Throughput::Elements(s.try_into().unwrap()))
.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)),
);
}
pub fn log_thread_bench<F>(crit: &mut Criterion, id: &str, size: usize, mut f: F)
where
F: FnMut(&mut Bencher<'_>) + 'static + Send,
{
let _ = crit.bench(
id,
ParameterizedBenchmark::new(
id,
move |bench, &&num_threads| {
let pool = ThreadPoolBuilder::new()
.num_threads(num_threads)
.build()
.expect("Building benchmark thread pool failed.");
pool.install(|| f(bench))
},
THREADS.iter(),
)
.sample_size(10)
.throughput(move |_| Throughput::Elements(size.try_into().unwrap()))
.plot_config(PlotConfiguration::default().summary_scale(AxisScale::Logarithmic)),
);
}