just so much groundwork
This commit is contained in:
38
test/bench.ts
Normal file
38
test/bench.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { assert } from "jsr:@std/assert";
|
||||
import { describe, it } from "jsr:@std/testing/bdd";
|
||||
|
||||
/**
|
||||
* Tests if a function can run a given number of iterations within a target frame time.
|
||||
* @param fn The function to test.
|
||||
* @param iterations Number of times to run the function per frame.
|
||||
* @param fps Target frames per second.
|
||||
*/
|
||||
export function testPerformance(
|
||||
fn: () => unknown,
|
||||
iterations: number,
|
||||
fps: number,
|
||||
) {
|
||||
console.log(`Performance Test - ${iterations} iterations at ${fps} FPS`);
|
||||
const frameTime = 1000 / fps;
|
||||
const startTime = performance.now();
|
||||
|
||||
for (let i = 0; i < iterations; i++) {
|
||||
fn();
|
||||
}
|
||||
|
||||
const endTime = performance.now();
|
||||
const elapsed = endTime - startTime;
|
||||
|
||||
console.log(
|
||||
`Elapsed time: ${elapsed.toFixed(2)}ms (Target: ≤${
|
||||
frameTime.toFixed(2)
|
||||
}ms)`,
|
||||
);
|
||||
assert(
|
||||
elapsed <= frameTime,
|
||||
`Function took too long: ${elapsed.toFixed(2)}ms (Target: ≤${
|
||||
frameTime.toFixed(2)
|
||||
}ms)`,
|
||||
);
|
||||
// });
|
||||
}
|
Reference in New Issue
Block a user