Skip to content

Commit

Permalink
Add benchmarking with criterion
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigorc authored and ogoffart committed Aug 6, 2024
1 parent 9c4da7a commit 34a0b07
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
7 changes: 7 additions & 0 deletions tr/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,10 @@ default = ["gettext-rs"]
lazy_static = "1.2"
gettext-rs = { version = "0.7", optional = true, features = ["gettext-system"] }
gettext = { version = "0.4", optional = true }

[dev-dependencies]
criterion = "0.5"

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

pub fn short_literal(c: &mut Criterion) {
c.bench_function("short_literal", |b| b.iter(|| {
tr!("Hello");
}));
}

pub fn long_literal(c: &mut Criterion) {
c.bench_function("long_literal", |b| b.iter(|| {
tr!("Hello, world! This is a longer sentence but without argument markers. That is all for now, thank you for reading.");
}));
}

pub fn short_argument(c: &mut Criterion) {
c.bench_function("short_argument", |b| b.iter(|| {
tr!("Hello {}!", black_box("world"));
}));
}

pub fn long_argument(c: &mut Criterion) {
c.bench_function("long_argument", |b| b.iter(|| {
tr!("Hello {} and {} and {} and {} and {} and {} and {} and finally {}!",
black_box("Mercury"),
black_box("Venus"),
black_box("Earth"),
black_box("Mars"),
black_box("Jupiter"),
black_box("Saturn"),
black_box("Uranus"),
black_box("Neptune"),
);
}));
}

criterion_group!(benches, short_literal, long_literal, short_argument, long_argument);
criterion_main!(benches);

0 comments on commit 34a0b07

Please sign in to comment.