diff --git a/tr/Cargo.toml b/tr/Cargo.toml index ea9f17c..d61a91b 100644 --- a/tr/Cargo.toml +++ b/tr/Cargo.toml @@ -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 diff --git a/tr/benches/my_bench.rs b/tr/benches/my_bench.rs new file mode 100644 index 0000000..7b402ad --- /dev/null +++ b/tr/benches/my_bench.rs @@ -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);