From 4305bb4bd8ec3747031ee92baa8e55d50d22b47c Mon Sep 17 00:00:00 2001 From: Divy Srivastava Date: Wed, 8 Jun 2022 17:33:38 +0530 Subject: [PATCH] chore(bench): generalized HTTP benchmarks framework (#14815) --- cli/bench/http.rs | 156 +++++++----------- cli/bench/{ => http}/deno_http_native.js | 0 .../{ => http}/deno_http_native_headers.js | 0 cli/bench/{ => http}/deno_tcp.ts | 0 cli/bench/{ => http}/node_http.js | 0 cli/bench/{ => http}/node_tcp.js | 0 cli/bench/node_tcp_proxy.js | 69 -------- tools/lint.js | 2 +- 8 files changed, 63 insertions(+), 164 deletions(-) rename cli/bench/{ => http}/deno_http_native.js (100%) rename cli/bench/{ => http}/deno_http_native_headers.js (100%) rename cli/bench/{ => http}/deno_tcp.ts (100%) rename cli/bench/{ => http}/node_http.js (100%) rename cli/bench/{ => http}/node_tcp.js (100%) delete mode 100644 cli/bench/node_tcp_proxy.js diff --git a/cli/bench/http.rs b/cli/bench/http.rs index b000bc2857..83f8eef8b0 100644 --- a/cli/bench/http.rs +++ b/cli/bench/http.rs @@ -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 + res.insert( + name, + run( + &["node", path, &port.to_string()], + port, + None, + None, + maybe_lua, + )?, + ); + } else { + // deno run -A --unstable + 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>, origin_cmd: Option<&[&str]>, + lua_script: Option<&str>, ) -> Result { // 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 { - 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 { - 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 { - 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 { println!("http_benchmark testing CORE http_bench_json_ops"); - run(&[exe], 4544, None, None) -} - -fn node_http() -> Result { - 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 { - 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 { 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) } diff --git a/cli/bench/deno_http_native.js b/cli/bench/http/deno_http_native.js similarity index 100% rename from cli/bench/deno_http_native.js rename to cli/bench/http/deno_http_native.js diff --git a/cli/bench/deno_http_native_headers.js b/cli/bench/http/deno_http_native_headers.js similarity index 100% rename from cli/bench/deno_http_native_headers.js rename to cli/bench/http/deno_http_native_headers.js diff --git a/cli/bench/deno_tcp.ts b/cli/bench/http/deno_tcp.ts similarity index 100% rename from cli/bench/deno_tcp.ts rename to cli/bench/http/deno_tcp.ts diff --git a/cli/bench/node_http.js b/cli/bench/http/node_http.js similarity index 100% rename from cli/bench/node_http.js rename to cli/bench/http/node_http.js diff --git a/cli/bench/node_tcp.js b/cli/bench/http/node_tcp.js similarity index 100% rename from cli/bench/node_tcp.js rename to cli/bench/http/node_tcp.js diff --git a/cli/bench/node_tcp_proxy.js b/cli/bench/node_tcp_proxy.js deleted file mode 100644 index 8b4b958d7a..0000000000 --- a/cli/bench/node_tcp_proxy.js +++ /dev/null @@ -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 ", 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, -); diff --git a/tools/lint.js b/tools/lint.js index 5ecf1c3c46..7a77c09ae0 100755 --- a/tools/lint.js +++ b/tools/lint.js @@ -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/**",