2023-01-02 16:00:42 -05:00
|
|
|
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
|
2020-08-28 09:03:50 -04:00
|
|
|
|
2023-01-14 23:18:58 -05:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::path::Path;
|
|
|
|
use std::process::Command;
|
|
|
|
use std::sync::atomic::AtomicU16;
|
|
|
|
use std::sync::atomic::Ordering;
|
|
|
|
use std::time::Duration;
|
2023-01-05 14:29:50 -05:00
|
|
|
|
|
|
|
use super::Result;
|
|
|
|
|
2023-01-14 23:18:58 -05:00
|
|
|
pub use test_util::parse_wrk_output;
|
|
|
|
pub use test_util::WrkOutput as HttpBenchmarkResult;
|
2020-08-28 09:03:50 -04:00
|
|
|
// Some of the benchmarks in this file have been renamed. In case the history
|
|
|
|
// somehow gets messed up:
|
|
|
|
// "node_http" was once called "node"
|
|
|
|
// "deno_tcp" was once called "deno"
|
|
|
|
// "deno_http" was once called "deno_net_http"
|
|
|
|
|
2022-10-17 10:55:15 -04:00
|
|
|
const DURATION: &str = "10s";
|
2020-08-28 09:03:50 -04:00
|
|
|
|
2022-03-23 09:54:22 -04:00
|
|
|
pub fn benchmark(
|
2021-03-25 14:17:37 -04:00
|
|
|
target_path: &Path,
|
2020-08-28 09:03:50 -04:00
|
|
|
) -> Result<HashMap<String, HttpBenchmarkResult>> {
|
|
|
|
let deno_exe = test_util::deno_exe_path();
|
2023-06-10 11:09:45 -04:00
|
|
|
let deno_exe = deno_exe.to_string();
|
2020-08-28 09:03:50 -04:00
|
|
|
|
|
|
|
let hyper_hello_exe = target_path.join("test_server");
|
|
|
|
let hyper_hello_exe = hyper_hello_exe.to_str().unwrap();
|
|
|
|
|
|
|
|
let core_http_json_ops_exe = target_path.join("examples/http_bench_json_ops");
|
|
|
|
let core_http_json_ops_exe = core_http_json_ops_exe.to_str().unwrap();
|
|
|
|
|
|
|
|
let mut res = HashMap::new();
|
2022-06-08 08:03:38 -04:00
|
|
|
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
|
|
|
let http_dir = manifest_dir.join("bench").join("http");
|
2023-01-05 14:29:50 -05:00
|
|
|
for entry in std::fs::read_dir(&http_dir)? {
|
2022-06-08 08:03:38 -04:00
|
|
|
let entry = entry?;
|
|
|
|
let pathbuf = entry.path();
|
|
|
|
let path = pathbuf.to_str().unwrap();
|
2022-07-01 03:29:01 -04:00
|
|
|
if path.ends_with(".lua") {
|
|
|
|
continue;
|
|
|
|
}
|
2022-06-08 08:03:38 -04:00
|
|
|
let name = entry.file_name().into_string().unwrap();
|
|
|
|
let file_stem = pathbuf.file_stem().unwrap().to_str().unwrap();
|
|
|
|
|
2023-01-27 10:43:16 -05:00
|
|
|
let lua_script = http_dir.join(format!("{file_stem}.lua"));
|
2022-06-08 08:03:38 -04:00
|
|
|
let mut maybe_lua = None;
|
|
|
|
if lua_script.exists() {
|
|
|
|
maybe_lua = Some(lua_script.to_str().unwrap());
|
|
|
|
}
|
|
|
|
|
|
|
|
let port = get_port();
|
|
|
|
if name.starts_with("node") {
|
|
|
|
// node <path> <port>
|
|
|
|
res.insert(
|
2022-06-28 08:21:05 -04:00
|
|
|
file_stem.to_string(),
|
2022-06-08 08:03:38 -04:00
|
|
|
run(
|
|
|
|
&["node", path, &port.to_string()],
|
|
|
|
port,
|
2022-06-29 07:27:19 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
maybe_lua,
|
|
|
|
)?,
|
|
|
|
);
|
|
|
|
} else if name.starts_with("bun") && !cfg!(target_os = "windows") {
|
|
|
|
// Bun does not support Windows.
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
#[cfg(not(target_vendor = "apple"))]
|
|
|
|
let bun_exe = test_util::prebuilt_tool_path("bun");
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
#[cfg(target_arch = "x86_64")]
|
|
|
|
let bun_exe = test_util::prebuilt_tool_path("bun-x64");
|
|
|
|
#[cfg(target_vendor = "apple")]
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
let bun_exe = test_util::prebuilt_tool_path("bun-aarch64");
|
2022-10-09 22:29:31 -04:00
|
|
|
#[cfg(target_os = "linux")]
|
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
let bun_exe = test_util::prebuilt_tool_path("bun-aarch64");
|
2022-06-29 07:27:19 -04:00
|
|
|
|
|
|
|
// bun <path> <port>
|
|
|
|
res.insert(
|
|
|
|
file_stem.to_string(),
|
|
|
|
run(
|
2023-06-10 11:09:45 -04:00
|
|
|
&[&bun_exe.to_string(), path, &port.to_string()],
|
2022-06-29 07:27:19 -04:00
|
|
|
port,
|
2022-06-08 08:03:38 -04:00
|
|
|
None,
|
|
|
|
None,
|
|
|
|
maybe_lua,
|
|
|
|
)?,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// deno run -A --unstable <path> <addr>
|
|
|
|
res.insert(
|
2022-06-28 08:21:05 -04:00
|
|
|
file_stem.to_string(),
|
2022-06-08 08:03:38 -04:00
|
|
|
run(
|
|
|
|
&[
|
2023-06-10 11:09:45 -04:00
|
|
|
deno_exe.as_str(),
|
2022-06-08 08:03:38 -04:00
|
|
|
"run",
|
|
|
|
"--allow-all",
|
|
|
|
"--unstable",
|
2023-04-26 17:59:32 -04:00
|
|
|
"--enable-testing-features-do-not-use",
|
2022-06-08 08:03:38 -04:00
|
|
|
path,
|
|
|
|
&server_addr(port),
|
|
|
|
],
|
|
|
|
port,
|
|
|
|
None,
|
|
|
|
None,
|
|
|
|
maybe_lua,
|
|
|
|
)?,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-28 09:03:50 -04:00
|
|
|
|
2021-04-12 15:55:05 -04:00
|
|
|
// "core_http_json_ops" previously had a "bin op" counterpart called "core_http_bin_ops",
|
|
|
|
// which was previously also called "deno_core_http_bench", "deno_core_single"
|
2020-08-28 09:03:50 -04:00
|
|
|
res.insert(
|
|
|
|
"core_http_json_ops".to_string(),
|
|
|
|
core_http_json_ops(core_http_json_ops_exe)?,
|
|
|
|
);
|
|
|
|
res.insert("hyper".to_string(), hyper_http(hyper_hello_exe)?);
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
server_cmd: &[&str],
|
|
|
|
port: u16,
|
|
|
|
env: Option<Vec<(String, String)>>,
|
|
|
|
origin_cmd: Option<&[&str]>,
|
2022-06-08 08:03:38 -04:00
|
|
|
lua_script: Option<&str>,
|
2020-08-28 09:03:50 -04:00
|
|
|
) -> Result<HttpBenchmarkResult> {
|
|
|
|
// Wait for port 4544 to become available.
|
|
|
|
// TODO Need to use SO_REUSEPORT with tokio::net::TcpListener.
|
|
|
|
std::thread::sleep(Duration::from_secs(5));
|
|
|
|
|
|
|
|
let mut origin = None;
|
|
|
|
if let Some(cmd) = origin_cmd {
|
|
|
|
let mut com = Command::new(cmd[0]);
|
|
|
|
com.args(&cmd[1..]);
|
|
|
|
if let Some(env) = env.clone() {
|
|
|
|
com.envs(env);
|
|
|
|
}
|
|
|
|
origin = Some(com.spawn()?);
|
|
|
|
};
|
|
|
|
|
|
|
|
println!("{}", server_cmd.join(" "));
|
|
|
|
let mut server = {
|
|
|
|
let mut com = Command::new(server_cmd[0]);
|
|
|
|
com.args(&server_cmd[1..]);
|
|
|
|
if let Some(env) = env {
|
|
|
|
com.envs(env);
|
|
|
|
}
|
|
|
|
com.spawn()?
|
|
|
|
};
|
|
|
|
|
|
|
|
std::thread::sleep(Duration::from_secs(5)); // wait for server to wake up. TODO racy.
|
|
|
|
|
|
|
|
let wrk = test_util::prebuilt_tool_path("wrk");
|
|
|
|
assert!(wrk.is_file());
|
|
|
|
|
2023-01-27 10:43:16 -05:00
|
|
|
let addr = format!("http://127.0.0.1:{port}/");
|
2023-06-10 11:09:45 -04:00
|
|
|
let wrk = wrk.to_string();
|
|
|
|
let mut wrk_cmd = vec![wrk.as_str(), "-d", DURATION, "--latency", &addr];
|
2022-06-08 08:03:38 -04:00
|
|
|
|
|
|
|
if let Some(lua_script) = lua_script {
|
|
|
|
wrk_cmd.push("-s");
|
|
|
|
wrk_cmd.push(lua_script);
|
|
|
|
}
|
|
|
|
|
2020-08-28 09:03:50 -04:00
|
|
|
println!("{}", wrk_cmd.join(" "));
|
2022-06-08 08:03:38 -04:00
|
|
|
let output = test_util::run_collect(&wrk_cmd, None, None, None, true).0;
|
2020-08-28 09:03:50 -04:00
|
|
|
|
2021-04-08 12:04:02 -04:00
|
|
|
std::thread::sleep(Duration::from_secs(1)); // wait to capture failure. TODO racy.
|
|
|
|
|
2023-01-27 10:43:16 -05:00
|
|
|
println!("{output}");
|
2020-08-28 09:03:50 -04:00
|
|
|
assert!(
|
2023-03-15 17:46:36 -04:00
|
|
|
server.try_wait()?.map(|s| s.success()).unwrap_or(true),
|
2020-08-28 09:03:50 -04:00
|
|
|
"server ended with error"
|
|
|
|
);
|
|
|
|
|
|
|
|
server.kill()?;
|
|
|
|
if let Some(mut origin) = origin {
|
|
|
|
origin.kill()?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(parse_wrk_output(&output))
|
|
|
|
}
|
|
|
|
|
2022-06-29 04:16:02 -04:00
|
|
|
static NEXT_PORT: AtomicU16 = AtomicU16::new(4544);
|
2023-04-05 09:01:07 -04:00
|
|
|
pub(crate) fn get_port() -> u16 {
|
2022-06-29 04:16:02 -04:00
|
|
|
let p = NEXT_PORT.load(Ordering::SeqCst);
|
|
|
|
NEXT_PORT.store(p.wrapping_add(1), Ordering::SeqCst);
|
|
|
|
p
|
2020-08-28 09:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn server_addr(port: u16) -> String {
|
2023-01-27 10:43:16 -05:00
|
|
|
format!("0.0.0.0:{port}")
|
2020-08-28 09:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn core_http_json_ops(exe: &str) -> Result<HttpBenchmarkResult> {
|
2022-10-15 07:35:04 -04:00
|
|
|
// let port = get_port();
|
2020-08-28 09:03:50 -04:00
|
|
|
println!("http_benchmark testing CORE http_bench_json_ops");
|
2022-10-15 07:35:04 -04:00
|
|
|
run(&[exe], 4570, None, None, None)
|
2020-08-28 09:03:50 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hyper_http(exe: &str) -> Result<HttpBenchmarkResult> {
|
|
|
|
let port = get_port();
|
|
|
|
println!("http_benchmark testing RUST hyper");
|
2022-06-08 08:03:38 -04:00
|
|
|
run(&[exe, &port.to_string()], port, None, None, None)
|
2020-08-28 09:03:50 -04:00
|
|
|
}
|