// Taktdauer messen (Echtzeit-Hybrid mit künstlicher Uhr, ohne Netzwerk): Spieltakt (game.tick) und Marktdaten für die // Clients (game.marketView + JSON, wird in index.js in jedem Takt einmal gebaut). Feste Zufallszahlen, damit Läufe vor und // nach einer Änderung vergleichbar sind. Ergebnisse: docs/BALANCING.md (Abschnitt Aktienuniversum). // // Aufruf (im Ordner server): node scripts/tickbench.mjs [--tage 2] [--seed 1] import fs from 'node:fs'; import os from 'node:os'; import path from 'node:path'; const arg = (k, d) => { const i = process.argv.indexOf(k); return i > 0 ? Number(process.argv[i + 1]) : d; }; const DAYS = arg('--tage', 2), SEED = arg('--seed', 1); const DAY = 86_400_000; function mulberry32(a) { return function () { a |= 0; a = (a + 0x6d2b79f5) | 0; let t = Math.imul(a ^ (a >>> 15), 1 | a); t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t; return ((t ^ (t >>> 14)) >>> 0) / 4294967296; }; } Math.random = mulberry32(SEED * 7919 + 17); process.env.TIME_MODE = 'hybrid'; process.env.NODE_ENV = 'test'; process.env.DATA_DIR = fs.mkdtempSync(path.join(os.tmpdir(), 'wibosim-bench-')); // Montag 06:00 UTC als Start (wie balance.mjs) const d0 = new Date(Date.UTC(2026, 0, 5, 6, 0, 0)); const clock = { t: d0.getTime() }; Date.now = () => clock.t; const { settings } = await import('../src/settings.js'); settings.news.enabled = false; const { Store } = await import('../src/store.js'); const { Game } = await import('../src/game.js'); const { simulatedNews } = await import('../src/news.js'); const store = new Store(process.env.DATA_DIR); store.backup = () => null; const g = new Game({ store, now: () => clock.t }); const cfg = settings.game; const nStocks = [...g.stocks.values()].filter((s) => s.kind === 'stock').length; const open = [], closed = [], view = [], bytes = []; const endAt = clock.t + DAYS * DAY; const simEvery = settings.news.simulatedEveryMin * 60_000; let lastSim = clock.t; const cpu0 = process.cpuUsage(); const wall0 = performance.now(); while (clock.t < endAt) { const ph = g.market.phase; const isOpen = ph !== 'closed'; clock.t += isOpen ? cfg.tickMs : 30_000; if (simEvery > 0 && clock.t - lastSim >= simEvery) { lastSim = clock.t; g.addNews(simulatedNews()); } const t0 = performance.now(); g.tick(); const t1 = performance.now(); // wie index.js (bis 0.24 mit trades[]: mit --mit-trades nachstellen) const str = JSON.stringify(process.argv.includes('--mit-trades') ? { type: 'market', market: g.marketView(), trades: g.ex.pendingTrades.splice(0, 200) } : { type: 'market', market: g.marketView() }); const t2 = performance.now(); g.ex.pendingTrades.length = 0; g.drainChanged(); (g.market.phase === 'continuous' ? open : closed).push(t1 - t0); if (g.market.phase === 'continuous') { view.push(t2 - t1); bytes.push(str.length); if (process.argv.includes('--detail') && view.length === 2000) { const m = JSON.parse(str); const parts = Object.fromEntries(Object.entries(m.market).map(([k, v]) => [k, JSON.stringify(v ?? null).length])); console.log('Anteile (Bytes):', { ...parts, trades: JSON.stringify(m.trades ?? []).length }); console.log('Beispiel:', JSON.stringify(m.market.tickers.find((t) => t.kind === 'stock'))); } } } const cpu = process.cpuUsage(cpu0); const wall = performance.now() - wall0; const q = (a, p) => { const s = [...a].sort((x, y) => x - y); return s[Math.min(s.length - 1, Math.floor((s.length - 1) * p))] ?? NaN; }; const mean = (a) => a.reduce((x, y) => x + y, 0) / Math.max(1, a.length); const f = (x) => x.toFixed(2); console.log(`Aktien: ${nStocks}, Instrumente: ${g.stocks.size}, Kalendertage: ${DAYS}, Seed ${SEED}`); console.log(`Takt im Handel: Ø ${f(mean(open))} ms · Median ${f(q(open, 0.5))} · p99 ${f(q(open, 0.99))} · max ${f(Math.max(...open))} (${open.length} Takte)`); console.log(`Takt außerhalb: Ø ${f(mean(closed))} ms · p99 ${f(q(closed, 0.99))} (${closed.length} Takte)`); console.log(`Marktdaten+JSON: Ø ${f(mean(view))} ms · p99 ${f(q(view, 0.99))} · Größe Ø ${Math.round(mean(bytes) / 1024)} KB`); console.log(`CPU gesamt: ${(cpu.user / 1e6 + cpu.system / 1e6).toFixed(1)} s bei ${(wall / 1000).toFixed(1)} s Laufzeit`); console.log(JSON.stringify({ stocks: nStocks, open: mean(open), openP99: q(open, 0.99), closed: mean(closed), view: mean(view), kb: mean(bytes) / 1024, cpu: (cpu.user + cpu.system) / 1e6 })); try { store.close(); } catch { /* egal */ } fs.rmSync(process.env.DATA_DIR, { recursive: true, force: true });