use energy_bench::{EnergyBenchBuilder, Metadata}; use rand::distributions::{Distribution, Uniform}; struct SimpleLogger; impl log::Log for SimpleLogger { fn enabled(&self, metadata: &log::Metadata) -> bool { metadata.level() <= log::Level::Info } fn log(&self, record: &log::Record) { if self.enabled(record.metadata()) { println!("{} - {}", record.level(), record.args()); } } fn flush(&self) {} } const SIZE: i64 = 1000000; pub fn sorted() -> Vec { (0..SIZE).collect() } pub fn reversed() -> Vec { (0..SIZE).rev().collect() } pub fn uniform() -> Vec { let range = Uniform::new(i64::MIN, i64::MAX); let mut rng = rand::thread_rng(); (0..SIZE).map(|_| range.sample(&mut rng)).collect() } fn sort(vec: Vec) -> Vec { let mut vec = vec; vec.sort(); vec } struct SortMetadata { kind: String, size: usize, } impl Metadata<2> for SortMetadata { fn get_header() -> [&'static str; 2] { ["Kind", "Size"] } fn get_values(&self) -> [String; 2] { [self.kind.to_string(), self.size.to_string()] } } fn main() { static LOGGER: SimpleLogger = SimpleLogger; log::set_logger(&LOGGER) .map(|()| log::set_max_level(log::LevelFilter::Info)) .unwrap(); let mut bench = EnergyBenchBuilder::new("sort") .with_idle_duration(10) .with_number_of_measurements(5) .build(); bench.benchmark(SortMetadata { kind: "sorted".to_string(), size: 1000000 }, &sorted, &sort); bench.benchmark(SortMetadata { kind: "reversed".to_string(), size: 1000000 }, &reversed, &sort); bench.benchmark(SortMetadata { kind: "uniform".to_string(), size: 1000000 }, &uniform, &sort); }