|
| 1 | +#[cfg(not(all(feature = "__bench_cycles", target_arch = "aarch64")))] |
| 2 | +use std::time::{Duration, Instant}; |
| 3 | + |
1 | 4 | #[cfg(feature = "__bench_codspeed")] |
2 | 5 | pub use codspeed_criterion_compat::*; |
3 | 6 | #[cfg(not(feature = "__bench_codspeed"))] |
4 | 7 | pub use criterion::*; |
| 8 | + |
| 9 | +#[cfg(all(feature = "__bench_cycles", target_arch = "aarch64"))] |
| 10 | +mod aarch64; |
| 11 | + |
| 12 | +// A measurement type for use with Criterion. |
| 13 | +// Depending on the features enabled, it wraps either Criterion's |
| 14 | +// standard `WallTime` metric or a target-specific measurement. |
| 15 | +pub struct CustomMeasurement( |
| 16 | + #[cfg(not(all(feature = "__bench_cycles", target_arch = "aarch64")))] measurement::WallTime, |
| 17 | + #[cfg(all(feature = "__bench_cycles", target_arch = "aarch64"))] aarch64::CycleCount, |
| 18 | +); |
| 19 | + |
| 20 | +impl measurement::Measurement for CustomMeasurement { |
| 21 | + #[cfg(not(all(feature = "__bench_cycles", target_arch = "aarch64")))] |
| 22 | + type Intermediate = Instant; |
| 23 | + |
| 24 | + #[cfg(not(all(feature = "__bench_cycles", target_arch = "aarch64")))] |
| 25 | + type Value = Duration; |
| 26 | + |
| 27 | + #[cfg(all(feature = "__bench_cycles", target_arch = "aarch64"))] |
| 28 | + type Intermediate = u64; |
| 29 | + |
| 30 | + #[cfg(all(feature = "__bench_cycles", target_arch = "aarch64"))] |
| 31 | + type Value = u64; |
| 32 | + |
| 33 | + fn start(&self) -> Self::Intermediate { |
| 34 | + self.0.start() |
| 35 | + } |
| 36 | + |
| 37 | + fn end(&self, i: Self::Intermediate) -> Self::Value { |
| 38 | + self.0.end(i) |
| 39 | + } |
| 40 | + |
| 41 | + fn add(&self, v1: &Self::Value, v2: &Self::Value) -> Self::Value { |
| 42 | + self.0.add(v1, v2) |
| 43 | + } |
| 44 | + |
| 45 | + fn zero(&self) -> Self::Value { |
| 46 | + self.0.zero() |
| 47 | + } |
| 48 | + |
| 49 | + fn to_f64(&self, value: &Self::Value) -> f64 { |
| 50 | + self.0.to_f64(value) |
| 51 | + } |
| 52 | + |
| 53 | + fn formatter(&self) -> &dyn measurement::ValueFormatter { |
| 54 | + self.0.formatter() |
| 55 | + //&CustomMeasurementFormatter |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +impl CustomMeasurement { |
| 60 | + #[allow(dead_code)] |
| 61 | + #[cfg(not(all(feature = "__bench_cycles", target_arch = "aarch64")))] |
| 62 | + pub fn new() -> Self { |
| 63 | + CustomMeasurement(measurement::WallTime) |
| 64 | + } |
| 65 | + |
| 66 | + #[allow(dead_code)] |
| 67 | + #[cfg(all(feature = "__bench_cycles", target_arch = "aarch64"))] |
| 68 | + pub fn new() -> Self { |
| 69 | + CustomMeasurement(aarch64::CycleCount) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/* |
| 74 | +struct CustomMeasurementFormatter; |
| 75 | +
|
| 76 | +impl measurement::ValueFormatter for CustomMeasurementFormatter { |
| 77 | + fn scale_values(&self, _typical_value: f64, _values: &mut [f64]) -> &'static str { |
| 78 | + "cycle" |
| 79 | + } |
| 80 | +
|
| 81 | + fn scale_throughputs(&self, _typical_value: f64, _throughput: &Throughput, _values: &mut [f64]) -> &'static str { |
| 82 | + "elem/sycle" |
| 83 | + } |
| 84 | +
|
| 85 | + fn scale_for_machines(&self, _values: &mut [f64]) -> &'static str { |
| 86 | + "cycle" |
| 87 | + } |
| 88 | +} |
| 89 | +*/ |
| 90 | + |
| 91 | +#[macro_export] |
| 92 | +macro_rules! custom_benchmark_group { |
| 93 | + ($name:ident, $( $target:path ),+ $(,)*) => { |
| 94 | + criterion_group!{ |
| 95 | + name = $name; |
| 96 | + config = Criterion::default().with_measurement(CustomMeasurement::new()); |
| 97 | + targets = $( $target ),+ |
| 98 | + } |
| 99 | + } |
| 100 | +} |
0 commit comments