mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
chore(bench): generalized HTTP benchmarks framework (#14815)
This commit is contained in:
parent
d7d3341cc3
commit
a307218efc
8 changed files with 63 additions and 164 deletions
|
@ -25,21 +25,62 @@ pub fn benchmark(
|
|||
let core_http_json_ops_exe = core_http_json_ops_exe.to_str().unwrap();
|
||||
|
||||
let mut res = HashMap::new();
|
||||
let manifest_dir = Path::new(env!("CARGO_MANIFEST_DIR"));
|
||||
let http_dir = manifest_dir.join("bench").join("http");
|
||||
for entry in std::fs::read_dir(http_dir.clone())? {
|
||||
let entry = entry?;
|
||||
let pathbuf = entry.path();
|
||||
let path = pathbuf.to_str().unwrap();
|
||||
let name = entry.file_name().into_string().unwrap();
|
||||
let file_stem = pathbuf.file_stem().unwrap().to_str().unwrap();
|
||||
|
||||
let lua_script = http_dir.join(format!("{}.lua", file_stem));
|
||||
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(
|
||||
name,
|
||||
run(
|
||||
&["node", path, &port.to_string()],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
maybe_lua,
|
||||
)?,
|
||||
);
|
||||
} else {
|
||||
// deno run -A --unstable <path> <addr>
|
||||
res.insert(
|
||||
name,
|
||||
run(
|
||||
&[
|
||||
deno_exe,
|
||||
"run",
|
||||
"--allow-all",
|
||||
"--unstable",
|
||||
path,
|
||||
&server_addr(port),
|
||||
],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
maybe_lua,
|
||||
)?,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// "deno_tcp" was once called "deno"
|
||||
res.insert("deno_tcp".to_string(), deno_tcp(deno_exe)?);
|
||||
// res.insert("deno_udp".to_string(), deno_udp(deno_exe)?);
|
||||
res.insert("deno_http".to_string(), deno_http(deno_exe)?);
|
||||
res.insert("deno_http_native".to_string(), deno_http_native(deno_exe)?);
|
||||
// "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"
|
||||
res.insert(
|
||||
"core_http_json_ops".to_string(),
|
||||
core_http_json_ops(core_http_json_ops_exe)?,
|
||||
);
|
||||
// "node_http" was once called "node"
|
||||
res.insert("node_http".to_string(), node_http()?);
|
||||
res.insert("node_tcp".to_string(), node_tcp()?);
|
||||
res.insert("hyper".to_string(), hyper_http(hyper_hello_exe)?);
|
||||
|
||||
Ok(res)
|
||||
|
@ -50,6 +91,7 @@ fn run(
|
|||
port: u16,
|
||||
env: Option<Vec<(String, String)>>,
|
||||
origin_cmd: Option<&[&str]>,
|
||||
lua_script: Option<&str>,
|
||||
) -> Result<HttpBenchmarkResult> {
|
||||
// Wait for port 4544 to become available.
|
||||
// TODO Need to use SO_REUSEPORT with tokio::net::TcpListener.
|
||||
|
@ -80,15 +122,17 @@ fn run(
|
|||
let wrk = test_util::prebuilt_tool_path("wrk");
|
||||
assert!(wrk.is_file());
|
||||
|
||||
let wrk_cmd = &[
|
||||
wrk.to_str().unwrap(),
|
||||
"-d",
|
||||
DURATION,
|
||||
"--latency",
|
||||
&format!("http://127.0.0.1:{}/", port),
|
||||
];
|
||||
let addr = format!("http://127.0.0.1:{}/", port);
|
||||
let mut wrk_cmd =
|
||||
vec![wrk.to_str().unwrap(), "-d", DURATION, "--latency", &addr];
|
||||
|
||||
if let Some(lua_script) = lua_script {
|
||||
wrk_cmd.push("-s");
|
||||
wrk_cmd.push(lua_script);
|
||||
}
|
||||
|
||||
println!("{}", wrk_cmd.join(" "));
|
||||
let output = test_util::run_collect(wrk_cmd, None, None, None, true).0;
|
||||
let output = test_util::run_collect(&wrk_cmd, None, None, None, true).0;
|
||||
|
||||
std::thread::sleep(Duration::from_secs(1)); // wait to capture failure. TODO racy.
|
||||
|
||||
|
@ -122,89 +166,13 @@ fn server_addr(port: u16) -> String {
|
|||
format!("0.0.0.0:{}", port)
|
||||
}
|
||||
|
||||
fn deno_tcp(deno_exe: &str) -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing DENO tcp.");
|
||||
run(
|
||||
&[
|
||||
deno_exe,
|
||||
"run",
|
||||
"--allow-net",
|
||||
"cli/bench/deno_tcp.ts",
|
||||
&server_addr(port),
|
||||
],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn deno_http(deno_exe: &str) -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing DENO using net/http.");
|
||||
run(
|
||||
&[
|
||||
deno_exe,
|
||||
"run",
|
||||
"--allow-net",
|
||||
"--reload",
|
||||
"--unstable",
|
||||
"test_util/std/http/bench.ts",
|
||||
&server_addr(port),
|
||||
],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn deno_http_native(deno_exe: &str) -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing DENO using native bindings.");
|
||||
run(
|
||||
&[
|
||||
deno_exe,
|
||||
"run",
|
||||
"--allow-net",
|
||||
"--reload",
|
||||
"cli/bench/deno_http_native.js",
|
||||
&server_addr(port),
|
||||
],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn core_http_json_ops(exe: &str) -> Result<HttpBenchmarkResult> {
|
||||
println!("http_benchmark testing CORE http_bench_json_ops");
|
||||
run(&[exe], 4544, None, None)
|
||||
}
|
||||
|
||||
fn node_http() -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing NODE.");
|
||||
run(
|
||||
&["node", "cli/bench/node_http.js", &port.to_string()],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
}
|
||||
|
||||
fn node_tcp() -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing node_tcp.js");
|
||||
run(
|
||||
&["node", "cli/bench/node_tcp.js", &port.to_string()],
|
||||
port,
|
||||
None,
|
||||
None,
|
||||
)
|
||||
run(&[exe], 4544, None, None, None)
|
||||
}
|
||||
|
||||
fn hyper_http(exe: &str) -> Result<HttpBenchmarkResult> {
|
||||
let port = get_port();
|
||||
println!("http_benchmark testing RUST hyper");
|
||||
run(&[exe, &port.to_string()], port, None, None)
|
||||
run(&[exe, &port.to_string()], port, None, None, None)
|
||||
}
|
||||
|
|
|
@ -1,69 +0,0 @@
|
|||
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
||||
const net = require("net");
|
||||
|
||||
process.on("uncaughtException", function (error) {
|
||||
console.error(error);
|
||||
});
|
||||
|
||||
if (process.argv.length != 4) {
|
||||
console.log("usage: %s <localport> <remoteport>", process.argv[1]);
|
||||
process.exit();
|
||||
}
|
||||
|
||||
const localport = process.argv[2];
|
||||
const remoteport = process.argv[3];
|
||||
|
||||
const remotehost = "127.0.0.1";
|
||||
|
||||
const server = net.createServer(function (localsocket) {
|
||||
const remotesocket = new net.Socket();
|
||||
|
||||
remotesocket.connect(remoteport, remotehost);
|
||||
|
||||
localsocket.on("data", function (data) {
|
||||
const flushed = remotesocket.write(data);
|
||||
if (!flushed) {
|
||||
localsocket.pause();
|
||||
}
|
||||
});
|
||||
|
||||
remotesocket.on("data", function (data) {
|
||||
const flushed = localsocket.write(data);
|
||||
if (!flushed) {
|
||||
remotesocket.pause();
|
||||
}
|
||||
});
|
||||
|
||||
localsocket.on("drain", function () {
|
||||
remotesocket.resume();
|
||||
});
|
||||
|
||||
remotesocket.on("drain", function () {
|
||||
localsocket.resume();
|
||||
});
|
||||
|
||||
localsocket.on("close", function () {
|
||||
remotesocket.end();
|
||||
});
|
||||
|
||||
remotesocket.on("close", function () {
|
||||
localsocket.end();
|
||||
});
|
||||
|
||||
localsocket.on("error", function () {
|
||||
localsocket.end();
|
||||
});
|
||||
|
||||
remotesocket.on("error", function () {
|
||||
remotesocket.end();
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(localport);
|
||||
|
||||
console.log(
|
||||
"redirecting connections from 127.0.0.1:%d to %s:%d",
|
||||
localport,
|
||||
remotehost,
|
||||
remoteport,
|
||||
);
|
|
@ -20,7 +20,7 @@ async function dlint() {
|
|||
":!:cli/tests/testdata/swc_syntax_error.ts",
|
||||
":!:cli/tests/testdata/038_checkjs.js",
|
||||
":!:cli/tests/testdata/error_008_checkjs.js",
|
||||
":!:cli/bench/node*.js",
|
||||
":!:cli/bench/http/node*.js",
|
||||
":!:cli/bench/testdata/express-router.js",
|
||||
":!:cli/compilers/wasm_wrap.js",
|
||||
":!:cli/dts/**",
|
||||
|
|
Loading…
Reference in a new issue