Skip to content

Commit

Permalink
feat: add bench.todo (#39)
Browse files Browse the repository at this point in the history
* feat: add bench.todo

* docs: update README.md
  • Loading branch information
kamiloox committed Apr 29, 2023
1 parent 3543937 commit 76461a7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 3 deletions.
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()];
}

/**
* 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

0 comments on commit 76461a7

Please sign in to comment.