| 1 |
use criterion::{ |
| 2 |
criterion_group, |
| 3 |
measurement::{Measurement, ValueFormatter}, |
| 4 |
Criterion, Throughput, |
| 5 |
}; |
| 6 |
use std::hint::black_box; |
| 7 |
use std::time::{Duration, Instant}; |
| 8 |
|
| 9 |
struct HalfSecFormatter; |
| 10 |
impl ValueFormatter for HalfSecFormatter { |
| 11 |
fn format_value(&self, value: f64) -> String { |
| 12 |
// The value will be in nanoseconds so we have to convert to half-seconds. |
| 13 |
format!("{} s/2", value * 2f64 * 10f64.powi(-9)) |
| 14 |
} |
| 15 |
|
| 16 |
fn format_throughput(&self, throughput: &Throughput, value: f64) -> String { |
| 17 |
match *throughput { |
| 18 |
Throughput::Bytes(bytes) | Throughput::BytesDecimal(bytes) => { |
| 19 |
format!("{} b/s/2", (bytes as f64) / (value * 2f64 * 10f64.powi(-9))) |
| 20 |
} |
| 21 |
Throughput::Bits(bits) => { |
| 22 |
format!( |
| 23 |
"{} bits/s/2", |
| 24 |
(bits as f64) / (value * 2f64 * 10f64.powi(-9)) |
| 25 |
) |
| 26 |
} |
| 27 |
Throughput::Elements(elems) => format!( |
| 28 |
"{} elem/s/2", |
| 29 |
(elems as f64) / (value * 2f64 * 10f64.powi(-9)) |
| 30 |
), |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
fn scale_values(&self, _typical: f64, values: &mut [f64]) -> &'static str { |
| 35 |
for val in values { |
| 36 |
*val *= 2f64 * 10f64.powi(-9); |
| 37 |
} |
| 38 |
|
| 39 |
"s/2" |
| 40 |
} |
| 41 |
|
| 42 |
fn scale_throughputs( |
| 43 |
&self, |
| 44 |
_typical: f64, |
| 45 |
throughput: &Throughput, |
| 46 |
values: &mut [f64], |
| 47 |
) -> &'static str { |
| 48 |
match *throughput { |
| 49 |
Throughput::Bytes(bytes) | Throughput::BytesDecimal(bytes) => { |
| 50 |
for val in values { |
| 51 |
*val = (bytes as f64) / (*val * 2f64 * 10f64.powi(-9)); |
| 52 |
} |
| 53 |
|
| 54 |
"b/s/2" |
| 55 |
} |
| 56 |
Throughput::Bits(bits) => { |
| 57 |
for val in values { |
| 58 |
*val = (bits as f64) / (*val * 2f64 * 10f64.powi(-9)); |
| 59 |
} |
| 60 |
|
| 61 |
"bits/s/2" |
| 62 |
} |
| 63 |
Throughput::Elements(elems) => { |
| 64 |
for val in values { |
| 65 |
*val = (elems as f64) / (*val * 2f64 * 10f64.powi(-9)); |
| 66 |
} |
| 67 |
|
| 68 |
"elem/s/2" |
| 69 |
} |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
fn scale_for_machines(&self, values: &mut [f64]) -> &'static str { |
| 74 |
for val in values { |
| 75 |
*val *= 2f64 * 10f64.powi(-9); |
| 76 |
} |
| 77 |
|
| 78 |
"s/2" |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
const NANOS_PER_SEC: u64 = 1_000_000_000; |
| 83 |
|
| 84 |
/// Silly "measurement" that is really just wall-clock time reported in half-seconds. |
| 85 |
struct HalfSeconds; |
| 86 |
impl Measurement for HalfSeconds { |
| 87 |
type Intermediate = Instant; |
| 88 |
type Value = Duration; |
| 89 |
|
| 90 |
fn start(&self) -> Self::Intermediate { |
| 91 |
Instant::now() |
| 92 |
} |
| 93 |
fn end(&self, i: Self::Intermediate) -> Self::Value { |
| 94 |
i.elapsed() |
| 95 |
} |
| 96 |
fn add(&self, v1: &Self::Value, v2: &Self::Value) -> Self::Value { |
| 97 |
*v1 + *v2 |
| 98 |
} |
| 99 |
fn zero(&self) -> Self::Value { |
| 100 |
Duration::from_secs(0) |
| 101 |
} |
| 102 |
fn to_f64(&self, val: &Self::Value) -> f64 { |
| 103 |
let nanos = val.as_secs() * NANOS_PER_SEC + u64::from(val.subsec_nanos()); |
| 104 |
nanos as f64 |
| 105 |
} |
| 106 |
fn formatter(&self) -> &dyn ValueFormatter { |
| 107 |
&HalfSecFormatter |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
fn fibonacci_slow(n: u64) -> u64 { |
| 112 |
match n { |
| 113 |
0 | 1 => 1, |
| 114 |
n => fibonacci_slow(n - 1) + fibonacci_slow(n - 2), |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
fn fibonacci_cycles(criterion: &mut Criterion<HalfSeconds>) { |
| 119 |
criterion.bench_function("fibonacci_custom_measurement", |bencher| { |
| 120 |
bencher.iter(|| fibonacci_slow(black_box(10))); |
| 121 |
}); |
| 122 |
} |
| 123 |
|
| 124 |
fn alternate_measurement() -> Criterion<HalfSeconds> { |
| 125 |
Criterion::default().with_measurement(HalfSeconds) |
| 126 |
} |
| 127 |
|
| 128 |
criterion_group! { |
| 129 |
name = benches; |
| 130 |
config = alternate_measurement(); |
| 131 |
targets = fibonacci_cycles |
| 132 |
} |