From 165d4ba9689d26a09c2e7fa4710b984c39b36540 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Sun, 12 May 2024 18:33:15 +0200 Subject: [PATCH] use bench groups and args --- .../benches/parse_cavage_header.rs | 13 ++--- lib/masto-id-convert/benches/process.rs | 14 ++--- lib/post-process/benches/simple.rs | 6 +-- lib/tick-tock-mock/benches/simple_now.rs | 51 +++++++++++-------- 4 files changed, 44 insertions(+), 40 deletions(-) diff --git a/lib/http-signatures/benches/parse_cavage_header.rs b/lib/http-signatures/benches/parse_cavage_header.rs index e3821d671..4ae4f3824 100644 --- a/lib/http-signatures/benches/parse_cavage_header.rs +++ b/lib/http-signatures/benches/parse_cavage_header.rs @@ -1,4 +1,4 @@ -use divan::{black_box, black_box_drop}; +use divan::black_box_drop; use http_signatures::cavage; #[global_allocator] @@ -7,14 +7,9 @@ static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); const CAVAGE_HEADER_1: &str = r#"keyId="Test",algorithm="rsa-sha256",headers="(request-target) host date",signature="qdx+H7PHHDZgy4y/Ahn9Tny9V3GP6YgBPyUXMmoxWtLbHpUnXS2mg2+SbrQDMCJypxBLSPQR2aAjn7ndmw2iicw3HMbe8VfEdKFYRqzic+efkb3nndiv/x1xSHDJWeSWkx3ButlYSuBskLu6kd9Fswtemr3lgdDEmn04swr2Os0=""#; const CAVAGE_HEADER_2: &str = r#"keyId="Test",algorithm="rsa-sha256",created=1402170695, expires=1402170699,headers="(request-target) (created) (expires) host date content-type digest content-length",signature="vSdrb+dS3EceC9bcwHSo4MlyKS59iFIrhgYkz8+oVLEEzmYZZvRs8rgOp+63LEM3v+MFHB32NfpB2bEKBIvB1q52LaEUHFv120V01IL+TAD48XaERZFukWgHoBTLMhYS2Gb51gWxpeIq8knRmPnYePbF5MOkR0Zkly4zKH7s1dE=""#; -#[divan::bench] -fn parse_cavage_header1() { - black_box_drop(cavage::parse(black_box(CAVAGE_HEADER_1))); -} - -#[divan::bench] -fn parse_cavage_header2() { - black_box_drop(cavage::parse(black_box(CAVAGE_HEADER_2))); +#[divan::bench(args = [CAVAGE_HEADER_1, CAVAGE_HEADER_2])] +fn parse_cavage_header(header: &str) { + black_box_drop(cavage::parse(header)); } fn main() { diff --git a/lib/masto-id-convert/benches/process.rs b/lib/masto-id-convert/benches/process.rs index 27c8d43b1..31174c667 100644 --- a/lib/masto-id-convert/benches/process.rs +++ b/lib/masto-id-convert/benches/process.rs @@ -1,18 +1,18 @@ #![allow(clippy::unreadable_literal)] -use divan::{black_box, black_box_drop}; +use divan::black_box_drop; #[global_allocator] static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); -#[divan::bench(name = "process integer 110368129515784116")] -fn process_u64() { - black_box_drop(masto_id_convert::process_u64(black_box(110368129515784116))); +#[divan::bench(name = "process integer", args = [110368129515784116])] +fn process_u64(id: u64) { + black_box_drop(masto_id_convert::process_u64(id)); } -#[divan::bench(name = "process ASCII 110368129515784116")] -fn process_ascii() { - black_box_drop(masto_id_convert::process(black_box("110368129515784116"))); +#[divan::bench(name = "process ASCII", args = ["110368129515784116"])] +fn process_ascii(id: &str) { + black_box_drop(masto_id_convert::process(id)); } fn main() { diff --git a/lib/post-process/benches/simple.rs b/lib/post-process/benches/simple.rs index b3e7fa7ec..0d5c9f999 100644 --- a/lib/post-process/benches/simple.rs +++ b/lib/post-process/benches/simple.rs @@ -7,10 +7,10 @@ static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); const SIMPLE_POST: &str = "Hello, #World! This is a benchmark for the post transformer of @kitsune"; -#[divan::bench] -fn simple_bench() -> post_process::Result { +#[divan::bench(args = [SIMPLE_POST])] +fn simple_bench(post: &str) -> post_process::Result { block_on(post_process::transform( - black_box(SIMPLE_POST), + post, black_box(|item| future::ready(Ok(item))), )) } diff --git a/lib/tick-tock-mock/benches/simple_now.rs b/lib/tick-tock-mock/benches/simple_now.rs index 4e705afd2..7ca2f9685 100644 --- a/lib/tick-tock-mock/benches/simple_now.rs +++ b/lib/tick-tock-mock/benches/simple_now.rs @@ -1,32 +1,41 @@ -use divan::{black_box, Bencher}; -use std::time::{Duration, SystemTime}; -use tick_tock_mock::{Clock, DeltaDirection}; - #[global_allocator] static GLOBAL: divan::AllocProfiler = divan::AllocProfiler::system(); -#[divan::bench] -fn std_systemtime_now() -> SystemTime { - black_box(SystemTime::now()) -} +#[divan::bench_group] +mod std { + use divan::black_box; + use std::time::SystemTime; -#[divan::bench] -fn ttm_now_tl() -> SystemTime { - black_box(tick_tock_mock::now()) + #[divan::bench] + fn systemtime_now() -> SystemTime { + black_box(SystemTime::now()) + } } -#[divan::bench] -fn ttm_now_simple(bencher: Bencher<'_, '_>) { - let clock = Clock::new(); - bencher.bench(|| black_box(clock.now())); -} +#[divan::bench_group] +mod tick_tock_mock { + use divan::{black_box, Bencher}; + use std::time::{Duration, SystemTime}; + use tick_tock_mock::{Clock, DeltaDirection}; + + #[divan::bench] + fn now_thread_local() -> SystemTime { + black_box(tick_tock_mock::now()) + } + + #[divan::bench] + fn now(bencher: Bencher<'_, '_>) { + let clock = Clock::new(); + bencher.bench(|| black_box(clock.now())); + } -#[divan::bench] -fn ttm_now_mock(bencher: Bencher<'_, '_>) { - let (clock, mock) = Clock::mockable(); - mock.adjust(DeltaDirection::Add, Duration::from_secs(1)); + #[divan::bench] + fn now_mocked(bencher: Bencher<'_, '_>) { + let (clock, mock) = Clock::mockable(); + mock.adjust(DeltaDirection::Add, Duration::from_secs(1)); - bencher.bench(|| black_box(clock.now())); + bencher.bench(|| black_box(clock.now())); + } } fn main() {