|
1 | 1 | import { expect, test, vi } from 'vitest';
|
2 |
| -import { Bench, type Task } from '../src'; |
| 2 | +import { Bench, type Task, hrtimeNow } from '../src'; |
3 | 3 |
|
4 |
| -test('basic', async () => { |
| 4 | +test('now() basic', async () => { |
5 | 5 | const bench = new Bench({ time: 100 });
|
6 | 6 | bench
|
7 | 7 | .add('foo', async () => {
|
@@ -36,6 +36,41 @@ test('basic', async () => {
|
36 | 36 | ).toBeCloseTo(1000, 1);
|
37 | 37 | });
|
38 | 38 |
|
| 39 | +test('hrtimeNow() basic', async () => { |
| 40 | + const bench = new Bench({ time: 100, now: hrtimeNow }); |
| 41 | + bench |
| 42 | + .add('foo', async () => { |
| 43 | + await new Promise((resolve) => setTimeout(resolve, 50)); |
| 44 | + }) |
| 45 | + .add('bar', async () => { |
| 46 | + await new Promise((resolve) => setTimeout(resolve, 100)); |
| 47 | + }); |
| 48 | + |
| 49 | + await bench.run(); |
| 50 | + |
| 51 | + const { tasks } = bench; |
| 52 | + |
| 53 | + expect(tasks.length).toEqual(2); |
| 54 | + |
| 55 | + expect(tasks[0]?.name).toEqual('foo'); |
| 56 | + expect(tasks[0]?.result?.totalTime).toBeGreaterThan(50); |
| 57 | + expect(tasks[0]?.result?.latency.mean).toBeGreaterThan(50); |
| 58 | + // throughput mean is ops/s, period is ms unit value |
| 59 | + expect( |
| 60 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 61 | + tasks[0]!.result!.throughput.mean * tasks[0]!.result!.period, |
| 62 | + ).toBeCloseTo(1000, 1); |
| 63 | + |
| 64 | + expect(tasks[1]?.name).toEqual('bar'); |
| 65 | + expect(tasks[1]?.result?.totalTime).toBeGreaterThan(100); |
| 66 | + expect(tasks[1]?.result?.latency.mean).toBeGreaterThan(100); |
| 67 | + // throughput mean is ops/s, period is ms unit value |
| 68 | + expect( |
| 69 | + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
| 70 | + tasks[1]!.result!.throughput.mean * tasks[1]!.result!.period, |
| 71 | + ).toBeCloseTo(1000, 1); |
| 72 | +}); |
| 73 | + |
39 | 74 | test('bench and task runs, time consistency', async () => {
|
40 | 75 | const bench = new Bench({ time: 100, iterations: 15 });
|
41 | 76 | bench.add('foo', async () => {
|
|
0 commit comments