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)`,
|
||||
);
|
||||
// });
|
||||
}
|
1455
test/bundle.js
1455
test/bundle.js
File diff suppressed because it is too large
Load Diff
35
test/contextBench.test.js
Normal file
35
test/contextBench.test.js
Normal file
@@ -0,0 +1,35 @@
|
||||
import {
|
||||
getContextItem,
|
||||
setDefaultContext,
|
||||
withContext,
|
||||
} from "@lib/context.ts"; // adjust path as needed
|
||||
import { testPerformance } from "./bench.ts";
|
||||
|
||||
Deno.test("Context Benchmark", () => {
|
||||
console.log("Context Benchmark - run within frame time");
|
||||
testPerformance(
|
||||
() => {
|
||||
setDefaultContext({ a: 1 });
|
||||
},
|
||||
10000,
|
||||
60,
|
||||
);
|
||||
|
||||
testPerformance(
|
||||
() => {
|
||||
withContext({ a: 1 }, () => {
|
||||
getContextItem("a");
|
||||
});
|
||||
},
|
||||
10000,
|
||||
60,
|
||||
);
|
||||
|
||||
testPerformance(
|
||||
() => {
|
||||
getContextItem("a");
|
||||
},
|
||||
100000,
|
||||
240,
|
||||
);
|
||||
});
|
Reference in New Issue
Block a user