54 lines
2.5 KiB
TypeScript
54 lines
2.5 KiB
TypeScript
// a game with a worldgen pool, and ticking it like a client loads chunks, for tests/chunk_loading_test.ts and
|
|
// tools/chunk_loading_bench.ts
|
|
import { type WorkerPort, WorldgenPool } from "$/server/game/worldgen_pool.ts";
|
|
import { start_worldgen_workers } from "$/server/worldgen_workers.ts";
|
|
import { test_game } from "./helpers.ts";
|
|
|
|
// a game whose chunks come from a pool of workers
|
|
// warm_up generates the spawn area and finds the spawn, which in a world without land searches a long way
|
|
export async function game_with_pool(workers: number, seed?: string, mods_dir = "mods", warm_up = true) {
|
|
if (workers === 0) return { ...(await test_game(mods_dir, undefined, seed)), pool: undefined, close: () => {} };
|
|
const t = await test_game(mods_dir, undefined, seed);
|
|
const started = start_worldgen_workers(workers, { permissions: "inherit", on_restart: () => {} });
|
|
const pool: WorldgenPool | undefined = new WorldgenPool(
|
|
started.ports as unknown as WorkerPort[],
|
|
t.game.worldgen_bundle!,
|
|
);
|
|
t.game.use_worldgen_pool(pool);
|
|
await pool.ready();
|
|
if (warm_up) await t.game.warm_up();
|
|
return { ...t, pool, close: () => (pool.close(), started.terminate()) };
|
|
}
|
|
|
|
// ticks, answering chunk batches like a client, until the player has every chunk in view. the chunks they got, by
|
|
// "x,z", and how long the longest tick took
|
|
// deno-lint-ignore no-explicit-any
|
|
export async function load_view(
|
|
t: Awaited<ReturnType<typeof test_game>>,
|
|
conn: number,
|
|
// how far apart ticks start, 50 like the game loop for timing, less for tests
|
|
tick_ms = 5,
|
|
timeout_ms = 120_000,
|
|
) {
|
|
const player = t.game.players().find((p) => p.conn === conn)!;
|
|
// deno-lint-ignore no-explicit-any
|
|
const chunks = new Map<string, any>();
|
|
let longest_tick = 0;
|
|
const start = performance.now();
|
|
while (player.chunks.pending_count > 0) {
|
|
if (performance.now() - start > timeout_ms) throw new Error(`${player.chunks.pending_count} chunks never came`);
|
|
const tick_start = performance.now();
|
|
t.game.tick();
|
|
longest_tick = Math.max(longest_tick, performance.now() - tick_start);
|
|
for (const message of t.take(conn)) {
|
|
if (message.type === "level_chunk") chunks.set(`${message.x},${message.z}`, message);
|
|
if (message.type === "chunk_batch_finished") {
|
|
t.send(conn, { type: "chunk_batch_received", chunks_per_tick: 64 });
|
|
}
|
|
}
|
|
// the rest of the tick, when the workers' chunks come in
|
|
await new Promise((resolve) => setTimeout(resolve, Math.max(1, tick_ms - (performance.now() - tick_start))));
|
|
}
|
|
return { chunks, longest_tick, ms: performance.now() - start };
|
|
}
|