2020-02-06 11:09:57 +03:00
|
|
|
use criterion::{Criterion, criterion_main, criterion_group, Fun};
|
|
|
|
use std::env;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate lazy_static;
|
2020-02-08 15:09:58 +03:00
|
|
|
#[macro_use]
|
|
|
|
extern crate itconfig;
|
2020-02-06 11:09:57 +03:00
|
|
|
|
|
|
|
|
|
|
|
|
2020-02-08 15:09:58 +03:00
|
|
|
fn setup_env_var(key: &'static str, initial: String) {
|
|
|
|
env::set_var(key, initial);
|
2020-02-06 11:09:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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) {
|
2020-02-08 15:09:58 +03:00
|
|
|
setup_env_var("TEST", "1".to_string());
|
2020-02-06 11:09:57 +03:00
|
|
|
|
2020-02-08 15:09:58 +03:00
|
|
|
let source = Fun::new("source", |b, _| {
|
2020-02-06 11:09:57 +03:00
|
|
|
b.iter(move || {
|
2020-02-08 15:09:58 +03:00
|
|
|
assert_eq!(source_get_env(), 1)
|
2020-02-06 11:09:57 +03:00
|
|
|
})
|
|
|
|
});
|
2020-02-08 15:09:58 +03:00
|
|
|
let lazy = Fun::new("lazy", |b, _| {
|
|
|
|
b.iter(move || {
|
|
|
|
assert_eq!(lazy_get_env(), 1);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
c.bench_functions("get_env", vec![source, lazy], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fn source_macro_vs_lazy_macro(c: &mut Criterion) {
|
|
|
|
config! {
|
|
|
|
TEST: &'static str,
|
|
|
|
TEST_WITH_DEFAULT: &'static str => "default",
|
|
|
|
|
|
|
|
static LAZY_TEST: &'static str,
|
|
|
|
static LAZY_TEST_WITH_DEFAULT: &'static str => "default",
|
|
|
|
}
|
|
|
|
|
|
|
|
setup_env_var("TEST", "test".to_string());
|
|
|
|
setup_env_var("LAZY_TEST", "test".to_string());
|
2020-02-06 11:09:57 +03:00
|
|
|
|
2020-02-08 15:09:58 +03:00
|
|
|
let source = Fun::new("source", |b, _| {
|
2020-02-06 11:09:57 +03:00
|
|
|
b.iter(move || {
|
2020-02-08 15:09:58 +03:00
|
|
|
assert_eq!(cfg::TEST(), "test");
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let lazy = Fun::new("lazy", |b, _| {
|
|
|
|
b.iter(move || {
|
|
|
|
assert_eq!(cfg::LAZY_TEST(), "test");
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let source_with_default = Fun::new("source_with_default", |b, _| {
|
|
|
|
b.iter(move || {
|
|
|
|
assert_eq!(cfg::TEST_WITH_DEFAULT(), "default");
|
|
|
|
})
|
|
|
|
});
|
|
|
|
let lazy_with_default = Fun::new("lazy_with_default", |b, _| {
|
|
|
|
b.iter(move || {
|
|
|
|
assert_eq!(cfg::LAZY_TEST_WITH_DEFAULT(), "default");
|
2020-02-06 11:09:57 +03:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-02-08 15:09:58 +03:00
|
|
|
let funcs = vec![source, lazy, source_with_default, lazy_with_default];
|
2020-02-06 11:09:57 +03:00
|
|
|
|
2020-02-08 15:09:58 +03:00
|
|
|
c.bench_functions("macro", funcs, 0);
|
2020-02-06 11:09:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
criterion_group! {
|
|
|
|
benches,
|
|
|
|
source_vs_lazy,
|
2020-02-08 15:09:58 +03:00
|
|
|
source_macro_vs_lazy_macro,
|
2020-02-06 11:09:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
criterion_main!(benches);
|