// bench.mjs -- la mesure de la page bench.html, sous Node, sur le PC
// (`make bench`). Premier ordre de grandeur seulement : le chiffre qui compte
// est celui du téléphone.
//
// Copyright (C) 2026 les auteurs d'Ustagammon.
// Licence : GPLv3 ou ultérieure (voir LICENSE).
import { readFileSync } from "node:fs";
import os from "node:os";
import { fileURLToPath } from "node:url";
import { gzipSync } from "node:zlib";
import createEngineModule from "../build/wasm/engine.mjs";
import { DOWNLOADED_FILES, formatReport, measureTimes } from "../web/benchmark.mjs";
import { commands, loadEngine } from "../web/engine-wasm.mjs";
const root = new URL("../", import.meta.url);
const wasmDir = fileURLToPath(new URL("build/wasm/", root));
// Les fichiers du site : web/, et build/wasm/ servi sous wasm/.
function sitePath(name) {
const path = name.startsWith("wasm/") ? "build/" + name : "web/" + name;
return fileURLToPath(new URL(path, root));
}
const files = DOWNLOADED_FILES.map((name) => {
const content = readFileSync(sitePath(name));
return { name, bytes: content.length, gzipBytes: gzipSync(content).length };
});
const start = performance.now();
const engine = await loadEngine(createEngineModule, wasmDir);
const loadMs = performance.now() - start;
const { positions } = JSON.parse(readFileSync(sitePath("wasm/positions.json"), "utf8"));
const times = await measureTimes({
bestMove: (...args) => engine.run(commands.bestMove(...args)),
cubeDecision: (...args) => engine.run(commands.cubeDecision(...args)),
}, positions, (done, total) => process.stderr.write("\r " + done + "/" + total + " calculs"));
process.stderr.write("\n");
const environment = "Node " + process.version + ", " + os.cpus()[0].model + " (PC de développement)";
process.stdout.write(formatReport({
environment, files, loadMs, memoryBytes: engine.memoryBytes(), times,
}));