just so much groundwork

This commit is contained in:
2025-02-05 04:00:40 -07:00
parent b3772052f5
commit 952b5dd57f
22 changed files with 1003 additions and 2081 deletions

38
test/bench.ts Normal file
View 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)`,
);
// });
}

File diff suppressed because it is too large Load Diff

35
test/contextBench.test.js Normal file
View 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,
);
});