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("")
|
|
|
|
.map(c => c.charCodeAt(0))
|
|
|
|
);
|
|
|
|
const promiseMap = new Map();
|
2019-06-14 13:58:20 -04:00
|
|
|
let nextPromiseId = 1;
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
function createResolvable() {
|
2020-01-11 04:49:16 -05:00
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
const promise = new Promise((res, rej) => {
|
|
|
|
resolve = res;
|
|
|
|
reject = rej;
|
2019-02-26 17:36:05 -05:00
|
|
|
});
|
2020-01-11 04:49:16 -05:00
|
|
|
promise.resolve = resolve;
|
|
|
|
promise.reject = reject;
|
|
|
|
return promise;
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
const scratch32 = new Int32Array(3);
|
2019-03-14 19:17:52 -04:00
|
|
|
const scratchBytes = new Uint8Array(
|
|
|
|
scratch32.buffer,
|
|
|
|
scratch32.byteOffset,
|
|
|
|
scratch32.byteLength
|
|
|
|
);
|
2019-08-07 14:02:29 -04:00
|
|
|
assert(scratchBytes.byteLength === 3 * 4);
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2020-02-06 18:34:40 -05:00
|
|
|
function send(promiseId, opId, rid, zeroCopy = null) {
|
2019-06-14 13:58:20 -04:00
|
|
|
scratch32[0] = promiseId;
|
2020-02-06 18:34:40 -05:00
|
|
|
scratch32[1] = rid;
|
2019-08-07 14:02:29 -04:00
|
|
|
scratch32[2] = -1;
|
|
|
|
return Deno.core.dispatch(opId, scratchBytes, zeroCopy);
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
/** Returns Promise<number> */
|
2020-02-06 18:34:40 -05:00
|
|
|
function sendAsync(opId, rid, zeroCopy = null) {
|
2019-06-14 13:58:20 -04:00
|
|
|
const promiseId = nextPromiseId++;
|
2019-02-26 17:36:05 -05:00
|
|
|
const p = createResolvable();
|
2020-02-06 18:34:40 -05:00
|
|
|
const buf = send(promiseId, opId, rid, zeroCopy);
|
2019-10-14 17:46:27 -04:00
|
|
|
if (buf) {
|
|
|
|
const record = recordFromBuf(buf);
|
|
|
|
// Sync result.
|
|
|
|
p.resolve(record.result);
|
|
|
|
} else {
|
|
|
|
// Async result.
|
|
|
|
promiseMap.set(promiseId, p);
|
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-10-14 17:46:27 -04:00
|
|
|
/** Returns i32 number */
|
2020-02-06 18:34:40 -05:00
|
|
|
function sendSync(opId, rid) {
|
|
|
|
const buf = send(0, opId, rid);
|
2019-10-14 17:46:27 -04:00
|
|
|
const record = recordFromBuf(buf);
|
2020-01-11 04:49:16 -05:00
|
|
|
return record[2];
|
2019-10-14 17:46:27 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function recordFromBuf(buf) {
|
2019-08-07 14:02:29 -04:00
|
|
|
assert(buf.byteLength === 3 * 4);
|
2020-01-11 04:49:16 -05:00
|
|
|
return new Int32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2020-01-17 09:26:51 -05:00
|
|
|
function handleAsyncMsgFromRust(buf) {
|
2019-03-14 19:17:52 -04:00
|
|
|
const record = recordFromBuf(buf);
|
2020-01-11 04:49:16 -05:00
|
|
|
const p = promiseMap.get(record[0]);
|
|
|
|
promiseMap.delete(record[0]);
|
|
|
|
p.resolve(record[2]);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Listens on 0.0.0.0:4500, returns rid. */
|
|
|
|
function listen() {
|
2019-09-30 14:59:44 -04:00
|
|
|
return sendSync(ops["listen"], -1);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Accepts a connection, returns rid. */
|
2020-03-20 09:38:34 -04:00
|
|
|
function accept(rid) {
|
2020-03-16 05:22:16 -04:00
|
|
|
return sendAsync(ops["accept"], rid);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a packet from the rid, presumably an http request. data is ignored.
|
|
|
|
* Returns bytes read.
|
|
|
|
*/
|
2020-03-20 09:38:34 -04:00
|
|
|
function read(rid, data) {
|
2020-03-16 05:22:16 -04:00
|
|
|
return sendAsync(ops["read"], rid, data);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
|
2020-03-20 09:38:34 -04:00
|
|
|
function write(rid, data) {
|
2020-03-16 05:22:16 -04:00
|
|
|
return sendAsync(ops["write"], rid, data);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function close(rid) {
|
2019-09-30 14:59:44 -04:00
|
|
|
return sendSync(ops["close"], rid);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2019-09-30 14:59:44 -04:00
|
|
|
let ops;
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
async function main() {
|
2019-09-30 14:59:44 -04:00
|
|
|
ops = Deno.core.ops();
|
2020-01-17 08:26:11 -05:00
|
|
|
for (const opName in ops) {
|
|
|
|
Deno.core.setAsyncHandler(ops[opName], handleAsyncMsgFromRust);
|
|
|
|
}
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
Deno.core.print("http_bench.js start\n");
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-03-09 12:30:38 -05:00
|
|
|
const listenerRid = listen();
|
2020-02-06 18:34:40 -05:00
|
|
|
Deno.core.print(`listening http://127.0.0.1:4544/ rid=${listenerRid}\n`);
|
2019-02-26 17:36:05 -05:00
|
|
|
while (true) {
|
2019-03-09 12:30:38 -05:00
|
|
|
const rid = await accept(listenerRid);
|
2019-03-26 08:22:07 -04:00
|
|
|
// Deno.core.print(`accepted ${rid}`);
|
2019-02-26 17:36:05 -05:00
|
|
|
if (rid < 0) {
|
2019-03-26 08:22:07 -04:00
|
|
|
Deno.core.print(`accept error ${rid}`);
|
2019-02-26 17:36:05 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
serve(rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|