Merge pull request #12 from icetemple/task-11

bench: add benchmarks for getenv
This commit is contained in:
Dmitriy Pleshevskiy 2020-02-06 11:16:51 +03:00 committed by GitHub
commit ee2f098f4d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 0 deletions

2
.gitignore vendored
View file

@ -1,6 +1,8 @@
.idea/ .idea/
/target /target
/itconfig-tests/target
Cargo.lock Cargo.lock
itconfig/src/main.rs itconfig/src/main.rs

View file

@ -10,7 +10,13 @@ publish = false
[dependencies] [dependencies]
itconfig = { path = '../itconfig' } itconfig = { path = '../itconfig' }
criterion = "0.3.1"
lazy_static = "1.4.0"
[features] [features]
default = ["meta_namespace"] default = ["meta_namespace"]
meta_namespace = [] meta_namespace = []
[[bench]]
name = "main_benches"
harness = false

View 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);