Skip to content

Commit

Permalink
test: add display bench
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Feb 13, 2025
1 parent 5f599dc commit 277aefe
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 0 deletions.
188 changes: 188 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,16 @@ arbitrary = { version = "1", optional = true }
serde = { version = "1", optional = true }

[dev-dependencies]
divan = "0.1"
quickcheck = "1"
serde = { version = "1", features = ["derive"] }
serde_json = "1"
toml = "0.8"

[[bench]]
name = "display"
harness = false

[lints.rust]
rust-2018-idioms = { level = "deny" }
future-incompatible = { level = "deny" }
Expand Down
47 changes: 47 additions & 0 deletions benches/display.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#![allow(missing_docs, clippy::incompatible_msrv)]

use std::{env, fmt, hint::black_box};

struct ByteSizeAlwaysPad(bytesize::ByteSize);

impl fmt::Display for ByteSizeAlwaysPad {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(&self.0.display().to_string())
}
}

#[divan::bench]
fn display_inner_display() {
black_box(format!("{}", bytesize::ByteSize::kib(42).display()));
}

#[divan::bench]
fn display_bytesize_standard() {
black_box(format!("{}", bytesize::ByteSize::kib(42).display()));
}

#[divan::bench]
fn display_bytesize_custom() {
black_box(format!("|{:-^10}|", bytesize::ByteSize::kib(42)));
}

#[divan::bench]
fn display_always_pad_standard() {
black_box(format!(
"{}",
ByteSizeAlwaysPad(bytesize::ByteSize::kib(42))
));
}

#[divan::bench]
fn display_always_pad_custom() {
black_box(format!(
"|{:-^10}|",
ByteSizeAlwaysPad(bytesize::ByteSize::kib(42))
));
}

fn main() {
env::set_var("DIVAN_SAMPLE_COUNT", "1000");
divan::main();
}

0 comments on commit 277aefe

Please sign in to comment.