diff options
author | Divy Srivastava <dj.srivastava23@gmail.com> | 2023-04-05 18:31:07 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-04-05 18:31:07 +0530 |
commit | 34d596e04f49400ed6460e03461e21c441bcb5dd (patch) | |
tree | da465892b423c0b47eb9c70a4869b7cb25383302 /cli/bench | |
parent | db39855fcb9e90131432d1c03bd5c16263addb3e (diff) |
chore(cli/bench): add ws echo bench (#18595)
Diffstat (limited to 'cli/bench')
-rw-r--r-- | cli/bench/http.rs | 2 | ||||
-rw-r--r-- | cli/bench/main.rs | 8 | ||||
-rw-r--r-- | cli/bench/websocket.rs | 77 | ||||
-rw-r--r-- | cli/bench/websocket/deno_echo.js | 25 |
4 files changed, 111 insertions, 1 deletions
diff --git a/cli/bench/http.rs b/cli/bench/http.rs index 065d6f657..4f8ab968f 100644 --- a/cli/bench/http.rs +++ b/cli/bench/http.rs @@ -187,7 +187,7 @@ fn run( } static NEXT_PORT: AtomicU16 = AtomicU16::new(4544); -fn get_port() -> u16 { +pub(crate) fn get_port() -> u16 { let p = NEXT_PORT.load(Ordering::SeqCst); NEXT_PORT.store(p.wrapping_add(1), Ordering::SeqCst); p diff --git a/cli/bench/main.rs b/cli/bench/main.rs index 48598a9b0..721cf06ab 100644 --- a/cli/bench/main.rs +++ b/cli/bench/main.rs @@ -17,6 +17,7 @@ include!("../util/time.rs"); mod http; mod lsp; +mod websocket; fn read_json(filename: &str) -> Result<Value> { let f = fs::File::open(filename)?; @@ -401,6 +402,7 @@ struct BenchResult { max_memory: HashMap<String, i64>, lsp_exec_time: HashMap<String, i64>, req_per_sec: HashMap<String, i64>, + ws_msg_per_sec: HashMap<String, f64>, syscall_count: HashMap<String, i64>, thread_count: HashMap<String, i64>, } @@ -416,6 +418,7 @@ async fn main() -> Result<()> { "cargo_deps", "lsp", "http", + "websocket", "strace", "mem_usage", ]; @@ -455,6 +458,11 @@ async fn main() -> Result<()> { ..Default::default() }; + if benchmarks.contains(&"websocket") { + let ws = websocket::benchmark()?; + new_data.ws_msg_per_sec = ws; + } + if benchmarks.contains(&"bundle") { let bundle_size = bundle_benchmark(&deno_exe)?; new_data.bundle_size = bundle_size; diff --git a/cli/bench/websocket.rs b/cli/bench/websocket.rs new file mode 100644 index 000000000..84a799660 --- /dev/null +++ b/cli/bench/websocket.rs @@ -0,0 +1,77 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. +use std::collections::HashMap; +use std::io::BufRead; +use std::path::Path; +use std::process::Command; +use std::process::Stdio; +use std::time::Duration; + +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")); + let ws_dir = manifest_dir.join("bench").join("websocket"); + for entry in std::fs::read_dir(&ws_dir)? { + let port = crate::http::get_port(); + let entry = entry?; + let pathbuf = entry.path(); + 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 server = cmd + .arg("run") + .arg("-A") + .arg("--unstable") + .arg(path) + .arg(&port.to_string()) + .spawn() + .unwrap(); + + std::thread::sleep(Duration::from_secs(5)); // wait for server to wake up. + + let load_test = test_util::prebuilt_tool_path("load_test"); + assert!(load_test.is_file()); + // ./load_test 100 0.0.0.0 8000 0 0 + // Running benchmark now... + // Msg/sec: 161327.500000 + // Msg/sec: 163977.000000 + // ^C⏎ + let mut cmd = Command::new(load_test); + let mut process = cmd + .stdout(Stdio::piped()) + .arg("100") + .arg("0.0.0.0") + .arg(&port.to_string()) + .arg("0") + .arg("0") + .spawn() + .unwrap(); + + let mut lines = Vec::new(); + + let mut stdout = + std::io::BufReader::new(process.stdout.take().unwrap()).lines(); + for _ in 0..5 { + let line = stdout.next().unwrap().unwrap(); + lines.push(line); + } + + process.kill().unwrap(); + let msg_per_sec = lines + .into_iter() + .filter(|line| line.starts_with("Msg/sec:")) + .map(|line| line.split(": ").nth(1).unwrap().parse::<f64>().unwrap()) + .max_by(|a, b| a.partial_cmp(b).unwrap()) + .unwrap(); + + res.insert(file_stem.to_string(), msg_per_sec); + server.kill().unwrap(); + } + + Ok(res) +} diff --git a/cli/bench/websocket/deno_echo.js b/cli/bench/websocket/deno_echo.js new file mode 100644 index 000000000..70e64dcbe --- /dev/null +++ b/cli/bench/websocket/deno_echo.js @@ -0,0 +1,25 @@ +// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. + +const port = Deno.args[0] ?? "8080"; +const { serve } = Deno; + +function handler(request) { + const { socket, response } = Deno.upgradeWebSocket(request, { + idleTimeout: 0, + }); + socket.onmessage = (e) => { + socket.send(e.data); + }; + + socket.onopen = () => { + console.log("Connected to client"); + }; + + socket.onerror = (e) => { + console.log(e); + }; + + return response; +} + +serve(handler, { port: parseInt(port), hostname: "0.0.0.0" }); |