summaryrefslogtreecommitdiff
path: root/cli/bench/http/deno_tcp.ts
diff options
context:
space:
mode:
authorDivy Srivastava <dj.srivastava23@gmail.com>2022-06-08 17:33:38 +0530
committerGitHub <noreply@github.com>2022-06-08 17:33:38 +0530
commit4305bb4bd8ec3747031ee92baa8e55d50d22b47c (patch)
tree0bd15f3e8082c7865f246696ec8ce644172ab129 /cli/bench/http/deno_tcp.ts
parent8113fac939c06b0d71a22d008c060bed3cb47d72 (diff)
chore(bench): generalized HTTP benchmarks framework (#14815)
Diffstat (limited to 'cli/bench/http/deno_tcp.ts')
-rw-r--r--cli/bench/http/deno_tcp.ts33
1 files changed, 33 insertions, 0 deletions
diff --git a/cli/bench/http/deno_tcp.ts b/cli/bench/http/deno_tcp.ts
new file mode 100644
index 000000000..43b4d2264
--- /dev/null
+++ b/cli/bench/http/deno_tcp.ts
@@ -0,0 +1,33 @@
+// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
+// Used for benchmarking Deno's networking.
+// TODO(bartlomieju): Replace this with a real HTTP server once
+// https://github.com/denoland/deno/issues/726 is completed.
+// Note: this is a keep-alive server.
+const addr = Deno.args[0] || "127.0.0.1:4500";
+const [hostname, port] = addr.split(":");
+const listener = Deno.listen({ hostname, port: Number(port) });
+const response = new TextEncoder().encode(
+ "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n",
+);
+async function handle(conn: Deno.Conn): Promise<void> {
+ const buffer = new Uint8Array(1024);
+ try {
+ while (true) {
+ await conn.read(buffer);
+ await conn.write(response);
+ }
+ } catch (e) {
+ if (
+ !(e instanceof Deno.errors.BrokenPipe) &&
+ !(e instanceof Deno.errors.ConnectionReset)
+ ) {
+ throw e;
+ }
+ }
+ conn.close();
+}
+
+console.log("Listening on", addr);
+for await (const conn of listener) {
+ handle(conn);
+}