tinylibs / tinybench

🔎 A simple, tiny and lightweight benchmarking library!
MIT License
1.73k stars 36 forks source link

Idea #62

Open Uzlopak opened 10 months ago

Uzlopak commented 10 months ago

We can load childProcess like this

import createBenchEvent from "./event";
import { isESM, isNode, taskIdFromEnv } from "./utils";
import Bench from "./bench";
import Task from "./task";

let childProcess = null;

// in bench.ts
  async run() {
    this.dispatchEvent(createBenchEvent('start'));
    const values: Task[] = [];

    if (!isNode) {
      for (const task of [...this._tasks.values()]) {
        if (this.signal?.aborted) values.push(task);
        else values.push(await task.run());
      }
    } else {
      const taskId = taskIdFromEnv();
      if (taskId !== -1) {
        const task = this.getTask(taskId);
        if (task) {
          await task.run();
          return JSON.stringify(task.result);
        }
        return [];
      } else {
        // @ts-ignore
        childProcess ??= isESM ? await import('node:child_process') : require('node:child_process');
        for (const task of [...this._tasks.values()]) {
          const result = childProcess.execSync(`TINYBENCH_TASK_ID=${task.id} ${process.argv.join(' ')}`, { env: process.env });
          task.result = JSON.parse(result.toString());
        }
      }
    }
Uzlopak commented 10 months ago

I cant get it to work properly. :D

Maybe somebody else has to take over.

sirenkovladd commented 10 months ago

Do you consider the possibility that tasks can be added in a different order at each launch? so the id will be different on each run

Aslemammad commented 8 months ago

Any summary on this PR?