From cad7a9972d5baa10c2a23eec3018d8df148f11ea Mon Sep 17 00:00:00 2001 From: Orion Reed Date: Sat, 7 Dec 2024 14:45:53 -0500 Subject: [PATCH] add benchmarking --- package.json | 4 +- src/__tests__/Vector.bench.ts | 72 +++++++++++++++++++++++++++++++++++ src/__tests__/bench.ts | 19 +++++++++ 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 src/__tests__/Vector.bench.ts create mode 100644 src/__tests__/bench.ts diff --git a/package.json b/package.json index e7a39ba..0d93763 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "dev": "vite", "build": "tsc --noEmit && vite build --base=/folk-canvas", "preview": "tsc --noEmit && vite build && vite preview", - "types": "tsc --noEmit" + "types": "tsc --noEmit", + "bench": "bun run src/__tests__/bench.ts" }, "dependencies": { "@babel/parser": "^7.26.2", @@ -20,6 +21,7 @@ "@types/node": "^22.10.1", "@webgpu/types": "^0.1.51", "bun-types": "^1.1.38", + "mitata": "^1.0.20", "typescript": "^5.7.2", "vite": "^6.0.0" } diff --git a/src/__tests__/Vector.bench.ts b/src/__tests__/Vector.bench.ts new file mode 100644 index 0000000..eebb6b9 --- /dev/null +++ b/src/__tests__/Vector.bench.ts @@ -0,0 +1,72 @@ +import { bench, run } from 'mitata'; +import { Vector } from '../common/Vector'; + +// Basic vector operations +bench('Vector.zero()', () => { + Vector.zero(); +}); + +bench('Vector.add', () => { + Vector.add({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +bench('Vector.sub', () => { + Vector.sub({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +bench('Vector.mult', () => { + Vector.mult({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +bench('Vector.scale', () => { + Vector.scale({ x: 1, y: 2 }, 2); +}); + +// Trigonometric operations +bench('Vector.rotate', () => { + Vector.rotate({ x: 1, y: 2 }, Math.PI / 4); +}); + +bench('Vector.rotateAround', () => { + Vector.rotateAround({ x: 1, y: 2 }, { x: 0, y: 0 }, Math.PI / 4); +}); + +bench('Vector.angle', () => { + Vector.angle({ x: 1, y: 2 }); +}); + +bench('Vector.angleTo', () => { + Vector.angleTo({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +bench('Vector.angleFromOrigin', () => { + Vector.angleFromOrigin({ x: 1, y: 2 }, { x: 0, y: 0 }); +}); + +// Distance and magnitude operations +bench('Vector.mag', () => { + Vector.mag({ x: 1, y: 2 }); +}); + +bench('Vector.magSquared', () => { + Vector.magSquared({ x: 1, y: 2 }); +}); + +bench('Vector.distance', () => { + Vector.distance({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +bench('Vector.distanceSquared', () => { + Vector.distanceSquared({ x: 1, y: 2 }, { x: 3, y: 4 }); +}); + +// Normalization and interpolation +bench('Vector.normalized', () => { + Vector.normalized({ x: 1, y: 2 }); +}); + +bench('Vector.lerp', () => { + Vector.lerp({ x: 1, y: 2 }, { x: 3, y: 4 }, 0.5); +}); + +await run(); diff --git a/src/__tests__/bench.ts b/src/__tests__/bench.ts new file mode 100644 index 0000000..9426f5f --- /dev/null +++ b/src/__tests__/bench.ts @@ -0,0 +1,19 @@ +import { readdirSync } from 'fs'; +import { join } from 'path'; +import { fileURLToPath } from 'url'; +import { dirname } from 'path'; + +const __filename = fileURLToPath(import.meta.url); +const __dirname = dirname(__filename); + +// Get all .bench.ts files in the __tests__ directory +const benchFiles = readdirSync(__dirname) + .filter((file) => file.endsWith('.bench.ts')) + .filter((file) => file !== 'bench.ts'); // Exclude this runner file + +// Run each benchmark file +for (const file of benchFiles) { + console.log(`\nRunning ${file}...`); + console.log('='.repeat(50)); + await import(`./${file}`); +}