From 7afcb8d649dfed4dfb51173acebffb3d18a8cd6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sat, 28 Jan 2023 14:11:09 -0800 Subject: [PATCH] tests: stop spawning a new tokio runtime every benchmark iteration --- Cargo.toml | 1 + benches/benchmarks.rs | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index bce2fae..9a9e144 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,6 +37,7 @@ walkdir = "2.3.2" [dev-dependencies] criterion = "0.4.0" +lazy_static = "1.4.0" tokio = { version = "1.12.0", features = [ "fs", "io-util", diff --git a/benches/benchmarks.rs b/benches/benchmarks.rs index 4de127e..4e16286 100644 --- a/benches/benchmarks.rs +++ b/benches/benchmarks.rs @@ -5,13 +5,19 @@ use tokio::fs as afs; #[cfg(all(test, feature = "async-std"))] pub use async_std::task::block_on; + +#[cfg(all(test, feature = "tokio"))] +lazy_static::lazy_static! { + static ref TOKIO_RUNTIME: tokio::runtime::Runtime = tokio::runtime::Runtime::new().unwrap(); +} + #[cfg(all(test, feature = "tokio"))] #[inline] pub fn block_on(future: F) -> T where F: std::future::Future, { - tokio::runtime::Runtime::new().unwrap().block_on(future) + TOKIO_RUNTIME.block_on(future) } use std::fs::{self, File};