seriesoftubes/tests/routing-reference.test.js
Antoine 1edf0abb54
Routing core made once, list orders kept, and Auto-TE's detour searches cut: same routing, much less work
The game plays exactly as before.

* The core of compute() (rankPass, directPass, sendGroup, send, push, carry, altPath) is ROUTE in 29-route-core.js,
  built once by routeCore(): made inside compute() they were new functions every call and V8 threw away their code.
  Exits are read by number from typed columns (USED, CAPX, CIX, HPX, VISX), each list is an object of one shape.
* The route cache keeps, for each list and source city, the order routing tries it (xOrder()); a list whose exits,
  cities and preferences are unchanged on the same graph keeps its orders when other exits change (rcExitKeep()).
* Auto-TE: pairs with no detour are marked (TEF), exits that cannot help are skipped (dud()), one failed search
  answers the next ones, dijkstraTo() searches in arrays kept between calls, teSearch() runs on the graph as flat
  arrays and picks up where the last search from the same POP stopped.
* The PNI/IX tie-break (#298) reaches the core.

Tests: route-cache, routing-reference, speed (the fast core against the plain path, Auto-TE on crowded meshes with
POPs outside the core, picked-up searches counted).
2026-11-07 23:51:35 +01:00

96 lines
6.4 KiB
JavaScript

const test = require('node:test');
const assert = require('node:assert/strict');
const { load } = require('./harness');
const referenceAllocation = require('./helpers/reference-allocation');
const { lit } = require('../scripts/lib/js-literal');
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});
// Other carriers at some of the same POPs: a carrier's own port beats the rest for traffic bound for it.
if(i%2===0)S.transit.push({id:'transitL'+i,net:'lvl',city:cities[i],tier:1,down:0});
if(i%3===0)S.transit.push({id:'transitT'+i,net:'tly',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: in the routing core (29-route-core.js), the passes of run().
const core = ev('routeCore.toString()'), source = ev('compute.toString()');
const start = core.indexOf(' for(const pass of PASSES)for(const rank of RANKS){');
const end = core.indexOf(' for(let x=0;x<NX;x++)E[x].used=USED[x];', start);
assert.ok(start >= 0 && end > start, 'allocation loop boundaries must exist');
// The reference also builds every table afresh, so the comparison covers what routing keeps between calls.
assert.ok(source.includes('RCON=RC.on'), 'compute() can be told not to keep its tables');
const refCore = (core.slice(0, start) + referenceAllocation + core.slice(end)).replace(/^function routeCore/, 'function');
ev(`window.referenceCore=(${refCore})();window.referenceRaw=(0,eval)('('+${lit(source.replace('RCON=RC.on', 'RCON=false').replace(/^function compute/, 'function'))}+')');
window.referenceCompute=function(){const r=ROUTE;ROUTE=referenceCore;try{referenceRaw();}finally{ROUTE=r;}};`);
}
test('indexed routing matches the simple allocation loop across network changes', () => {
const { ev, dom, errors } = load();
try {
network(ev);
const changes = [
'',
// Ports set to carry pull their carrier's traffic from every POP; the rest only take their own POP's.
"S.transit.filter(t=>t.net==='lvl').forEach((t,i)=>{if(i%2===0)t.carry=true})",
"S.transit.filter(t=>t.net==='tly').forEach(t=>{t.carry=true})",
"S.transit.filter(t=>t.net==='lvl').forEach((t,i)=>{t.pref=i%2?'low':'normal'})",
// Prefer keeps the say at its own POP: the POPs with a Prefer-marked port skip the carrier-direct rule, the others keep it.
"S.transit.forEach((t,i)=>{if(t.net!=='lvl')t.pref=i%3===0?'high':'normal'})",
"S.pni.forEach((p,i)=>p.pref=i%3===0?'high':i%3===1?'low':'normal')",
"S.te={on:true,script:''};S.core={lv:2,run:2,legacy:true}",
"S.te.script='prefer pni for goggle\\nspill at 80%\\navoid transit for netchill'",
"S.te.script='avoid transit level tree\\nprefer transit tell-ya\\nspill at 80%'",
"S.te.script='prefer transit Cogentle\\nprepend transit Cogentle 2\\nprotect customers'",
"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.ev.ddosMore=[{id:'x2',city:'nyc',until:S.day+3,gbps:200,vec:'http',entry:{k:'t',id:'transit4'}}]",
"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(); }
});
// Islands: POPs no backbone joins. Exit lists are split per island (a flow only scans exits it can reach),
// and must route exactly like the full scan, also when a cut splits an island or joins two.
test('indexed routing matches the simple allocation loop on a network of islands', () => {
const { ev, dom, errors } = load();
try {
network(ev);
ev("S.links=S.links.filter(l=>!['df4','df9','w3','w4','w8','w9'].includes(l.id));compute()");
assert.ok(ev("Object.keys(S.pops).some(c=>!pathExists('fra',c))"), 'the network is in pieces');
const changes = [
'',
"S.pni.forEach((p,i)=>p.pref=i%3===0?'high':i%3===1?'low':'normal')",
"S.te={on:true,script:'prefer pni for goggle\\nspill at 80%'};S.core={lv:2,run:2,legacy:true}",
"S.ev.ddos={city:'fra',until:S.day+3,gbps:300,vec:'syn',entry:{k:'edge'}}",
"S.links[0].cut=S.day+2",
"S.te=null;S.links[0].cut=0;S.links.push({id:'join',a:'par',b:'pek',kind:'wave',tier:2,km:8000,cut:0,mo:100})",
];
for (const change of changes) {
ev(change);
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(); }
});