Skip to content

Commit

Permalink
fix: example eslint config and task time calculate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kainstar authored and Aslemammad committed Oct 18, 2022
1 parent 04b2cda commit 9ceac22
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 18 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": "tsconfig.json"
"project": ["tsconfig.json", "examples/tsconfig.json"]
},
"plugins": [
"@typescript-eslint/eslint-plugin"
Expand Down
38 changes: 31 additions & 7 deletions examples/src/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,40 @@ bench
a = b + a;
b = a - b;
a = b - a;
})
.add('async switch 1', async () => {
let a = 1;
let b = 2;
const c = a;
a = b;
b = c;
})
.add('async switch 2', async () => {
let a = 1;
let b = 10;
a = b + a;
b = a - b;
a = b - a;
});

await bench.run();

console.table(bench.tasks.map(({ name, result }) => ({ "Task Name": name, "Average Time (ps)": result?.mean! * 1000, "Variance (ps)": result?.variance! * 1000 })));
console.table(
bench.tasks.map(({ name, result }) => (result ? ({
'Task Name': name,
'ops/sec': result.hz,
'Average Time (ps)': result.mean * 1000,
'Variance (ps)': result.variance * 1000,
Samples: result.samples.length,
}) : null)),
);

// Output:
// ┌─────────┬────────────┬────────────────────┬────────────────────┐
// │ (index) │ Task Name │ Average Time (ps) │ Variance (ps) │
// ├─────────┼────────────┼────────────────────┼────────────────────┤
// │ 0 │ 'switch 1' │ 1.8458325710527104 │ 1.2113875253341617 │
// │ 1 │ 'switch 2' │ 1.8746935152109603 │ 1.2254725890767446 │
// └─────────┴────────────┴────────────────────┴────────────────────┘
// ┌─────────┬──────────────────┬────────────────────┬─────────────────────┬───────────────────────┬─────────┐
// │ (index) │ Task Name │ ops/sec │ Average Time (ps) │ Variance (ps) │ Samples │
// ├─────────┼──────────────────┼────────────────────┼─────────────────────┼───────────────────────┼─────────┤
// │ 0 │ 'switch 1' │ 14463.371878123484 │ 0.06914017066190119 │ 0.006014460930767669 │ 414836 │
// │ 1 │ 'switch 2' │ 14880.867755337544 │ 0.06720038215790977 │ 0.0004529273320957288 │ 458644 │
// │ 2 │ 'async switch 1' │ 7970.65255150332 │ 0.1254602422497256 │ 0.010617119343817942 │ 367049 │
// │ 3 │ 'async switch 2' │ 8702.849913513868 │ 0.11490488862127687 │ 0.002613875857497825 │ 389356 │
// └─────────┴──────────────────┴────────────────────┴─────────────────────┴───────────────────────┴─────────┘
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"version": "2.3.0",
"type": "module",
"packageManager": "pnpm@7.5.1",
"engines": {
"node": ">= 10.0.0"
},
"scripts": {
"dev": "tsup --watch",
"build": "tsup",
Expand Down
23 changes: 13 additions & 10 deletions src/task.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type {
Fn, TaskEvents, TaskResult, TaskEventsMap,
} from 'types/index';
import { types } from 'util';
import Bench from './bench';
import tTable from './constants';
import { createBenchEvent } from './event';
import { getMean, getVariance } from './utils';
import { getMean, getSum, getVariance } from './utils';

/**
* A class that represents each benchmark task in Tinybench. It keeps track of the
Expand Down Expand Up @@ -55,20 +56,22 @@ export default class Task extends EventTarget {
&& !this.bench.signal?.aborted
) {
let taskStart = 0;
const isAsync = types.isAsyncFunction(this.fn);

try {
// eslint-disable-next-line no-await-in-loop
await Promise.resolve().then(() => {
taskStart = this.bench.now();
return this.fn();
});
taskStart = this.bench.now();
if (isAsync) {
await this.fn();
} else {
this.fn();
}
const taskTime = this.bench.now() - taskStart;
this.runs += 1;
samples.push(taskTime);
} catch (e) {
this.setResult({ error: e });
}

const taskTime = this.bench.now() - taskStart;
this.runs += 1;
samples.push(taskTime);
totalTime = this.bench.now() - startTime;
}
await this.bench.teardown(this, 'run');
Expand All @@ -78,7 +81,7 @@ export default class Task extends EventTarget {
{
const min = samples[0]!;
const max = samples[samples.length - 1]!;
const period = totalTime / this.runs;
const period = getSum(samples) / this.runs;
const hz = 1 / period;

// benchmark.js: https://github.com/bestiejs/benchmark.js/blob/42f3b732bac3640eddb3ae5f50e445f3141016fd/benchmark.js#L1912-L1927
Expand Down
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,8 @@ export const getVariance = (samples: number[], mean: number) => {
const result = samples.reduce((sum, n) => sum + ((n - mean) ** 2));
return result / (samples.length - 1) || 0;
};

/**
* Computes the sum of a sample
*/
export const getSum = (samples: number[]) => samples.reduce((sum, n) => sum + n, 0);

0 comments on commit 9ceac22

Please sign in to comment.