Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Idea #62

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Idea #62

Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import { createBenchEvent } from './event';
import Task from './task';
import { AddEventListenerOptionsArgument, RemoveEventListenerOptionsArgument } from './types';
import { now } from './utils';
import { now, taskIdFromEnv } from './utils';

/**
* The Benchmark instance for keeping track of the benchmark tasks and controlling
Expand All @@ -20,9 +20,9 @@ export default class Bench extends EventTarget {
/*
* @private the task map
*/
_tasks: Map<string, Task> = new Map();
_tasks: Map<string | number, Task> = new Map();
sirenkovladd marked this conversation as resolved.
Show resolved Hide resolved

_todos: Map<string, Task> = new Map();
_taskIdCounter = 0;

signal?: AbortSignal;

Expand Down Expand Up @@ -75,6 +75,17 @@ export default class Bench extends EventTarget {
async run() {
this.dispatchEvent(createBenchEvent('start'));
const values: Task[] = [];

const taskId = taskIdFromEnv();
if (taskId !== -1) {
const task = this.getTask(taskId);
if (task) {
await task.run();
return [task];
}
return [];
sirenkovladd marked this conversation as resolved.
Show resolved Hide resolved
}

for (const task of [...this._tasks.values()]) {
if (this.signal?.aborted) values.push(task);
else values.push(await task.run());
Expand Down Expand Up @@ -108,7 +119,7 @@ export default class Bench extends EventTarget {
* add a benchmark task to the task map
*/
add(name: string, fn: Fn, opts: FnOptions = {}) {
const task = new Task(this, name, fn, opts);
const task = new Task(this, name, fn, { ...opts, isTodo: false });
this._tasks.set(name, task);
this.dispatchEvent(createBenchEvent('add', task));
return this;
Expand All @@ -119,8 +130,8 @@ export default class Bench extends EventTarget {
*/
// eslint-disable-next-line @typescript-eslint/no-empty-function
todo(name: string, fn: Fn = () => {}, opts: FnOptions = {}) {
const task = new Task(this, name, fn, opts);
this._todos.set(name, task);
const task = new Task(this, name, fn, { ...opts, isTodo: true });
this._tasks.set(name, task);
this.dispatchEvent(createBenchEvent('todo', task));
return this;
}
Expand Down Expand Up @@ -157,9 +168,10 @@ export default class Bench extends EventTarget {
* table of the tasks results
*/
table() {
return this.tasks.map(({ name, result }) => {
return this.tasks.map(({ name, id, result }) => {
if (result) {
return {
TaskId: id,
'Task Name': name,
'ops/sec': result.error ? 'NaN' : parseInt(result.hz.toString(), 10).toLocaleString(),
'Average Time (ns)': result.error ? 'NaN' : result.mean * 1000 * 1000,
Expand All @@ -175,24 +187,29 @@ export default class Bench extends EventTarget {
* (getter) tasks results as an array
*/
get results(): (TaskResult | undefined)[] {
return [...this._tasks.values()].map((task) => task.result);
return this.tasks.map((task) => task.result);
}

/**
* (getter) tasks as an array
*/
get tasks(): Task[] {
return [...this._tasks.values()];
return [...this._tasks.values()].filter((task) => !task.isTodo);
}

get todos(): Task[] {
return [...this._todos.values()];
return [...this._tasks.values()].filter((task) => task.isTodo);
}

/**
* get a task based on the task name
* get a task based on the task name or task id
*/
getTask(name: string): Task | undefined {
return this._tasks.get(name);
getTask(t: string | number): Task | undefined {
if (typeof t === 'string') {
return this._tasks.get(t);
}
return [...this._tasks.values()].filter(
(task) => task.id === t,
)[0];
sirenkovladd marked this conversation as resolved.
Show resolved Hide resolved
}
}
10 changes: 10 additions & 0 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,22 @@ export default class Task extends EventTarget {
*/
opts: FnOptions;

/**
* Indicate if the task a todo task
*/
isTodo: boolean;

id: number;

constructor(bench: Bench, name: string, fn: Fn, opts: FnOptions = {}) {
super();
this.bench = bench;
this.name = name;
this.fn = fn;
this.opts = opts;
this.isTodo = opts.isTodo ?? false;
this.id = bench._taskIdCounter++; // eslint-disable-line no-param-reassign
sirenkovladd marked this conversation as resolved.
Show resolved Hide resolved

// TODO: support signals in Tasks
}

Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ export interface FnOptions {
* An optional function that is run after all iterations of this task end
*/
afterAll?: (this: Task) => void | Promise<void>;

/**
* Task is a todo
*/
isTodo?: boolean;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,19 @@ export const isAsyncTask = async (task: Task) => {
return false;
}
};

export const isNode = (() => process?.release.name === 'node')();

export const taskIdFromEnv = (): number => {
if (
isNode
&& process.env.TINYBENCH_TASK_ID
) {
return parseInt(process.env.TINYBENCH_TASK_ID, 10);
}
return -1;
};

export const isESM = (() => typeof __dirname !== 'string')();

export const isCJS = !isESM;
Loading