mirror of
https://github.com/denoland/deno.git
synced 2024-11-01 09:24:20 -04:00
83bece56b0
Even if bootstrapping the JS runtime is low level, it's an abstraction leak of core to require users to call `Deno.core.ops()` in JS space. So instead we're introducing a `JsRuntime::sync_ops_cache()` method, once we have runtime extensions a new runtime will ensure the ops cache is setup (for the provided extensions) and then loading/unloading plugins should be the only operations that require op cache syncs
66 lines
1.6 KiB
JavaScript
66 lines
1.6 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);
|
|
}
|
|
|
|
/**
|
|
* Reads a packet from the rid, presumably an http request. data is ignored.
|
|
* Returns bytes read.
|
|
*/
|
|
function read(rid, data) {
|
|
return Deno.core.opAsync("read", rid, data);
|
|
}
|
|
|
|
/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
|
|
function write(rid, data) {
|
|
return Deno.core.opAsync("write", rid, data);
|
|
}
|
|
|
|
function close(rid) {
|
|
Deno.core.opSync("close", rid);
|
|
}
|
|
|
|
async function serve(rid) {
|
|
try {
|
|
while (true) {
|
|
await read(rid, requestBuf);
|
|
await write(rid, responseBuf);
|
|
}
|
|
} catch (e) {
|
|
if (
|
|
!e.message.includes("Broken pipe") &&
|
|
!e.message.includes("Connection reset by peer")
|
|
) {
|
|
throw e;
|
|
}
|
|
}
|
|
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();
|