Skip to content

Commit a0319cc

Browse files
committed
test: add display bench
1 parent 5f599dc commit a0319cc

File tree

3 files changed

+240
-0
lines changed

3 files changed

+240
-0
lines changed

Cargo.lock

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ arbitrary = { version = "1", optional = true }
2424
serde = { version = "1", optional = true }
2525

2626
[dev-dependencies]
27+
divan = "0.1"
2728
quickcheck = "1"
2829
serde = { version = "1", features = ["derive"] }
2930
serde_json = "1"
3031
toml = "0.8"
3132

33+
[[bench]]
34+
name = "display"
35+
harness = false
36+
3237
[lints.rust]
3338
rust-2018-idioms = { level = "deny" }
3439
future-incompatible = { level = "deny" }

benches/display.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![allow(missing_docs)]
2+
3+
use std::{env, fmt, hint::black_box};
4+
5+
struct ByteSizeAlwaysPad(bytesize::ByteSize);
6+
7+
impl fmt::Display for ByteSizeAlwaysPad {
8+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
9+
f.pad(&self.0.display().to_string())
10+
}
11+
}
12+
13+
#[divan::bench]
14+
fn display_inner_display() {
15+
black_box(format!("{}", bytesize::ByteSize::kib(42).display()));
16+
}
17+
18+
#[divan::bench]
19+
fn display_bytesize_standard() {
20+
black_box(format!("{}", bytesize::ByteSize::kib(42).display()));
21+
}
22+
23+
#[divan::bench]
24+
fn display_bytesize_custom() {
25+
black_box(format!("|{:-^10}|", bytesize::ByteSize::kib(42)));
26+
}
27+
28+
#[divan::bench]
29+
fn display_always_pad_standard() {
30+
black_box(format!(
31+
"{}",
32+
ByteSizeAlwaysPad(bytesize::ByteSize::kib(42))
33+
));
34+
}
35+
36+
#[divan::bench]
37+
fn display_always_pad_custom() {
38+
black_box(format!(
39+
"|{:-^10}|",
40+
ByteSizeAlwaysPad(bytesize::ByteSize::kib(42))
41+
));
42+
}
43+
44+
fn main() {
45+
env::set_var("DIVAN_SAMPLE_COUNT", "1000");
46+
divan::main();
47+
}

0 commit comments

Comments
 (0)