diff options
Diffstat (limited to 'core/http_bench.js')
-rw-r--r-- | core/http_bench.js | 111 |
1 files changed, 64 insertions, 47 deletions
diff --git a/core/http_bench.js b/core/http_bench.js index 29a79c436..aae15a72c 100644 --- a/core/http_bench.js +++ b/core/http_bench.js @@ -1,24 +1,57 @@ // 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 shared32 = new Int32Array(libdeno.shared); - -const INDEX_NUM_RECORDS = 0; -const INDEX_RECORDS = 1; -const RECORD_OFFSET_PROMISE_ID = 0; -const RECORD_OFFSET_OP = 1; -const RECORD_OFFSET_ARG = 2; -const RECORD_OFFSET_RESULT = 3; -const RECORD_SIZE = 4; const OP_LISTEN = 1; const OP_ACCEPT = 2; const OP_READ = 3; const OP_WRITE = 4; const OP_CLOSE = 5; +const INDEX_START = 0; +const INDEX_END = 1; +const NUM_RECORDS = 128; +const RECORD_SIZE = 4; + +const shared32 = new Int32Array(libdeno.shared); + +function idx(i, off) { + return 2 + i * RECORD_SIZE + off; +} + +function recordsPush(promiseId, opId, arg, result) { + let i = shared32[INDEX_END]; + if (i >= NUM_RECORDS) { + return false; + } + shared32[idx(i, 0)] = promiseId; + shared32[idx(i, 1)] = opId; + shared32[idx(i, 2)] = arg; + shared32[idx(i, 3)] = result; + shared32[INDEX_END]++; + return true; +} + +function recordsShift() { + if (shared32[INDEX_START] == shared32[INDEX_END]) { + return null; + } + const i = shared32[INDEX_START]; + const record = { + promiseId: shared32[idx(i, 0)], + opId: shared32[idx(i, 1)], + arg: shared32[idx(i, 2)], + result: shared32[idx(i, 3)] + }; + shared32[INDEX_START]++; + return record; +} + +function recordsReset() { + shared32[INDEX_START] = 0; + shared32[INDEX_END] = 0; +} -const NUM_RECORDS = (shared32.length - INDEX_RECORDS) / RECORD_SIZE; -if (NUM_RECORDS != 100) { - throw Error("expected 100 entries"); +function recordsSize() { + return shared32[INDEX_END] - shared32[INDEX_START]; } const requestBuf = new Uint8Array(64 * 1024); @@ -39,51 +72,35 @@ function createResolvable() { return Object.assign(promise, methods); } -function setRecord(i, off, value) { - if (i >= NUM_RECORDS) { - throw Error("out of range"); - } - shared32[INDEX_RECORDS + RECORD_SIZE * i + off] = value; -} - -function getRecord(i, off) { - if (i >= NUM_RECORDS) { - throw Error("out of range"); - } - return shared32[INDEX_RECORDS + RECORD_SIZE * i + off]; -} - /** Returns Promise<number> */ -function sendAsync(op, arg, zeroCopyData) { - const id = nextPromiseId++; +function sendAsync(opId, arg, zeroCopyData) { + const promiseId = nextPromiseId++; const p = createResolvable(); - shared32[INDEX_NUM_RECORDS] = 1; - setRecord(0, RECORD_OFFSET_PROMISE_ID, id); - setRecord(0, RECORD_OFFSET_OP, op); - setRecord(0, RECORD_OFFSET_ARG, arg); - setRecord(0, RECORD_OFFSET_RESULT, -1); - promiseMap.set(id, p); + recordsReset(); + recordsPush(promiseId, opId, arg, -1); + promiseMap.set(promiseId, p); libdeno.send(null, zeroCopyData); return p; } /** Returns u32 number */ -function sendSync(op, arg) { - shared32[INDEX_NUM_RECORDS] = 1; - setRecord(0, RECORD_OFFSET_PROMISE_ID, 0); - setRecord(0, RECORD_OFFSET_OP, op); - setRecord(0, RECORD_OFFSET_ARG, arg); - setRecord(0, RECORD_OFFSET_RESULT, -1); +function sendSync(opId, arg) { + recordsReset(); + recordsPush(0, opId, arg, -1); libdeno.send(); - return getRecord(0, RECORD_OFFSET_RESULT); + if (recordsSize() != 1) { + throw Error("Expected sharedSimple to have size 1"); + } + let { result } = recordsShift(); + return result; } function handleAsyncMsgFromRust() { - for (let i = 0; i < shared32[INDEX_NUM_RECORDS]; i++) { - let id = getRecord(i, RECORD_OFFSET_PROMISE_ID); - const p = promiseMap.get(id); - promiseMap.delete(id); - p.resolve(getRecord(i, RECORD_OFFSET_RESULT)); + while (recordsSize() > 0) { + const { promiseId, result } = recordsShift(); + const p = promiseMap.get(promiseId); + promiseMap.delete(promiseId); + p.resolve(result); } } @@ -132,7 +149,7 @@ async function serve(rid) { async function main() { libdeno.recv(handleAsyncMsgFromRust); - libdeno.print("http_bench.js start"); + libdeno.print("http_bench.js start\n"); const listenerRid = listen(); libdeno.print(`listening http://127.0.0.1:4544/ rid = ${listenerRid}`); |