add benchmarking

This commit is contained in:
Orion Reed 2024-12-07 14:45:53 -05:00
parent 9affffeab1
commit cad7a9972d
3 changed files with 94 additions and 1 deletions

View File

@ -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"
}

View File

@ -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();

19
src/__tests__/bench.ts Normal file
View File

@ -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}`);
}