diff options
Diffstat (limited to 'core/examples/http_bench.js')
-rw-r--r-- | core/examples/http_bench.js | 94 |
1 files changed, 10 insertions, 84 deletions
diff --git a/core/examples/http_bench.js b/core/examples/http_bench.js index ac97e0d88..de6b5a9a4 100644 --- a/core/examples/http_bench.js +++ b/core/examples/http_bench.js @@ -7,76 +7,6 @@ const responseBuf = new Uint8Array( .split("") .map((c) => c.charCodeAt(0)), ); -const promiseMap = new Map(); -let nextPromiseId = 1; - -function assert(cond) { - if (!cond) { - throw Error("assert"); - } -} - -function createResolvable() { - let resolve; - let reject; - const promise = new Promise((res, rej) => { - resolve = res; - reject = rej; - }); - promise.resolve = resolve; - promise.reject = reject; - return promise; -} - -const scratch32 = new Int32Array(3); -const scratchBytes = new Uint8Array( - scratch32.buffer, - scratch32.byteOffset, - scratch32.byteLength, -); -assert(scratchBytes.byteLength === 3 * 4); - -function send(promiseId, opId, rid, ...zeroCopy) { - scratch32[0] = promiseId; - scratch32[1] = rid; - scratch32[2] = -1; - return Deno.core.dispatch(opId, scratchBytes, ...zeroCopy); -} - -/** Returns Promise<number> */ -function sendAsync(opId, rid, ...zeroCopy) { - const promiseId = nextPromiseId++; - const p = createResolvable(); - const buf = send(promiseId, opId, rid, ...zeroCopy); - if (buf) { - const record = recordFromBuf(buf); - // Sync result. - p.resolve(record.result); - } else { - // Async result. - promiseMap.set(promiseId, p); - } - return p; -} - -/** Returns i32 number */ -function sendSync(opId, rid) { - const buf = send(0, opId, rid); - const record = recordFromBuf(buf); - return record[2]; -} - -function recordFromBuf(buf) { - assert(buf.byteLength === 3 * 4); - return new Int32Array(buf.buffer, buf.byteOffset, buf.byteLength / 4); -} - -function handleAsyncMsgFromRust(buf) { - const record = recordFromBuf(buf); - const p = promiseMap.get(record[0]); - promiseMap.delete(record[0]); - p.resolve(record[2]); -} /** Listens on 0.0.0.0:4500, returns rid. */ function listen() { @@ -94,17 +24,19 @@ async function accept(serverRid) { * Reads a packet from the rid, presumably an http request. data is ignored. * Returns bytes read. */ -function read(rid, data) { - return sendAsync(ops["read"], rid, data); +async function read(rid, data) { + const { nread } = await Deno.core.jsonOpAsync("read", { rid }, data); + return nread; } /** Writes a fixed HTTP response to the socket rid. Returns bytes written. */ -function write(rid, data) { - return sendAsync(ops["write"], rid, data); +async function write(rid, data) { + const { nwritten } = await Deno.core.jsonOpAsync("write", { rid }, data); + return nwritten; } function close(rid) { - return sendSync(ops["close"], rid); + Deno.core.jsonOpSync("close", { rid }); } async function serve(rid) { @@ -122,20 +54,14 @@ async function serve(rid) { close(rid); } -let ops; - async function main() { - ops = Deno.core.ops(); - Deno.core.setAsyncHandler(ops["read"], handleAsyncMsgFromRust); - Deno.core.setAsyncHandler(ops["write"], handleAsyncMsgFromRust); - - Deno.core.print("http_bench.js start\n"); + Deno.core.ops(); const listenerRid = listen(); Deno.core.print(`listening http://127.0.0.1:4544/ rid=${listenerRid}\n`); - while (true) { + + for (;;) { const rid = await accept(listenerRid); - // Deno.core.print(`accepted ${rid}`); if (rid < 0) { Deno.core.print(`accept error ${rid}`); return; |