From 7a3df0a18465ceebe43f3183daa2f9397c4e5ebb Mon Sep 17 00:00:00 2001 From: andy finch Date: Fri, 5 Apr 2019 15:57:59 -0400 Subject: [PATCH] Add worker benchmarks (#2059) --- js/workers.ts | 9 +++- tests/subdir/bench_worker.ts | 20 ++++++++ tests/workers_round_robin_bench.ts | 75 ++++++++++++++++++++++++++++++ tests/workers_startup_bench.ts | 25 ++++++++++ tools/benchmark.py | 2 + website/benchmarks.html | 11 ++++- 6 files changed, 139 insertions(+), 3 deletions(-) create mode 100644 tests/subdir/bench_worker.ts create mode 100644 tests/workers_round_robin_bench.ts create mode 100644 tests/workers_startup_bench.ts diff --git a/js/workers.ts b/js/workers.ts index 601ffa0b15..8c08a8506b 100644 --- a/js/workers.ts +++ b/js/workers.ts @@ -150,11 +150,13 @@ export interface Worker { onmessage?: (e: { data: any }) => void; onmessageerror?: () => void; postMessage(data: any): void; + closed: Promise; } export class WorkerImpl implements Worker { private readonly rid: number; private isClosing: boolean = false; + private readonly isClosedPromise: Promise; public onerror?: () => void; public onmessage?: (data: any) => void; public onmessageerror?: () => void; @@ -162,11 +164,16 @@ export class WorkerImpl implements Worker { constructor(specifier: string) { this.rid = createWorker(specifier); this.run(); - hostGetWorkerClosed(this.rid).then(() => { + this.isClosedPromise = hostGetWorkerClosed(this.rid); + this.isClosedPromise.then(() => { this.isClosing = true; }); } + get closed(): Promise { + return this.isClosedPromise; + } + postMessage(data: any): void { hostPostMessage(this.rid, data); } diff --git a/tests/subdir/bench_worker.ts b/tests/subdir/bench_worker.ts new file mode 100644 index 0000000000..6dd2f95415 --- /dev/null +++ b/tests/subdir/bench_worker.ts @@ -0,0 +1,20 @@ +onmessage = function(e) { + const { cmdId, action, data } = e.data; + switch (action) { + case 0: // Static response + postMessage({ + cmdId, + data: "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n" + }); + break; + case 1: // Respond with request data + postMessage({ cmdId, data }); + break; + case 2: // Ping + postMessage({ cmdId }); + break; + case 3: // Close + workerClose(); + break; + } +}; diff --git a/tests/workers_round_robin_bench.ts b/tests/workers_round_robin_bench.ts new file mode 100644 index 0000000000..818a1145fa --- /dev/null +++ b/tests/workers_round_robin_bench.ts @@ -0,0 +1,75 @@ +// Benchmark measures time it takes to send a message to a group of workers one +// at a time and wait for a response from all of them. Just a general +// throughput and consistency benchmark. +const data = "HTTP/1.1 200 OK\r\nContent-Length: 12\r\n\r\nHello World\n"; +const workerCount = 4; +const cmdsPerWorker = 400; + +export interface ResolvableMethods { + resolve: (value?: T | PromiseLike) => void; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + reject: (reason?: any) => void; +} + +export type Resolvable = Promise & ResolvableMethods; + +export function createResolvable(): Resolvable { + let methods: ResolvableMethods; + const promise = new Promise((resolve, reject) => { + methods = { resolve, reject }; + }); + // TypeScript doesn't know that the Promise callback occurs synchronously + // therefore use of not null assertion (`!`) + return Object.assign(promise, methods!) as Resolvable; +} + +function handleAsyncMsgFromWorker( + promiseTable: Map>, + msg: { cmdId: number; data: string } +): void { + const promise = promiseTable.get(msg.cmdId); + if (promise === null) { + throw new Error(`Failed to find promise: cmdId: ${msg.cmdId}, msg: ${msg}`); + } + promise.resolve(data); +} + +async function main(): Promise { + const workers: Array<[Map>, Worker]> = []; + for (var i = 1; i <= workerCount; ++i) { + const worker = new Worker("tests/subdir/bench_worker.ts"); + const promise = new Promise(resolve => { + worker.onmessage = e => { + if (e.data.cmdId === 0) resolve(); + }; + }); + worker.postMessage({ cmdId: 0, action: 2 }); + await promise; + workers.push([new Map(), worker]); + } + // assign callback function + for (const [promiseTable, worker] of workers) { + worker.onmessage = e => { + handleAsyncMsgFromWorker(promiseTable, e.data); + }; + } + for (const cmdId of Array(cmdsPerWorker).keys()) { + const promises: Array> = []; + for (const [promiseTable, worker] of workers) { + const promise = createResolvable(); + promiseTable.set(cmdId, promise); + worker.postMessage({ cmdId: cmdId, action: 1, data }); + promises.push(promise); + } + for (const promise of promises) { + await promise; + } + } + for (const [, worker] of workers) { + worker.postMessage({ action: 3 }); + await worker.closed; // Required to avoid a cmdId not in table error. + } + console.log("Finished!"); +} + +main(); diff --git a/tests/workers_startup_bench.ts b/tests/workers_startup_bench.ts new file mode 100644 index 0000000000..46b2b28012 --- /dev/null +++ b/tests/workers_startup_bench.ts @@ -0,0 +1,25 @@ +// Benchmark measures time it takes to start and stop a number of workers. +const workerCount = 50; + +async function bench(): Promise { + const workers: Worker[] = []; + for (var i = 1; i <= workerCount; ++i) { + const worker = new Worker("tests/subdir/bench_worker.ts"); + const promise = new Promise(resolve => { + worker.onmessage = e => { + if (e.data.cmdId === 0) resolve(); + }; + }); + worker.postMessage({ cmdId: 0, action: 2 }); + await promise; + workers.push(worker); + } + console.log("Done creating workers closing workers!"); + for (const worker of workers) { + worker.postMessage({ action: 3 }); + await worker.closed; // Required to avoid a cmdId not in table error. + } + console.log("Finished!"); +} + +bench(); diff --git a/tools/benchmark.py b/tools/benchmark.py index 772e35d408..db2ef0fb2d 100755 --- a/tools/benchmark.py +++ b/tools/benchmark.py @@ -25,6 +25,8 @@ exec_time_benchmarks = [ ("error_001", ["tests/error_001.ts"]), ("cold_hello", ["tests/002_hello.ts", "--reload"]), ("cold_relative_import", ["tests/003_relative_import.ts", "--reload"]), + ("workers_startup", ["tests/workers_startup_bench.ts"]), + ("workers_round_robin", ["tests/workers_round_robin_bench.ts"]), ] gh_pages_data_file = "gh-pages/data.json" diff --git a/website/benchmarks.html b/website/benchmarks.html index 4d6d543dc6..560b96c7cb 100644 --- a/website/benchmarks.html +++ b/website/benchmarks.html @@ -29,11 +29,18 @@ href="https://github.com/denoland/deno/blob/master/tests/002_hello.ts" > tests/002_hello.ts - - and + , tests/003_relative_import.ts, + tests/worker_round_robin_bench.ts, and + tests/worker_startup_bench.ts. For deno to execute typescript, it must first compile it to JS. A warm startup is when deno has a cached JS output already, so it should be fast because it bypasses the TS compiler. A cold startup is when deno