mirror of
https://github.com/denoland/deno.git
synced 2024-10-31 09:14:20 -04:00
375ce63c63
This allows resources to be "streams" by implementing read/write/shutdown. These streams are implicit since their nature (read/write/duplex) isn't known until called, but we could easily add another method to explicitly tag resources as streams. `op_read/op_write/op_shutdown` are now builtin ops provided by `deno_core` Note: this current implementation is simple & straightforward but it results in an additional alloc per read/write call Closes #12556
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
// Copyright 2018-2021 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.
|
|
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:4500, returns rid. */
|
|
function listen() {
|
|
return Deno.core.opSync("listen");
|
|
}
|
|
|
|
/** Accepts a connection, returns rid. */
|
|
function accept(serverRid) {
|
|
return Deno.core.opAsync("accept", serverRid);
|
|
}
|
|
|
|
async function serve(rid) {
|
|
try {
|
|
while (true) {
|
|
await Deno.core.read(rid, requestBuf);
|
|
await Deno.core.write(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:4544/\n`);
|
|
|
|
while (true) {
|
|
const rid = await accept(listenerRid);
|
|
serve(rid);
|
|
}
|
|
}
|
|
|
|
main();
|