Skip to content

Commit a655515

Browse files
authored
Merge pull request #18 from berndfuhrmann/issue-17-fn-afterEach-parameters
feat: add parameters for test function and afterEach
2 parents f8334c6 + af0bd1c commit a655515

File tree

2 files changed

+34
-13
lines changed

2 files changed

+34
-13
lines changed

src/index.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export class Measurement {
9090
/**
9191
* Options for Benchmark.measure().
9292
*/
93-
export interface MeasureOptions {
93+
export interface MeasureOptions<BeforeEachReturnType = any, FunctionReturnType = any> {
9494
/**
9595
* The number of times to call the function and measure its duration.
9696
* @default 100
@@ -136,12 +136,12 @@ export interface MeasureOptions {
136136
/**
137137
* Callback to invoke before each iteration.
138138
*/
139-
beforeEach?: () => any;
139+
beforeEach?: () => BeforeEachReturnType;
140140

141141
/**
142142
* Callback to invoke after each iteration.
143143
*/
144-
afterEach?: () => any;
144+
afterEach?: (beforeEachValue: Awaited<BeforeEachReturnType>, functionValue: Awaited<FunctionReturnType>) => any;
145145

146146
/**
147147
* Whether to make use of the options like `meanUnder` and `minUnder`.
@@ -186,12 +186,6 @@ export interface BenchmarkData {
186186
};
187187
}
188188

189-
async function maybePromise(fn: () => any): Promise<void> {
190-
const ret = fn();
191-
if (ret instanceof Promise) {
192-
await ret;
193-
}
194-
}
195189

196190
function round(value: number, places: number = 5): number {
197191
return mathjs.round(value, places) as number;
@@ -203,24 +197,28 @@ function round(value: number, places: number = 5): number {
203197
* @param fn - Function to measure.
204198
* @param options - Options to customize the measurement.
205199
*/
206-
export async function measure(fn: () => any, options: Partial<MeasureOptions> = {}): Promise<Measurement> {
200+
export async function measure<BeforeEachReturnType = any, FunctionReturnType = any>(
201+
fn: (beforeEachValue: Awaited<BeforeEachReturnType>) => FunctionReturnType,
202+
options: Partial<MeasureOptions<BeforeEachReturnType, FunctionReturnType>> = {}
203+
): Promise<Measurement> {
207204
const mergedOptions = { ...defaultMeasureOptions, ...options };
208205
const durations: Array<number> = [];
209206
let calls: Array<Function> = [];
210207

211208
for (let i = 0; i < mergedOptions.iterations; i++) {
212209
calls.push(async () => {
210+
let beforeEachValue: BeforeEachReturnType | undefined = undefined;
213211
if (mergedOptions.beforeEach !== undefined) {
214-
await maybePromise(mergedOptions.beforeEach);
212+
beforeEachValue = await mergedOptions.beforeEach();
215213
}
216214

217215
const startTime = hrtime();
218-
await maybePromise(fn);
216+
const functionValue = await fn(beforeEachValue as Awaited<BeforeEachReturnType>);
219217
const [durationSec, durationNano] = hrtime(startTime);
220218
durations.push(durationSec * 1e3 + durationNano / 1e6);
221219

222220
if (mergedOptions.afterEach !== undefined) {
223-
await maybePromise(mergedOptions.afterEach);
221+
await mergedOptions.afterEach(beforeEachValue as Awaited<BeforeEachReturnType>, functionValue as Awaited<FunctionReturnType>);
224222
}
225223
});
226224
}

tests/index.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,34 @@ describe("measure", () => {
7474
expect(count).toBe(100);
7575
});
7676

77+
it("provides result of beforeEach to function", async () => {
78+
await measure((value) => {
79+
if (value !== 1) {
80+
throw new Error("error");
81+
}
82+
}, {
83+
beforeEach: () => 1
84+
});
85+
});
86+
7787
it("supports an afterEach callback", async () => {
7888
let count = 0;
7989
await measure(() => { }, { afterEach: () => count++ });
8090
expect(count).toBe(100);
8191
});
92+
93+
it("provides result of beforeEach and function to afterEach", async () => {
94+
await measure(() => {
95+
return 2;
96+
}, {
97+
beforeEach: () => 1,
98+
afterEach: (beforeValue, functionValue) => {
99+
if (beforeValue !== 1 || functionValue !== 2) {
100+
throw new Error("error");
101+
}
102+
}
103+
});
104+
});
82105
});
83106

84107
describe("Measurement", () => {

0 commit comments

Comments
 (0)