diff --git a/Cargo.toml b/Cargo.toml index c0adc3f9..f261ca66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -18,7 +18,7 @@ exclude = ["book/*"] [dependencies] anes = "0.1.4" -lazy_static = "1.4" +once_cell = "1.14" criterion-plot = { path = "plot", version = "0.5.0" } itertools = "0.10" serde = "1.0" diff --git a/src/lib.rs b/src/lib.rs index 16e79cc2..f35d4cdc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,9 +38,6 @@ extern crate quickcheck; use regex::Regex; -#[macro_use] -extern crate lazy_static; - #[cfg(feature = "real_blackbox")] extern crate test; @@ -86,6 +83,7 @@ use std::sync::{Mutex, MutexGuard}; use std::time::Duration; use criterion_plot::{Version, VersionError}; +use once_cell::sync::Lazy; use crate::benchmark::BenchmarkConfig; use crate::connection::Connection; @@ -103,53 +101,45 @@ pub use crate::bencher::AsyncBencher; pub use crate::bencher::Bencher; pub use crate::benchmark_group::{BenchmarkGroup, BenchmarkId}; -lazy_static! { - static ref DEBUG_ENABLED: bool = std::env::var_os("CRITERION_DEBUG").is_some(); - static ref GNUPLOT_VERSION: Result = criterion_plot::version(); - static ref DEFAULT_PLOTTING_BACKEND: PlottingBackend = { - if cfg!(feature = "html_reports") { - match &*GNUPLOT_VERSION { - Ok(_) => PlottingBackend::Gnuplot, - Err(e) => { - match e { - VersionError::Exec(_) => eprintln!("Gnuplot not found, using plotters backend"), - e => eprintln!( - "Gnuplot not found or not usable, using plotters backend\n{}", - e - ), - }; - PlottingBackend::Plotters - } - } - } else { - PlottingBackend::None - } - }; - static ref CARGO_CRITERION_CONNECTION: Option> = { - match std::env::var("CARGO_CRITERION_PORT") { - Ok(port_str) => { - let port: u16 = port_str.parse().ok()?; - let stream = TcpStream::connect(("localhost", port)).ok()?; - Some(Mutex::new(Connection::new(stream).ok()?)) - } - Err(_) => None, - } - }; - static ref DEFAULT_OUTPUT_DIRECTORY: PathBuf = { - // Set criterion home to (in descending order of preference): - // - $CRITERION_HOME (cargo-criterion sets this, but other users could as well) - // - $CARGO_TARGET_DIR/criterion - // - the cargo target dir from `cargo metadata` - // - ./target/criterion - if let Some(value) = env::var_os("CRITERION_HOME") { - PathBuf::from(value) - } else if let Some(path) = cargo_target_directory() { - path.join("criterion") - } else { - PathBuf::from("target/criterion") +static DEBUG_ENABLED: Lazy = Lazy::new(|| std::env::var_os("CRITERION_DEBUG").is_some()); +static GNUPLOT_VERSION: Lazy> = + Lazy::new(|| criterion_plot::version()); +static DEFAULT_PLOTTING_BACKEND: Lazy = Lazy::new(|| match &*GNUPLOT_VERSION { + Ok(_) => PlottingBackend::Gnuplot, + Err(e) => { + match e { + VersionError::Exec(_) => println!("Gnuplot not found, using plotters backend"), + e => println!( + "Gnuplot not found or not usable, using plotters backend\n{}", + e + ), + }; + PlottingBackend::Plotters + } +}); +static CARGO_CRITERION_CONNECTION: Lazy>> = + Lazy::new(|| match std::env::var("CARGO_CRITERION_PORT") { + Ok(port_str) => { + let port: u16 = port_str.parse().ok()?; + let stream = TcpStream::connect(("localhost", port)).ok()?; + Some(Mutex::new(Connection::new(stream).ok()?)) } - }; -} + Err(_) => None, + }); +static DEFAULT_OUTPUT_DIRECTORY: Lazy = Lazy::new(|| { + // Set criterion home to (in descending order of preference): + // - $CRITERION_HOME (cargo-criterion sets this, but other users could as well) + // - $CARGO_TARGET_DIR/criterion + // - the cargo target dir from `cargo metadata` + // - ./target/criterion + if let Some(value) = env::var_os("CRITERION_HOME") { + PathBuf::from(value) + } else if let Some(path) = cargo_target_directory() { + path.join("criterion") + } else { + PathBuf::from("target/criterion") + } +}); fn debug_enabled() -> bool { *DEBUG_ENABLED