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

feat: add bench.todo #39

Merged
merged 3 commits into from
Apr 29, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bench
await new Promise(r => setTimeout(r, 1)) // we wait 1ms :)
console.log('I am slower')
})
.todo('unimplemented bench')

await bench.run();

Expand All @@ -51,6 +52,19 @@ console.table(bench.table());
// │ 0 │ 'faster task' │ '41,621' │ 24025.791819761525 │ '±20.50%' │ 4257 │
// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │
// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘

console.table(
bench.todos.map(({ name }) => ({
'Task name': name,
})),
);

// Output:
// ┌─────────┬───────────────────────┐
// │ (index) │ Task name │
// ├─────────┼───────────────────────┤
// │ 0 │ 'unimplemented bench' │
// └─────────┴───────────────────────┘
```

The `add` method accepts a task name and a task function, so it can benchmark
Expand Down Expand Up @@ -133,6 +147,8 @@ export type Hook = (task: Task, mode: "warmup" | "run") => void | Promise<void>;
- `get results(): (TaskResult | undefined)[]`: (getter) tasks results as an array
- `get tasks(): Task[]`: (getter) tasks as an array
- `getTask(name: string): Task | undefined`: get a task based on the name
- `todo(name: string, fn?: Fn, opts: FnOptions)`: add a benchmark todo to the todo map
- `get todos(): Task[]`: (getter) tasks todos as an array

### `Task`

Expand Down Expand Up @@ -298,7 +314,8 @@ export type BenchEvents =
| "warmup" // when the benchmarks start getting warmed up (before start)
| "cycle" // when running each benchmark task gets done (cycle)
| "add" // when a Task gets added to the Bench
| "remove"; // when a Task gets removed of the Bench
| "remove" // when a Task gets removed of the Bench
| "todo"; // when a todo Task gets added to the Bench

/**
* task events
Expand Down
16 changes: 15 additions & 1 deletion examples/src/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ bench
.add('slower task', async () => {
await new Promise((r) => setTimeout(r, 1)); // we wait 1ms :)
console.log('I am slower');
});
})
.todo('unimplemented bench');

await bench.run();

Expand All @@ -22,3 +23,16 @@ console.table(bench.table());
// │ 0 │ 'faster task' │ '41,621' │ 24025.791819761525 │ '±20.50%' │ 4257 │
// │ 1 │ 'slower task' │ '828' │ 1207382.7838323202 │ '±7.07%' │ 83 │
// └─────────┴───────────────┴──────────┴────────────────────┴───────────┴─────────┘

console.table(
bench.todos.map(({ name }) => ({
'Task name': name,
})),
);

// Output:
// ┌─────────┬───────────────────────┐
// │ (index) │ Task name │
// ├─────────┼───────────────────────┤
// │ 0 │ 'unimplemented bench' │
// └─────────┴───────────────────────┘
17 changes: 17 additions & 0 deletions src/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default class Bench extends EventTarget {
*/
_tasks: Map<string, Task> = new Map();

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

signal?: AbortSignal;

warmupTime = 100;
Expand Down Expand Up @@ -108,6 +110,17 @@ export default class Bench extends EventTarget {
return this;
}

/**
* add a benchmark todo to the todo map
*/
// 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);
this.dispatchEvent(createBenchEvent('todo', task));
return this;
}

/**
* remove a benchmark task from the task map
*/
Expand Down Expand Up @@ -166,6 +179,10 @@ export default class Bench extends EventTarget {
return [...this._tasks.values()];
}

get todos(): Task[] {
return [...this._todos.values()];
}

kamiloox marked this conversation as resolved.
Show resolved Hide resolved
/**
* get a task based on the task name
*/
Expand Down
15 changes: 15 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ test('events order 2', async () => {
await new Promise((resolve) => setTimeout(resolve, 150));
});

test('todo event', async () => {
const bench = new Bench({ time: 50 });

let todoTask: Task;
bench.addEventListener('todo', ({ task }) => {
todoTask = task;
});

bench.todo('unimplemented bench');

await bench.run();

expect(todoTask!.name).toBe('unimplemented bench');
});

test('error event', async () => {
const bench = new Bench({ time: 50 });
const err = new Error();
Expand Down
4 changes: 3 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ export type BenchEvents =
| 'warmup' // when the benchmarks start getting warmed up (before start)
| 'cycle' // when running each benchmark task gets done (cycle)
| 'add' // when a Task gets added to the Bench
| 'remove'; // when a Task gets removed of the Bench
| 'remove' // when a Task gets removed of the Bench
| 'todo'; // when a todo Task gets added to the Bench

export type Hook = (task: Task, mode: 'warmup' | 'run') => void | Promise<void>;

Expand All @@ -162,6 +163,7 @@ export interface BenchEventsMap{
remove: TaskEventListener
cycle: TaskEventListener
error: TaskEventListener
todo: TaskEventListener
}

/**
Expand Down