Merge pull request #12 from icetemple/task-11
bench: add benchmarks for getenv
This commit is contained in:
commit
ee2f098f4d
3 changed files with 66 additions and 0 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -1,6 +1,8 @@
|
|||
.idea/
|
||||
|
||||
/target
|
||||
/itconfig-tests/target
|
||||
|
||||
Cargo.lock
|
||||
|
||||
itconfig/src/main.rs
|
||||
|
|
|
@ -10,7 +10,13 @@ publish = false
|
|||
|
||||
[dependencies]
|
||||
itconfig = { path = '../itconfig' }
|
||||
criterion = "0.3.1"
|
||||
lazy_static = "1.4.0"
|
||||
|
||||
[features]
|
||||
default = ["meta_namespace"]
|
||||
meta_namespace = []
|
||||
|
||||
[[bench]]
|
||||
name = "main_benches"
|
||||
harness = false
|
||||
|
|
58
itconfig-tests/benches/main_benches.rs
Normal file
58
itconfig-tests/benches/main_benches.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
use criterion::{Criterion, criterion_main, criterion_group, Fun};
|
||||
use std::env;
|
||||
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
|
||||
|
||||
|
||||
fn setup_env_var(initial: &String) {
|
||||
env::set_var("TEST", initial);
|
||||
}
|
||||
|
||||
|
||||
fn source_get_env() -> u32 {
|
||||
itconfig::get_env::<u32>("TEST").unwrap()
|
||||
}
|
||||
|
||||
|
||||
fn lazy_get_env() -> u32 {
|
||||
lazy_static! {
|
||||
static ref RES: u32 = source_get_env();
|
||||
};
|
||||
|
||||
return *RES;
|
||||
}
|
||||
|
||||
|
||||
fn source_vs_lazy(c: &mut Criterion) {
|
||||
let source = Fun::new("source", |b, initial: &String| {
|
||||
setup_env_var(initial);
|
||||
let int: u32 = initial.parse().unwrap();
|
||||
|
||||
b.iter(move || {
|
||||
assert_eq!(source_get_env(), int)
|
||||
})
|
||||
});
|
||||
let lazy = Fun::new("lazy", |b, initial: &String| {
|
||||
setup_env_var(initial);
|
||||
let int: u32 = initial.parse().unwrap();
|
||||
|
||||
b.iter(move || {
|
||||
assert_eq!(lazy_get_env(), int);
|
||||
})
|
||||
});
|
||||
|
||||
let funcs = vec![source, lazy];
|
||||
|
||||
c.bench_functions("get_env", funcs, "1".to_string());
|
||||
}
|
||||
|
||||
|
||||
|
||||
criterion_group! {
|
||||
benches,
|
||||
source_vs_lazy,
|
||||
}
|
||||
|
||||
criterion_main!(benches);
|
Reference in a new issue