2022-01-07 22:09:52 -05:00
|
|
|
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
|
2019-02-26 17:36:05 -05:00
|
|
|
// 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.
|
|
|
|
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("")
|
2020-07-14 15:24:17 -04:00
|
|
|
.map((c) => c.charCodeAt(0)),
|
2019-02-26 17:36:05 -05:00
|
|
|
);
|
|
|
|
|
2022-10-15 07:35:04 -04:00
|
|
|
/** Listens on 0.0.0.0:4570, returns rid. */
|
2019-02-26 17:36:05 -05:00
|
|
|
function listen() {
|
2022-08-11 09:56:56 -04:00
|
|
|
return Deno.core.ops.op_listen();
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Accepts a connection, returns rid. */
|
2021-03-31 10:37:38 -04:00
|
|
|
function accept(serverRid) {
|
2022-10-27 10:58:27 -04:00
|
|
|
return Deno.core.opAsync("op_accept", serverRid);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function serve(rid) {
|
2021-04-08 12:04:02 -04:00
|
|
|
try {
|
|
|
|
while (true) {
|
2021-11-09 13:26:17 -05:00
|
|
|
await Deno.core.read(rid, requestBuf);
|
2022-10-10 04:28:35 -04:00
|
|
|
await Deno.core.writeAll(rid, responseBuf);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
2021-04-08 12:04:02 -04:00
|
|
|
} catch (e) {
|
|
|
|
if (
|
|
|
|
!e.message.includes("Broken pipe") &&
|
|
|
|
!e.message.includes("Connection reset by peer")
|
|
|
|
) {
|
|
|
|
throw e;
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
}
|
2021-11-09 13:26:17 -05:00
|
|
|
Deno.core.close(rid);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
async function main() {
|
2019-03-09 12:30:38 -05:00
|
|
|
const listenerRid = listen();
|
2022-10-15 07:35:04 -04:00
|
|
|
Deno.core.print(`http_bench_ops listening on http://127.0.0.1:4570/\n`);
|
2020-08-21 05:47:57 -04:00
|
|
|
|
2021-04-08 12:04:02 -04:00
|
|
|
while (true) {
|
2019-03-09 12:30:38 -05:00
|
|
|
const rid = await accept(listenerRid);
|
2019-02-26 17:36:05 -05:00
|
|
|
serve(rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|