diff options
author | David Sherret <dsherret@users.noreply.github.com> | 2023-06-10 11:09:45 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-06-10 11:09:45 -0400 |
commit | 7f15126f23d97f20a4fb33e43136cd4d13825863 (patch) | |
tree | 85d77389969b31999680059e65954a9fa863758e /cli/bench | |
parent | f3326eebd6af2aaca1543e8cb543a7b16762bc96 (diff) |
chore(tests): test_util - Add `PathRef` (#19450)
This adds a new `PathRef` struct to test_util for making it easier to
work with paths in test code. I'm going to expand on this more in the
future.
Diffstat (limited to 'cli/bench')
-rw-r--r-- | cli/bench/http.rs | 10 | ||||
-rw-r--r-- | cli/bench/main.rs | 34 | ||||
-rw-r--r-- | cli/bench/websocket.rs | 3 |
3 files changed, 23 insertions, 24 deletions
diff --git a/cli/bench/http.rs b/cli/bench/http.rs index 031e9801c..909c4937c 100644 --- a/cli/bench/http.rs +++ b/cli/bench/http.rs @@ -23,7 +23,7 @@ pub fn benchmark( target_path: &Path, ) -> Result<HashMap<String, HttpBenchmarkResult>> { let deno_exe = test_util::deno_exe_path(); - let deno_exe = deno_exe.to_str().unwrap(); + let deno_exe = deno_exe.to_string(); let hyper_hello_exe = target_path.join("test_server"); let hyper_hello_exe = hyper_hello_exe.to_str().unwrap(); @@ -82,7 +82,7 @@ pub fn benchmark( res.insert( file_stem.to_string(), run( - &[bun_exe.to_str().unwrap(), path, &port.to_string()], + &[&bun_exe.to_string(), path, &port.to_string()], port, None, None, @@ -95,7 +95,7 @@ pub fn benchmark( file_stem.to_string(), run( &[ - deno_exe, + deno_exe.as_str(), "run", "--allow-all", "--unstable", @@ -160,8 +160,8 @@ fn run( assert!(wrk.is_file()); let addr = format!("http://127.0.0.1:{port}/"); - let mut wrk_cmd = - vec![wrk.to_str().unwrap(), "-d", DURATION, "--latency", &addr]; + let wrk = wrk.to_string(); + let mut wrk_cmd = vec![wrk.as_str(), "-d", DURATION, "--latency", &addr]; if let Some(lua_script) = lua_script { wrk_cmd.push("-s"); diff --git a/cli/bench/main.rs b/cli/bench/main.rs index 721cf06ab..fdeb588f4 100644 --- a/cli/bench/main.rs +++ b/cli/bench/main.rs @@ -12,6 +12,7 @@ use std::path::PathBuf; use std::process::Command; use std::process::Stdio; use std::time::SystemTime; +use test_util::PathRef; include!("../util/time.rs"); @@ -19,12 +20,12 @@ mod http; mod lsp; mod websocket; -fn read_json(filename: &str) -> Result<Value> { +fn read_json(filename: &Path) -> Result<Value> { let f = fs::File::open(filename)?; Ok(serde_json::from_reader(f)?) } -fn write_json(filename: &str, value: &Value) -> Result<()> { +fn write_json(filename: &Path, value: &Value) -> Result<()> { let f = fs::File::create(filename)?; serde_json::to_writer(f, value)?; Ok(()) @@ -170,17 +171,17 @@ const RESULT_KEYS: &[&str] = &["mean", "stddev", "user", "system", "min", "max"]; fn run_exec_time( deno_exe: &Path, - target_dir: &Path, + target_dir: &PathRef, ) -> Result<HashMap<String, HashMap<String, f64>>> { - let hyperfine_exe = test_util::prebuilt_tool_path("hyperfine"); + let hyperfine_exe = test_util::prebuilt_tool_path("hyperfine").to_string(); let benchmark_file = target_dir.join("hyperfine_results.json"); - let benchmark_file = benchmark_file.to_str().unwrap(); + let benchmark_file_str = benchmark_file.to_string(); let mut command = [ - hyperfine_exe.to_str().unwrap(), + hyperfine_exe.as_str(), "--export-json", - benchmark_file, + benchmark_file_str.as_str(), "--warmup", "3", ] @@ -213,7 +214,7 @@ fn run_exec_time( ); let mut results = HashMap::<String, HashMap<String, f64>>::new(); - let hyperfine_results = read_json(benchmark_file)?; + let hyperfine_results = read_json(benchmark_file.as_path())?; for ((name, _, _), data) in EXEC_TIME_BENCHMARKS.iter().zip( hyperfine_results .as_object() @@ -270,7 +271,7 @@ fn get_binary_sizes(target_dir: &Path) -> Result<HashMap<String, i64>> { sizes.insert( "deno".to_string(), - test_util::deno_exe_path().metadata()?.len() as i64, + test_util::deno_exe_path().as_path().metadata()?.len() as i64, ); // add up size for everything in target/release/deps/libswc* @@ -440,7 +441,7 @@ async fn main() -> Result<()> { println!("Starting Deno benchmark"); let target_dir = test_util::target_dir(); - let deno_exe = test_util::deno_exe_path(); + let deno_exe = test_util::deno_exe_path().to_path_buf(); env::set_current_dir(test_util::root_path())?; let mut new_data = BenchResult { @@ -474,7 +475,7 @@ async fn main() -> Result<()> { } if benchmarks.contains(&"binary_size") { - let binary_sizes = get_binary_sizes(&target_dir)?; + let binary_sizes = get_binary_sizes(target_dir.as_path())?; new_data.binary_size = binary_sizes; } @@ -489,7 +490,7 @@ async fn main() -> Result<()> { } if benchmarks.contains(&"http") && cfg!(not(target_os = "windows")) { - let stats = http::benchmark(&target_dir)?; + let stats = http::benchmark(target_dir.as_path())?; let req_per_sec = stats .iter() .map(|(name, result)| (name.clone(), result.requests as i64)) @@ -554,11 +555,10 @@ async fn main() -> Result<()> { new_data.max_memory = max_memory; } - if let Some(filename) = target_dir.join("bench.json").to_str() { - write_json(filename, &serde_json::to_value(&new_data)?)?; - } else { - eprintln!("Cannot write bench.json, path is invalid"); - } + write_json( + target_dir.join("bench.json").as_path(), + &serde_json::to_value(&new_data)?, + )?; Ok(()) } diff --git a/cli/bench/websocket.rs b/cli/bench/websocket.rs index 84a799660..0296d4ce8 100644 --- a/cli/bench/websocket.rs +++ b/cli/bench/websocket.rs @@ -10,7 +10,6 @@ use super::Result; pub fn benchmark() -> Result<HashMap<String, f64>> { let deno_exe = test_util::deno_exe_path(); - let deno_exe = deno_exe.to_str().unwrap(); let mut res = HashMap::new(); let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR")); @@ -22,7 +21,7 @@ pub fn benchmark() -> Result<HashMap<String, f64>> { let path = pathbuf.to_str().unwrap(); let file_stem = pathbuf.file_stem().unwrap().to_str().unwrap(); - let mut cmd = Command::new(deno_exe); + let mut cmd = Command::new(&deno_exe); let mut server = cmd .arg("run") .arg("-A") |