From be82a85d8c89c549108c6842c003a6b2c18b249e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Kohlgr=C3=BCber?= Date: Fri, 5 Aug 2022 19:25:48 +0200 Subject: [PATCH] Fix benchmarks --- Cargo.toml | 7 +++++ benches/bench.rs | 70 +++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 37 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1bc8cd2..8dc3a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,10 @@ edition = "2018" [dependencies] crc-catalog = "2.1.0" + +[dev-dependencies] +criterion = { version = "0.3", features = ["html_reports"] } + +[[bench]] +name = "bench" +harness = false \ No newline at end of file diff --git a/benches/bench.rs b/benches/bench.rs index 9363299..5cb72b1 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -1,6 +1,6 @@ use crc::*; use criterion::{criterion_group, criterion_main}; -use criterion::{Benchmark, Criterion, Throughput}; +use criterion::{Criterion, Throughput}; pub const BLUETOOTH: Crc = Crc::::new(&CRC_8_BLUETOOTH); pub const X25: Crc = Crc::::new(&CRC_16_IBM_SDLC); @@ -9,64 +9,60 @@ pub const GSM_40: Crc = Crc::::new(&CRC_40_GSM); pub const ECMA: Crc = Crc::::new(&CRC_64_ECMA_182); pub const DARC: Crc = Crc::::new(&CRC_82_DARC); +const INPUT_SIZE: usize = 1_000_000; + fn crc8(c: &mut Criterion) { let mut digest = BLUETOOTH.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc8", - Benchmark::new("crc8", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc8"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc8", move |b| b.iter(|| digest.update(&bytes))); } fn crc16(c: &mut Criterion) { let mut digest = X25.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc16", - Benchmark::new("crc16", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc16"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc16", move |b| b.iter(|| digest.update(&bytes))); } fn crc32(c: &mut Criterion) { let mut digest = CASTAGNOLI.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc32", - Benchmark::new("crc32", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc32"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc32", move |b| b.iter(|| digest.update(&bytes))); } fn crc40(c: &mut Criterion) { let mut digest = GSM_40.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc40", - Benchmark::new("crc40", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc40"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc40", move |b| b.iter(|| digest.update(&bytes))); } fn crc64(c: &mut Criterion) { let mut digest = ECMA.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc64", - Benchmark::new("crc64", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc64"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc64", move |b| b.iter(|| digest.update(&bytes))); } fn crc82(c: &mut Criterion) { let mut digest = ECMA.digest(); - let bytes = vec![0u8; 1_000_000]; - c.bench( - "crc82", - Benchmark::new("crc82", move |b| b.iter(|| digest.update(&bytes))) - .throughput(Throughput::Bytes(1_000_000)), - ); + let bytes = vec![0u8; INPUT_SIZE]; + let mut group = c.benchmark_group("crc82"); + group + .throughput(Throughput::Bytes(bytes.len() as u64)) + .bench_function("crc82", move |b| b.iter(|| digest.update(&bytes))); } criterion_group!(crc8_benches, crc8);