summaryrefslogtreecommitdiff
path: root/cli/tests/workers_startup_bench.ts
blob: 60c15a4b1d3e3c9fb9c98c76ceb28ade63f6a1d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 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 (let i = 1; i <= workerCount; ++i) {
    const worker = new Worker("./subdir/bench_worker.ts", { type: "module" });
    const promise = new Promise((resolve): void => {
      worker.onmessage = (e): void => {
        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) {
    const promise = new Promise((resolve): void => {
      worker.onmessage = (e): void => {
        if (e.data.cmdId === 3) resolve();
      };
    });
    worker.postMessage({ action: 3 });
    await promise;
  }
  console.log("Finished!");
}

bench();