Skip to content

Commit

Permalink
feat(examples): Add examples, and taskName to easily print the benchm…
Browse files Browse the repository at this point in the history
…arking results
  • Loading branch information
ecstrema authored and Aslemammad committed Aug 31, 2022
1 parent 21d1538 commit 7937010
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 55 deletions.
40 changes: 34 additions & 6 deletions README.md
Expand Up @@ -27,18 +27,37 @@ You can start benchmarking by instantiating the `Bench` class and adding
benchmark tasks to it.

```ts
const { Bench } = require("tinybench");
import { Bench } from 'tinybench';

const bench = new Bench({ time: 100 });

bench
.add("foo", () => {
// code
.add('switch 1', () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add("bar", async () => {
// code
.add('switch 2', () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

await bench.run();

console.table(bench.results.map((result) => ({ "Task Name": result?.taskName, "Average Time (ps)": result?.mean! * 1000, "Variance (ps)": result?.variance! * 1000 })));

// Output:
// β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
// β”‚ (index) β”‚ Task Name β”‚ Average Time (ps) β”‚ Variance (ps) β”‚
// β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
// β”‚ 0 β”‚ 'switch 1' β”‚ 1.8458325710527104 β”‚ 1.2113875253341617 β”‚
// β”‚ 1 β”‚ 'switch 2' β”‚ 1.8746935152109603 β”‚ 1.2254725890767446 β”‚
// β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
```

The `add` method accepts a task name and a task function, so it can benchmark
Expand All @@ -48,6 +67,9 @@ use it to create an another task for that instance.
Note that the task name should always be unique in an instance, because Tinybench stores the tasks based
on their names in a `Map`.

Also note that `tinybench` does not log any result by default. You can extract the relevant stats
from `bench.results` after running the benchmark, and process them however you want.

## Docs

### `Bench`
Expand Down Expand Up @@ -135,7 +157,13 @@ function has been executed.
the benchmark task result object.

```ts
export type TaskResult = {
export type ITaskResult = {

/**
* The name of the task. Provided for commodity.
*/
taskName: string;

/*
* the last error that was thrown while running the task
*/
Expand Down
6 changes: 6 additions & 0 deletions examples/.eslintrc.json
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc.json",
"include": [
"src/*.ts"
]
}
15 changes: 15 additions & 0 deletions examples/package.json
@@ -0,0 +1,15 @@
{
"name": "examples",
"version": "0.0.0",
"type": "module",
"scripts": {
"build": "tsc",
"simple": "tsx src/simple.ts",
"dev:simple": "tsx watch src/simple.ts"
},
"license": "ISC",
"devDependencies": {
"tsx": "^3.9.0",
"typescript": "^4.5.4"
}
}
31 changes: 31 additions & 0 deletions examples/src/simple.ts
@@ -0,0 +1,31 @@
import { Bench } from '../../src';

const bench = new Bench({ time: 100 });

bench
.add('switch 1', () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add('switch 2', () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

await bench.run();

console.table(bench.results.map((result) => ({ "Task Name": result?.taskName, "Average Time (ps)": result?.mean! * 1000, "Variance (ps)": result?.variance! * 1000 })));

// Output:
// β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
// β”‚ (index) β”‚ Task Name β”‚ Average Time (ps) β”‚ Variance (ps) β”‚
// β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
// β”‚ 0 β”‚ 'switch 1' β”‚ 1.8458325710527104 β”‚ 1.2113875253341617 β”‚
// β”‚ 1 β”‚ 'switch 2' β”‚ 1.8746935152109603 β”‚ 1.2254725890767446 β”‚
// β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
16 changes: 16 additions & 0 deletions examples/tsconfig.json
@@ -0,0 +1,16 @@
{
"extends": "../tsconfig.json",
"include": [
"src/*.ts"
],
"exclude": [
"node_modules",
"dist"
],
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"baseUrl": ".",
"outDir": "dist"
},
}

0 comments on commit 7937010

Please sign in to comment.