1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-23 15:16:54 -05:00
denoland-deno/core/examples/http_bench_json_ops.js
David Sherret 2fee8394a9
chore: update copyright year to 2023 (#17247)
Yearly tradition of creating extra noise in git.
2023-01-05 13:05:49 +01:00

51 lines
1.3 KiB
JavaScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
// This is not a real HTTP server. We read blindly one time into 'requestBuf',
// then write this fixed 'responseBuf'. The point of this benchmark is to
// exercise the event loop in a simple yet semi-realistic way.
Deno.core.initializeAsyncOps();
const requestBuf = new Uint8Array(64 * 1024);
const responseBuf = new Uint8Array(
"HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"
.split("")
.map((c) => c.charCodeAt(0)),
);
/** Listens on 0.0.0.0:4570, returns rid. */
function listen() {
return Deno.core.ops.op_listen();
}
/** Accepts a connection, returns rid. */
function accept(serverRid) {
return Deno.core.ops.op_accept(serverRid);
}
async function serve(rid) {
try {
while (true) {
await Deno.core.read(rid, requestBuf);
await Deno.core.writeAll(rid, responseBuf);
}
} catch (e) {
if (
!e.message.includes("Broken pipe") &&
!e.message.includes("Connection reset by peer")
) {
throw e;
}
}
Deno.core.close(rid);
}
async function main() {
const listenerRid = listen();
Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4570/\n`);
while (true) {
const rid = await accept(listenerRid);
serve(rid);
}
}
main();