mirror of
https://github.com/denoland/deno.git
synced 2024-12-24 16:19:12 -05:00
Add worker benchmarks (#2059)
This commit is contained in:
parent
031411b449
commit
7a3df0a184
6 changed files with 139 additions and 3 deletions
|
@ -150,11 +150,13 @@ export interface Worker {
|
|||
onmessage?: (e: { data: any }) => void;
|
||||
onmessageerror?: () => void;
|
||||
postMessage(data: any): void;
|
||||
closed: Promise<void>;
|
||||
}
|
||||
|
||||
export class WorkerImpl implements Worker {
|
||||
private readonly rid: number;
|
||||
private isClosing: boolean = false;
|
||||
private readonly isClosedPromise: Promise<void>;
|
||||
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<void> {
|
||||
return this.isClosedPromise;
|
||||
}
|
||||
|
||||
postMessage(data: any): void {
|
||||
hostPostMessage(this.rid, data);
|
||||
}
|
||||
|
|
20
tests/subdir/bench_worker.ts
Normal file
20
tests/subdir/bench_worker.ts
Normal file
|
@ -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;
|
||||
}
|
||||
};
|
75
tests/workers_round_robin_bench.ts
Normal file
75
tests/workers_round_robin_bench.ts
Normal file
|
@ -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<T> {
|
||||
resolve: (value?: T | PromiseLike<T>) => void;
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
reject: (reason?: any) => void;
|
||||
}
|
||||
|
||||
export type Resolvable<T> = Promise<T> & ResolvableMethods<T>;
|
||||
|
||||
export function createResolvable<T>(): Resolvable<T> {
|
||||
let methods: ResolvableMethods<T>;
|
||||
const promise = new Promise<T>((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<T>;
|
||||
}
|
||||
|
||||
function handleAsyncMsgFromWorker(
|
||||
promiseTable: Map<number, Resolvable<string>>,
|
||||
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<void> {
|
||||
const workers: Array<[Map<number, Resolvable<string>>, 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<Promise<string>> = [];
|
||||
for (const [promiseTable, worker] of workers) {
|
||||
const promise = createResolvable<string>();
|
||||
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();
|
25
tests/workers_startup_bench.ts
Normal file
25
tests/workers_startup_bench.ts
Normal file
|
@ -0,0 +1,25 @@
|
|||
// Benchmark measures time it takes to start and stop a number of workers.
|
||||
const workerCount = 50;
|
||||
|
||||
async function bench(): Promise<void> {
|
||||
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();
|
|
@ -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"
|
||||
|
|
|
@ -29,11 +29,18 @@
|
|||
href="https://github.com/denoland/deno/blob/master/tests/002_hello.ts"
|
||||
>
|
||||
tests/002_hello.ts
|
||||
</a>
|
||||
and
|
||||
</a>,
|
||||
<a
|
||||
href="https://github.com/denoland/deno/blob/master/tests/003_relative_import.ts"
|
||||
>tests/003_relative_import.ts</a
|
||||
>,
|
||||
<a
|
||||
href="https://github.com/denoland/deno/blob/master/tests/worker_round_robin_bench.ts"
|
||||
>tests/worker_round_robin_bench.ts</a
|
||||
>, and
|
||||
<a
|
||||
href="https://github.com/denoland/deno/blob/master/tests/worker_startup_bench.ts"
|
||||
>tests/worker_startup_bench.ts</a
|
||||
>. 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
|
||||
|
|
Loading…
Reference in a new issue