1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-28 16:20:57 -05:00
denoland-deno/core/examples/http_bench_json_ops.js
Aaron O'Mullan fec1b2a5a4
refactor: new optimized op-layer using serde_v8 (#9843)
- Improves op performance.
- Handle op-metadata (errors, promise IDs) explicitly in the op-layer vs
  per op-encoding (aka: out-of-payload).
- Remove shared queue & custom "asyncHandlers", all async values are
  returned in batches via js_recv_cb.
- The op-layer should be thought of as simple function calls with little
  indirection or translation besides the conceptually straightforward
  serde_v8 bijections.
- Preserve concepts of json/bin/min as semantic groups of their
  inputs/outputs instead of their op-encoding strategy, preserving these
  groups will also facilitate partial transitions over to v8 Fast API for the
  "min" and "bin" groups
2021-03-31 10:37:38 -04:00

71 lines
1.7 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.jsonOpSync("listen");
}
/** Accepts a connection, returns rid. */
function accept(serverRid) {
return Deno.core.jsonOpAsync("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.jsonOpAsync("read", rid, data);
}
/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
function write(rid, data) {
return Deno.core.jsonOpAsync("write", rid, data);
}
function close(rid) {
Deno.core.jsonOpSync("close", rid);
}
async function serve(rid) {
while (true) {
const nread = await read(rid, requestBuf);
if (nread <= 0) {
break;
}
const nwritten = await write(rid, responseBuf);
if (nwritten < 0) {
break;
}
}
close(rid);
}
async function main() {
Deno.core.ops();
Deno.core.registerErrorClass("Error", Error);
const listenerRid = listen();
Deno.core.print(`http_bench_json_ops listening on http://127.0.0.1:4544/\n`);
for (;;) {
const rid = await accept(listenerRid);
if (rid < 0) {
Deno.core.print(`accept error ${rid}`);
return;
}
serve(rid);
}
}
main();