Skip to content

Commit

Permalink
Add benchmarkAsync to BenchmarkUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
0pilatos0 committed Jan 27, 2024
1 parent 70d90f9 commit 0b6084d
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 1 deletion.
12 changes: 12 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Here are some of the utility functions and classes available in this library:
## Table of Contents

- [BenchmarkUtils](#benchmarkutils)
- [benchmarkAsync](#benchmarkasync)
- [benchmarkIterations](#benchmarkiterations)
- [benchmark](#benchmark)

Expand Down Expand Up @@ -89,6 +90,17 @@ Here are some of the utility functions and classes available in this library:
the following utility functions are available in the `BenchmarkUtils` class, they can be used as a static method on the `BenchmarkUtils` class or as a standalone function.


### benchmarkAsync
```typescript
/**
* 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.
*/
```


### benchmarkIterations
```typescript
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typelegend",
"version": "0.1.10",
"version": "0.1.11",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
17 changes: 17 additions & 0 deletions src/benchmark/benchmarkAsync.ts
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);
});
}
3 changes: 3 additions & 0 deletions src/benchmark/index.ts
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";

22 changes: 22 additions & 0 deletions test/benchmark/benchmarkAsync.test.ts
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);
});
});

0 comments on commit 0b6084d

Please sign in to comment.