🚀 benchmark
This commit is contained in:
75
packages/compute/__tests__/benchmark.ts
Normal file
75
packages/compute/__tests__/benchmark.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
// @ts-ignore
|
||||
import * as ParkMiller from "park-miller";
|
||||
import { generateRandomGrid } from "../generateGrid";
|
||||
import { Snake } from "../snake";
|
||||
import { Grid } from "../grid";
|
||||
import { computeBestRun } from "..";
|
||||
import { performance } from "perf_hooks";
|
||||
|
||||
const snake0 = [
|
||||
{ x: 4, y: -1 },
|
||||
{ x: 3, y: -1 },
|
||||
{ x: 2, y: -1 },
|
||||
{ x: 1, y: -1 },
|
||||
{ x: 0, y: -1 },
|
||||
];
|
||||
|
||||
const gameOptions = {
|
||||
maxSnakeLength: 5,
|
||||
colors: [1, 2, 3, 4],
|
||||
};
|
||||
|
||||
const run = (grid: Grid, snake0: Snake, k: number) => {
|
||||
const stats: number[] = [];
|
||||
const s0 = performance.now();
|
||||
|
||||
const M = 60 * 1000;
|
||||
let n = 40;
|
||||
|
||||
while (performance.now() - s0 < M && n-- > 0) {
|
||||
const s = performance.now();
|
||||
computeBestRun(grid, snake0, gameOptions, k);
|
||||
|
||||
stats.push(performance.now() - s);
|
||||
}
|
||||
|
||||
return stats;
|
||||
};
|
||||
|
||||
const report = (arr: number[]) => {
|
||||
const average = (arr: number[]) =>
|
||||
arr.reduce((s, x) => s + x, 0) / arr.length;
|
||||
|
||||
const spread = (arr: number[]) => {
|
||||
const m = average(arr);
|
||||
const v = average(arr.map((x) => Math.pow(x - m, 2)));
|
||||
return Math.sqrt(v) / m;
|
||||
};
|
||||
|
||||
const format = (x: number): string => {
|
||||
const u = Math.floor(x / 1000);
|
||||
const d = Math.floor(x % 1000).toString();
|
||||
return u === 0 ? d : format(u) + " " + d.padEnd(3, "0");
|
||||
};
|
||||
|
||||
return `${format(average(arr)).padStart(12)} ms ±${(spread(arr) * 100)
|
||||
.toFixed(2)
|
||||
.padStart(5)}% (x${arr.length.toString().padStart(3)})`;
|
||||
};
|
||||
|
||||
[
|
||||
//
|
||||
[10, 10, 1000],
|
||||
[21, 7, 1000],
|
||||
[42, 7, 1000],
|
||||
[42, 7, 5000],
|
||||
[42, 7, 14000],
|
||||
[42, 7, 30000],
|
||||
].forEach(([w, h, k]) => {
|
||||
const random = new ParkMiller(1);
|
||||
const grid = generateRandomGrid(w, h, { ...gameOptions, emptyP: 3 }, (a, b) =>
|
||||
random.integerInRange(a, b)
|
||||
);
|
||||
const stats = run(grid, snake0, k);
|
||||
console.log(`${w}x${h} : ${k}\n ${report(stats)}\n`);
|
||||
});
|
||||
@@ -1,8 +1,7 @@
|
||||
import { generateEmptyGrid } from "../generateGrid";
|
||||
import { setColor, getColor, isInside } from "../grid";
|
||||
import { createEmptyGrid, setColor, getColor, isInside } from "../grid";
|
||||
|
||||
it("should set / get cell", () => {
|
||||
const grid = generateEmptyGrid(2, 3);
|
||||
const grid = createEmptyGrid(2, 3);
|
||||
|
||||
expect(getColor(grid, 0, 1)).toBe(null);
|
||||
|
||||
@@ -20,7 +19,7 @@ test.each([
|
||||
[2, 1, false],
|
||||
[0, 3, false],
|
||||
])("isInside", (x, y, output) => {
|
||||
const grid = generateEmptyGrid(2, 3);
|
||||
const grid = createEmptyGrid(2, 3);
|
||||
|
||||
expect(isInside(grid, x, y)).toBe(output);
|
||||
});
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { snakeSelfCollide } from "../snake";
|
||||
import { snakeSelfCollide, snakeWillSelfCollide } from "../snake";
|
||||
|
||||
test.each([
|
||||
[[{ x: 0, y: 0 }], false],
|
||||
@@ -22,3 +22,41 @@ test.each([
|
||||
])("should report snake collision", (snake, collide) => {
|
||||
expect(snakeSelfCollide(snake)).toBe(collide);
|
||||
});
|
||||
|
||||
test.each([
|
||||
[
|
||||
[
|
||||
{ x: 1, y: 7 },
|
||||
{ x: 0, y: 7 },
|
||||
{ x: 0, y: 8 },
|
||||
{ x: 0, y: 9 },
|
||||
{ x: 1, y: 9 },
|
||||
],
|
||||
{ x: 1, y: 7 },
|
||||
true,
|
||||
],
|
||||
[
|
||||
[
|
||||
{ x: 1, y: 7 },
|
||||
{ x: 0, y: 7 },
|
||||
{ x: 0, y: 8 },
|
||||
{ x: 0, y: 9 },
|
||||
{ x: 1, y: 9 },
|
||||
],
|
||||
{ x: 1, y: 8 },
|
||||
false,
|
||||
],
|
||||
[
|
||||
[
|
||||
{ x: 1, y: 7 },
|
||||
{ x: 0, y: 7 },
|
||||
{ x: 0, y: 8 },
|
||||
{ x: 0, y: 9 },
|
||||
{ x: 1, y: 9 },
|
||||
],
|
||||
{ x: 1, y: 8 },
|
||||
false,
|
||||
],
|
||||
])("should report snake collision next", (snake, { x, y }, collide) => {
|
||||
expect(snakeWillSelfCollide(snake, x, y)).toBe(collide);
|
||||
});
|
||||
|
||||
@@ -1,10 +1,9 @@
|
||||
import { step } from "../step";
|
||||
import { generateEmptyGrid } from "../generateGrid";
|
||||
import { around4 } from "../point";
|
||||
import { setColor, getColor } from "../grid";
|
||||
import { createEmptyGrid, setColor, getColor } from "../grid";
|
||||
|
||||
it("should move snake", () => {
|
||||
const grid = generateEmptyGrid(4, 3);
|
||||
const grid = createEmptyGrid(4, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
@@ -36,7 +35,7 @@ it("should move snake", () => {
|
||||
});
|
||||
|
||||
it("should move short snake", () => {
|
||||
const grid = generateEmptyGrid(8, 3);
|
||||
const grid = createEmptyGrid(8, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
@@ -75,7 +74,7 @@ it("should move short snake", () => {
|
||||
});
|
||||
|
||||
it("should pick up fruit", () => {
|
||||
const grid = generateEmptyGrid(4, 3);
|
||||
const grid = createEmptyGrid(4, 3);
|
||||
const snake = [{ x: 1, y: 1 }];
|
||||
const direction = around4[0];
|
||||
const stack: number[] = [];
|
||||
|
||||
Reference in New Issue
Block a user