Skip to content

Commit

Permalink
[ISSUE #11]🍻Add bench test case (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsm authored Nov 6, 2024
1 parent 7448671 commit 6eea5b5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ serde = { version = "1.0", optional = true, default-features = false, features =
default = ["std"]
std = []
serde = ["serde/alloc"]
bytes = []
bytes = []

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }


[[bench]]
name = "cheetah"
harness = false
51 changes: 51 additions & 0 deletions benches/cheetah.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use cheetah_string::CheetahString;
use criterion::criterion_main;
use criterion::Criterion;
use criterion::{black_box, criterion_group};

fn criterion_benchmark(c: &mut Criterion) {
let cs1 = CheetahString::new();
c.bench_function("Empty CheetahString", |b| b.iter(|| black_box(cs1.clone())));
let s1 = String::new();
c.bench_function("empty string", |b| b.iter(|| black_box(s1.clone())));

let cs2 = CheetahString::from_static_str("Hello, World!");
c.bench_function("Static CheetahString", |b| {
b.iter(|| black_box(cs2.clone()))
});
let s2 = String::from("Hello, World!");
c.bench_function("Static string", |b| b.iter(|| black_box(s2.clone())));

let s_ = String::from("Hello, World!");
let cs3 = CheetahString::from_string(s_);
c.bench_function("String CheetahString", |b| {
b.iter(|| black_box(cs3.clone()))
});
let s3 = String::from("Hello, World!");
c.bench_function("String", |b| b.iter(|| black_box(s3.clone())));

for size in [
64,
128,
512,
4 * 1024,
16 * 1024,
64 * 1024,
512 * 1024,
1024 * 1024,
] {
let cs4 = CheetahString::from_string(String::from("h".repeat(size)));

c.bench_function(format!("{}B CheetahString", size).as_str(), |b| {
b.iter(|| black_box(cs4.clone()))
});

let s4 = String::from("a".repeat(size));
c.bench_function(format!("{}B std string", size).as_str(), |b| {
b.iter(|| black_box(s4.clone()))
});
}
}

criterion_group!(benches, criterion_benchmark);
criterion_main!(benches);

0 comments on commit 6eea5b5

Please sign in to comment.