Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Aslemammad committed Jun 19, 2023
1 parent 0efea3a commit 63a7f86
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 29 deletions.
48 changes: 25 additions & 23 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,36 +65,33 @@ export default class Task extends EventTarget {
await this.opts.beforeAll.call(this);
}

while (
(totalTime < this.bench.time || this.runs < this.bench.iterations)
&& !this.bench.signal?.aborted
) {
if (this.opts.beforeEach != null) {
await this.opts.beforeEach.call(this);
}

let taskStart = 0;
let taskTime = 0;
try {
while (
(totalTime < this.bench.time || this.runs < this.bench.iterations)
&& !this.bench.signal?.aborted
) {
if (this.opts.beforeEach != null) {
await this.opts.beforeEach.call(this);
}

try {
taskStart = this.bench.now();
const taskStart = this.bench.now();
if (isAsync) {
await this.fn();
} else {
this.fn();
}
taskTime = this.bench.now() - taskStart;
} catch (e) {
this.setResult({ error: e });
}
const taskTime = this.bench.now() - taskStart;

totalTime += taskTime;
this.runs += 1;
samples.push(taskTime);
samples.push(taskTime);
this.runs += 1;
totalTime += taskTime;

if (this.opts.afterEach != null) {
await this.opts.afterEach.call(this);
if (this.opts.afterEach != null) {
await this.opts.afterEach.call(this);
}
}
} catch (e) {
this.setResult({ error: e });
}

if (this.opts.afterAll != null) {
Expand All @@ -105,7 +102,7 @@ export default class Task extends EventTarget {

samples.sort((a, b) => a - b);

{
if (!this.result?.error) {
const min = samples[0]!;
const max = samples[samples.length - 1]!;
const period = totalTime / this.runs;
Expand Down Expand Up @@ -174,6 +171,7 @@ export default class Task extends EventTarget {
*/
async warmup() {
this.dispatchEvent(createBenchEvent('warmup', this));
const isAsync = isAsyncFunction(this.fn);
const startTime = this.bench.now();
let totalTime = 0;

Expand All @@ -194,7 +192,11 @@ export default class Task extends EventTarget {

try {
// eslint-disable-next-line no-await-in-loop
await Promise.resolve().then(this.fn);
if (isAsync) {
await this.fn();
} else {
this.fn();
}
} catch {
// todo
}
Expand Down
14 changes: 8 additions & 6 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,10 @@ test('setup and teardown', async () => {
});

test('task beforeAll, afterAll, beforeEach, afterEach', async () => {
const bench = new Bench({ time: 50 });
const iterations = 100;
const bench = new Bench({
time: 0, warmupTime: 0, iterations, warmupIterations: iterations,
});

const beforeAll = vi.fn(function hook(this: Task) {
expect(this).toBe(bench.getTask('foo'));
Expand All @@ -303,14 +306,13 @@ test('task beforeAll, afterAll, beforeEach, afterEach', async () => {
beforeEach,
afterEach,
});
const task = bench.getTask('foo')!;

await bench.warmup();
await bench.run();

expect(beforeAll.mock.calls.length).toBe(1);
expect(afterAll.mock.calls.length).toBe(1);
expect(beforeEach.mock.calls.length).toBe(task.runs);
expect(afterEach.mock.calls.length).toBe(task.runs);
expect(beforeAll.mock.calls.length).toBe(2);
expect(afterAll.mock.calls.length).toBe(2);
expect(beforeEach.mock.calls.length).toBe(iterations * 2 /* warmup + run */);
expect(afterEach.mock.calls.length).toBe(iterations * 2);
expect(beforeEach.mock.calls.length).toBe(afterEach.mock.calls.length);
});

0 comments on commit 63a7f86

Please sign in to comment.