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 OP_LISTEN = 1;
|
|
|
|
const OP_ACCEPT = 2;
|
|
|
|
const OP_READ = 3;
|
|
|
|
const OP_WRITE = 4;
|
|
|
|
const OP_CLOSE = 5;
|
|
|
|
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();
|
|
|
|
let nextPromiseId = 1;
|
|
|
|
|
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() {
|
|
|
|
let methods;
|
|
|
|
const promise = new Promise((resolve, reject) => {
|
|
|
|
methods = { resolve, reject };
|
|
|
|
});
|
|
|
|
return Object.assign(promise, methods);
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
const scratch32 = new Int32Array(4);
|
|
|
|
const scratchBytes = new Uint8Array(
|
|
|
|
scratch32.buffer,
|
|
|
|
scratch32.byteOffset,
|
|
|
|
scratch32.byteLength
|
|
|
|
);
|
|
|
|
assert(scratchBytes.byteLength === 4 * 4);
|
|
|
|
|
|
|
|
// Toggle what method we send with. false = legacy.
|
|
|
|
// AFAICT This has no effect on performance.
|
|
|
|
const sendWithShared = true;
|
|
|
|
|
|
|
|
function send(promiseId, opId, arg, zeroCopy = null) {
|
|
|
|
scratch32[0] = promiseId;
|
|
|
|
scratch32[1] = opId;
|
|
|
|
scratch32[2] = arg;
|
|
|
|
scratch32[3] = -1;
|
|
|
|
if (sendWithShared) {
|
|
|
|
Deno._sharedQueue.push(scratchBytes);
|
|
|
|
libdeno.send(null, zeroCopy);
|
|
|
|
} else {
|
|
|
|
libdeno.send(scratchBytes, zeroCopy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-26 17:36:05 -05:00
|
|
|
/** Returns Promise<number> */
|
2019-03-14 19:17:52 -04:00
|
|
|
function sendAsync(opId, arg, zeroCopy = null) {
|
2019-03-11 17:57:36 -04:00
|
|
|
const promiseId = nextPromiseId++;
|
2019-02-26 17:36:05 -05:00
|
|
|
const p = createResolvable();
|
2019-03-11 17:57:36 -04:00
|
|
|
promiseMap.set(promiseId, p);
|
2019-03-14 19:17:52 -04:00
|
|
|
send(promiseId, opId, arg, zeroCopy);
|
2019-02-26 17:36:05 -05:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function recordFromBuf(buf) {
|
|
|
|
assert(buf.byteLength === 16);
|
|
|
|
const buf32 = new Int32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4);
|
|
|
|
return {
|
|
|
|
promiseId: buf32[0],
|
|
|
|
opId: buf32[1],
|
|
|
|
arg: buf32[2],
|
|
|
|
result: buf32[3]
|
|
|
|
};
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function recv() {
|
|
|
|
const buf = Deno._sharedQueue.shift();
|
|
|
|
if (!buf) {
|
|
|
|
return null;
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
2019-03-14 19:17:52 -04:00
|
|
|
return recordFromBuf(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Returns i32 number */
|
|
|
|
function sendSync(opId, arg) {
|
|
|
|
send(0, opId, arg);
|
|
|
|
const record = recv();
|
|
|
|
assert(recv() == null);
|
|
|
|
return record.result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleAsyncMsgFromRust(buf) {
|
|
|
|
const record = recordFromBuf(buf);
|
|
|
|
const { promiseId, result } = record;
|
|
|
|
const p = promiseMap.get(promiseId);
|
|
|
|
promiseMap.delete(promiseId);
|
|
|
|
p.resolve(result);
|
2019-02-26 17:36:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Listens on 0.0.0.0:4500, returns rid. */
|
|
|
|
function listen() {
|
|
|
|
return sendSync(OP_LISTEN, -1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Accepts a connection, returns rid. */
|
|
|
|
async function accept(rid) {
|
|
|
|
return await sendAsync(OP_ACCEPT, rid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads a packet from the rid, presumably an http request. data is ignored.
|
|
|
|
* Returns bytes read.
|
|
|
|
*/
|
|
|
|
async function read(rid, data) {
|
|
|
|
return await sendAsync(OP_READ, rid, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Writes a fixed HTTP response to the socket rid. Returns bytes written. */
|
|
|
|
async function write(rid, data) {
|
|
|
|
return await sendAsync(OP_WRITE, rid, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
function close(rid) {
|
|
|
|
return sendSync(OP_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() {
|
2019-03-14 19:17:52 -04:00
|
|
|
Deno._setAsyncHandler(handleAsyncMsgFromRust);
|
2019-02-26 17:36:05 -05:00
|
|
|
|
2019-03-11 17:57:36 -04:00
|
|
|
libdeno.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();
|
2019-03-14 19:17:52 -04:00
|
|
|
libdeno.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-02-26 17:36:05 -05:00
|
|
|
// libdeno.print(`accepted ${rid}`);
|
|
|
|
if (rid < 0) {
|
|
|
|
libdeno.print(`accept error ${rid}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
serve(rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
main();
|