From ffd3d82ead95850ec831c5757475e0fdbc8a1fd7 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 6 Feb 2020 11:09:57 +0300 Subject: [PATCH] bench: add benchmarks for getenv Closes #11 --- .gitignore | 2 + itconfig-tests/Cargo.toml | 6 +++ itconfig-tests/benches/main_benches.rs | 58 ++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 itconfig-tests/benches/main_benches.rs diff --git a/.gitignore b/.gitignore index a46bd15..e77e33d 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .idea/ /target +/itconfig-tests/target + Cargo.lock itconfig/src/main.rs diff --git a/itconfig-tests/Cargo.toml b/itconfig-tests/Cargo.toml index 00ba8e3..fbc6ede 100644 --- a/itconfig-tests/Cargo.toml +++ b/itconfig-tests/Cargo.toml @@ -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 diff --git a/itconfig-tests/benches/main_benches.rs b/itconfig-tests/benches/main_benches.rs new file mode 100644 index 0000000..bdef991 --- /dev/null +++ b/itconfig-tests/benches/main_benches.rs @@ -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::("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);