-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benchmarkAsync to BenchmarkUtils
- Loading branch information
Showing
5 changed files
with
55 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* Measures the execution time of an asynchronous function. | ||
* @param fn - The asynchronous function to be benchmarked. | ||
* @param args - The arguments to be passed to the function. | ||
* @returns A promise that resolves with the execution time in milliseconds. | ||
*/ | ||
export function benchmarkAsync(fn: Function, ...args: any[]): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
const start = performance.now(); | ||
fn(...args) | ||
.then(() => { | ||
const end = performance.now(); | ||
resolve(end - start); | ||
}) | ||
.catch(reject); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,15 @@ | ||
|
||
import { benchmarkAsync } from "./benchmarkAsync"; | ||
import { benchmarkIterations } from "./benchmarkIterations"; | ||
import { benchmark } from "./benchmark"; | ||
|
||
export class BenchmarkUtils { | ||
static benchmarkAsync: typeof benchmarkAsync = benchmarkAsync; | ||
static benchmarkIterations: typeof benchmarkIterations = benchmarkIterations; | ||
static benchmark: typeof benchmark = benchmark; | ||
} | ||
|
||
export { benchmarkAsync } from "./benchmarkAsync"; | ||
export { benchmarkIterations } from "./benchmarkIterations"; | ||
export { benchmark } from "./benchmark"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect, describe, it } from "bun:test"; | ||
import { benchmarkAsync } from "../../src/benchmark/benchmarkAsync"; | ||
|
||
describe("benchmarkAsync", () => { | ||
it("should measure the execution time of an asynchronous function", async () => { | ||
const fn = async () => { | ||
setTimeout(() => {}, 1000); | ||
}; | ||
|
||
const time = await benchmarkAsync(fn); | ||
expect(time).toBeGreaterThan(0); | ||
}); | ||
|
||
it("should measure the execution time of an asynchronous function with arguments", async () => { | ||
const fn = async (a: number, b: number) => { | ||
setTimeout(() => {}, 1000); | ||
}; | ||
|
||
const time = await benchmarkAsync(fn, 5, 10); | ||
expect(time).toBeGreaterThan(0); | ||
}); | ||
}); |