forked from denoland/deno
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_bench_json_ops.js
53 lines (44 loc) · 1.29 KB
/
http_bench_json_ops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// 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.
const { ops, opAsync, opAsync2 } = Deno.core;
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 ops.op_listen();
}
/** Accepts a connection, returns rid. */
function accept(serverRid) {
return opAsync("op_accept", serverRid);
}
function read(serverRid, buf) {
return opAsync2("op_read_socket", serverRid, buf);
}
async function serve(rid) {
try {
while (true) {
await read(rid, requestBuf);
if (!ops.op_try_write(rid, responseBuf)) {
await Deno.core.writeAll(rid, responseBuf);
}
}
} catch {
// pass
}
Deno.core.close(rid);
}
async function main() {
const listenerRid = listen();
Deno.core.print(`http_bench_ops listening on http:https://127.0.0.1:4570/\n`);
while (true) {
const rid = await accept(listenerRid);
serve(rid);
}
}
main();