Merge #106: routing reference test and save-file benchmark

This commit is contained in:
Frederic Jung 2026-10-26 08:34:19 +01:00
commit 2366bb9a8f
No known key found for this signature in database
5 changed files with 132 additions and 1 deletions

View file

@ -160,3 +160,19 @@ Issues and pull requests are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).
## License
[MIT](LICENSE) © 2026 Frederic Jung
## Benchmark an exported save
To measure simulation updates, actions, the closed mobile Networks sheet and
opening Connections with your own save:
```bash
node scripts/benchmark-performance.js /path/to/save.json
git show origin/main:site/index.html > /tmp/tubes-baseline.html
node scripts/benchmark-performance.js /path/to/save.json /tmp/tubes-baseline.html
```
The optional baseline compares timings and asserts that the complete serialized
simulation output matches. Saves may be plain state or exports wrapped in `S`.
These are headless JavaScript/DOM timings, not phone interaction or browser
painting measurements. For the seeded network benchmark used by CI, use `npm run perf`.

View file

@ -7,7 +7,7 @@
"homepage": "https://seriesoftub.es/",
"repository": { "type": "git", "url": "git+https://github.com/fjaeckel/seriesoftubes.git" },
"scripts": {
"test": "node --test --test-concurrency=1 tests/game.test.js tests/build.test.js tests/persist.test.js tests/services.test.js tests/peering.test.js tests/issues.test.js tests/regions.test.js tests/modes.test.js tests/levels.test.js tests/speed.test.js tests/i18n.test.js",
"test": "node --test --test-concurrency=1 tests/game.test.js tests/build.test.js tests/persist.test.js tests/services.test.js tests/peering.test.js tests/issues.test.js tests/regions.test.js tests/modes.test.js tests/levels.test.js tests/speed.test.js tests/i18n.test.js tests/routing-reference.test.js",
"build": "bash scripts/build.sh",
"i18n:check": "node scripts/i18n-check.js",
"serve": "npm run build && npx --yes serve@14.2.4 _site",

View file

@ -0,0 +1,43 @@
#!/usr/bin/env node
// Headless CPU/DOM benchmark, not a browser frame-rate measurement.
// node scripts/benchmark-performance.js SAVE.json [BASELINE.html]
// git show main:site/index.html > /tmp/tubes-baseline.html
const fs = require('node:fs');
const path = require('node:path');
const assert = require('node:assert/strict');
const { load } = require('../tests/harness');
const [saveFile, baseline] = process.argv.slice(2);
if (!saveFile) {
console.error('Usage: node scripts/benchmark-performance.js SAVE.json [BASELINE.html]');
process.exit(1);
}
const input = JSON.parse(fs.readFileSync(saveFile, 'utf8'));
const save = input.S || input;
function benchmark(file) {
const { ev, dom, errors } = load({ file });
try {
ev('ACT.start({})');
dom.window.benchmarkSave = JSON.parse(JSON.stringify(save));
ev('S=migrate(window.benchmarkSave);UI.speed=0;innerWidth=390;innerHeight=844;compute()');
const result = JSON.parse(ev('JSON.stringify(R)'));
const measure = (setup, run, repeats) => {
ev(setup);
const samples = JSON.parse(ev(`(()=>{const out=[];for(let i=0;i<${repeats};i++){const start=performance.now();${run};out.push(performance.now()-start);}return JSON.stringify(out);})()`));
const sorted = [...samples].sort((a, b) => a - b);
return { medianMs: +sorted[Math.floor(sorted.length / 2)].toFixed(1), samplesMs: samples.map(v => +v.toFixed(1)) };
};
const timings = {
compute: measure('', 'compute()', 5),
closedNetworks: measure("UI.left='nets';UI.leftOpen=false;UI.sel=null", 'renderLeft()', 3),
openConnections: measure("UI._dh=null;UI.dtab='conn'", 'ACT.connOpen()', 1),
action: measure("$('dash').hidden=true;UI.leftOpen=false;UI.left='nets'", 'after()', 3),
};
assert.deepEqual(errors, []);
return { result, timings };
} finally { dom.window.close(); }
}
const original = baseline ? benchmark(path.resolve(baseline)) : null;
const current = benchmark(path.join(__dirname, '..', 'site', 'index.html'));
if (original) assert.deepEqual(current.result, original.result, 'Simulation output must match the baseline');
console.log(JSON.stringify({ baseline: original?.timings, current: current.timings, ...(original && { identicalSimulation: true }) }, null, 2));

View file

@ -0,0 +1,18 @@
// Deliberately scan all exits for every flow, without byRank, local-city
// shortcuts or first-group selection. Stable sorting preserves E order on ties.
module.exports = `
for(const rank of RANKS){
for(f of flows){
if(f.rem<=1e-6||(rank===0&&!f.cache))continue;
Dv=SDi[f.ci];Pv=SPi[f.ci];PP=walked[f.ci]||(walked[f.ci]=[]);vis++;
const candidates=E.filter(e=>e.used<e.cap&&e.ok(f.n)&&
(TR?eff(e,f.n):base(e))===rank&&Dv[CIDX[e.city]]<Infinity)
.sort((a,b)=>(a.pref==='high'?0:1)-(b.pref==='high'?0:1)||Dv[CIDX[a.city]]-Dv[CIDX[b.city]]);
for(let i=0;i<candidates.length&&f.rem>1e-6;){
let j=i+1;while(j<candidates.length&&(candidates[j].pref==='high')===(candidates[i].pref==='high')&&
Math.abs(Dv[CIDX[candidates[j].city]]-Dv[CIDX[candidates[i].city]])<1e-6)j++;
sendGroup(candidates.slice(i,j));i=j;
}
}
}
`;

View file

@ -0,0 +1,54 @@
const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const referenceAllocation = require('./helpers/reference-allocation');
function network(ev) {
ev(`ACT.start({});S.cash=1e10;S.lvl=4;S.day=500;S.biz=[];S.links=[];S.pops={};S.pni=[];S.caches=[];S.transit=[];S.ixp={};
const cities=['fra','ams','lon','par','nyc','ash','sin','hkg','mos','pek'];
for(const c of cities){S.pops[c]={routers:6,rgen:3,gen:true,bb:{d:700},down:0};
for(const n of NETS.filter(n=>!n.tail).slice(0,35))S.pni.push({id:n.id+'@'+c,city:c,net:n.id,tier:0});
for(const n of NETS.filter(n=>n.cache).slice(0,3))S.caches.push({id:'cache-'+n.id+'@'+c,net:n.id,city:c});
}
for(let i=0;i<cities.length;i++){
S.links.push({id:'df'+i,a:cities[i],b:cities[(i+1)%cities.length],kind:'df',own:true,sys:'roadm',ch:Array(32).fill('tp16'),km:800,ready:0,cut:0,mo:100});
S.links.push({id:'w'+i,a:cities[i],b:cities[(i+2)%cities.length],kind:'wave',tier:2,km:1200,cut:0,mo:100});
S.transit.push({id:'transit'+i,net:'cog',city:cities[i],tier:1,down:0});
S.biz.push({id:'l2-'+i,name:'Circuit '+i,city:cities[i],city2:cities[(i+4)%cities.length],type:'l2vpn',bw:50,rate:100,reqs:['diverse'],route:i%3===0?'scenic':i%3===1?'fast':'normal'});
}
compute();`);
// Share the model and path sender, but replace indexed exit selection with
// the original full scan and stable sort for every flow.
const source = ev('compute.toString()');
const start = source.indexOf(' for(const rank of RANKS){');
const end = source.indexOf(' for(const i of NORD)', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
ev(`window.referenceCompute=eval('('+${JSON.stringify(source.slice(0,start)+referenceAllocation+source.slice(end))}+')')`);
}
test('indexed routing matches the simple allocation loop across network changes', () => {
const { ev, dom, errors } = load();
try {
network(ev);
const changes = [
'',
"S.pni.forEach((p,i)=>p.pref=i%3===0?'high':i%3===1?'low':'normal')",
"S.te={on:true,script:''}",
"S.te.script='prefer pni for goggle\\nspill at 80%\\navoid transit for netchill'",
"S.ev.rpki={nets:['ggl','nfx'],until:S.day+3};S.ev.depeer=S.day+3",
"S.ev.ddos={city:'fra',until:S.day+3,gbps:300,vec:'syn',entry:{k:'edge'}}",
"S.links[0].eqDown=S.day+2;S.links[1].cut=S.day+2;S.links[2].loss={until:S.day+2,f:0.5}",
"S.links[4].chFail={until:S.day+2,o:'tp16'};S.links[6].ch.push('zr4');S.links[8].regen=2",
"S.day+=4;S.pni.splice(0,20);S.caches.splice(0,10);S.transit[0].pref='high'",
];
for (const change of changes) {
ev(change);
if (change.startsWith("S.te.script=")) assert.ok(ev("teRules()"), "RouteScript scenario must be active");
// Both implementations receive exactly the same prior load (scenic routing).
ev('window.priorR=JSON.stringify(R);referenceCompute();window.expectedR=JSON.stringify(R);R=JSON.parse(priorR);compute()');
assert.deepEqual(JSON.parse(ev('JSON.stringify(R)')), JSON.parse(ev('expectedR')), change || 'baseline');
}
assert.deepEqual(errors, []);
} finally { dom.window.close(); }
});