diff --git a/CHANGELOG.md b/CHANGELOG.md index 3bf0969..09a8382 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ All notable changes to this project are documented here. The project follows - Big networks no longer freeze for a moment at every new game day, above all with a NOC intern on duty. The intern now plans cache boxes, router ports and PNIs for every POP in one pass. On a network of 190 POPs the intern's shift held the page for about a tenth of a second on a computer and half a second on a phone; it now takes a few thousandths, and a whole day holds the page a quarter as long. The game plays exactly as before. - A low-latency boutique with a lot of its own fiber no longer stalls every day while it times its finance routes. On a big fiber network that check held the page for about a third of a second and now takes a few hundredths; checking customers who want a path without subsea or under a latency limit got quicker too. The game plays exactly as before. - The NOC intern no longer holds a big network for more than half a second each time it weighs taking out a spare wave. Before it decides, it checks every customer promised diverse paths, no subsea or a latency limit, and on a network of 390 POPs that check now takes a few hundredths of a second. Spotting new sovereign cloud and trading offers is about ten times quicker too, and the game plays exactly as before. +- Routing a big network's day takes about half the work, with exactly the same results: on a network of 390 POPs with Auto-TE, a day's routing went from about 0.7 to 0.3 seconds on a computer. ## [3.2.0] — 2026-11-07 diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 65247b3..4dfe7c0 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -186,10 +186,20 @@ them. Only indices are kept: exits, lists and paths are made fresh every call, b `scripts/route-fuzz.js` plays hundreds of random seeded games on main's page and this checkout's at once, comparing all of R and S after every move and day, with the kept tables and without (`tests/route-fuzz.test.js` plays a few). -Each loop of `compute()` runs in a function of its own (the stages are closures, the busiest is `rankPass`; -the money is `computeMoney()`). V8 optimises a small function quickly and keeps it; `compute()` as one 33 KB function -never stayed optimised: it ran in baseline code, boxing every number, and was compiled again and again in the background -only to be thrown away, which cost more CPU than the routing itself. Keep it that way: a new loop goes in a function. +Each loop of `compute()` runs in a function of its own (the money is `computeMoney()`). V8 optimises a small function +quickly and keeps it; `compute()` as one 33 KB function never stayed optimised: it ran in baseline code, boxing every +number, and was compiled again and again in the background only to be thrown away, which cost more CPU than the routing +itself. Keep it that way: a new loop goes in a function. + +The routing core, the loops that place every flow on an exit (`rankPass`, `directPass`, `sendGroup`, `send`, `push`, +`carry`, `altPath`), lives in `ROUTE` (`29-route-core.js`), made once at load: `compute()` hands it the day's state +(`ROUTE.nodes()`, `ROUTE.run()`). Made inside `compute()`, as they were, they were new functions every call, and V8 threw +away the code it had compiled for the old ones. The core reads exits by number from typed columns (`USED`, `CAPX`, `CIX`, +`HPX`, `VISX`), and each list as an object of one shape (`lx()`). For each list and source city, the route cache also +keeps the list in the order routing tries it (`xOrder()`: by preference, then distance, a stable sort): on a day the cache +is used, a flow's first group is where that order starts, past the exits that filled up, instead of a scan of the whole +list, and what is left needs no sort. Auto-TE marks the pairs that have no detour (`TEF`), the answer to most of its +asks. `tests/day-steps.test.js` and the routing equivalence tests hold all this to the same results. Served share per city (`sfOf`) drives eyeball revenue and customer mood. diff --git a/src/game/80-simulation/10-dijkstra.js b/src/game/80-simulation/10-dijkstra.js index 5b53685..71cd4c4 100644 --- a/src/game/80-simulation/10-dijkstra.js +++ b/src/game/80-simulation/10-dijkstra.js @@ -13,6 +13,21 @@ function dijkstra(adj,src,wf,skip,stop){const n=adj.length,D=new Float64Array(n) if(done[u]||d>D[u])continue;done[u]=1;if(u===stop)break; for(const [v,e] of adj[u]||[]){if(skip&&skip(v,e))continue;const nd=d+wf(e);if(nd2e9){DJ.n=n;DJ.D=new Float64Array(n);DJ.P=new Array(n);DJ.S=new Int32Array(n);DJ.done=new Int32Array(n);DJ.g=0;} + const g=++DJ.g,D=DJ.D,P=DJ.P,ST=DJ.S,DN=DJ.done;let hd=DJ.hd,hu=DJ.hu,cap=hd.length,m=0; + const put=(d,u)=>{if(m===cap){cap*=2;const a=new Float64Array(cap),b=new Int32Array(cap);a.set(hd);b.set(hu);hd=DJ.hd=a;hu=DJ.hu=b;} + let i=m++;hd[i]=d;hu[i]=u;while(i>0){const p=(i-1)>>1;if(hd[p]<=hd[i])break;const td=hd[p],tu=hu[p];hd[p]=hd[i];hu[p]=hu[i];hd[i]=td;hu[i]=tu;i=p;}}; + D[src]=0;ST[src]=g;put(0,src); + while(m){const d=hd[0],u=hu[0];m--; + if(m){hd[0]=hd[m];hu[0]=hu[m];let i=0;for(;;){const l=2*i+1,r=l+1;let k=i;if(l(ST[u]===g?D[u]:Infinity))continue;DN[u]=g;if(u===stop)break; + for(const [v,e] of adj[u]||[]){if(skip&&skip(v,e))continue;const nd=d+wf(e);if(nd<(ST[v]===g?D[v]:Infinity)){D[v]=nd;ST[v]=g;P[v]=e;put(nd,v);}}} + return ST[stop]===g?D[stop]:Infinity;} // The edges of a dijkstra() path back from city index B to A, and the cities on it (B first), as indices. function walkI(P,A,B){const path=[],ni=[B];let v=B;while(v!==A){const l=P[v];path.push(l);v=l.ai===v?l.bi:l.ai;ni.push(v);}return {path,ni};} // The same for compute()'s sender, with the marks it keeps on a path (full, its latency, its far-exit traffic) there from the start: one object shape. diff --git a/src/game/80-simulation/29-route-cache.js b/src/game/80-simulation/29-route-cache.js index 7b8e93b..15dc982 100644 --- a/src/game/80-simulation/29-route-cache.js +++ b/src/game/80-simulation/29-route-cache.js @@ -39,14 +39,26 @@ function exIdx(L){L.hi=L.some(e=>e.hp===0);L.mx=L.some(e=>e.dt>0)&&L.some(e=>e.d function rcReachTake(R2){return {reachable:{...R2.reachable},nt:{...R2.nt},cone:{...R2.cone},reach:R2.reach,reachNT:R2.reachNT};} function rcReachPut(X,R2){const r=X.reach;Object.assign(R2.reachable,r.reachable);Object.assign(R2.nt,r.nt);Object.assign(R2.cone,r.cone);R2.reach=r.reach;R2.reachNT=r.reachNT;} // Keep what was built (on a miss): okBy, the exit lists (each shared list once), each network's lists by rank, DSEL, RSET. -function rcExitKeep(key,E,okBy,reach,LIST,BRJ,DSEL,RSET){const ok=[];for(let j=0;j{for(let i=0;i=P.sig.length||P.sig[x]!==sig[x])return false;}return true;}; + const kept=(k,a)=>{if(!P)return null;const o=old.get(k);return o&&same(a)?o:null;}; const id=new Map(),lists=[];for(const V of LIST.values()){id.set(V,lists.length); - if(V.cs){const cs=[];for(let k=0;k{const o=[];for(const r in m)o.push(r,id.get(m[r]));return o;}); - RC.xk=key;RC.x={ok,reach,lists,brj,DSEL,RSET:[...RSET]};} + RC.xk=key;RC.x={ok,reach,lists,brj,DSEL,RSET:[...RSET],gk:RC.gk,bk,sig};} // Fresh lists for today's exits from a kept build. -function rcExitLists(X,E,BRJ,DSEL,RSET){const V=X.lists.map(d=>{if(d.cs){const cs={cs:[]};for(let k=0;k{if(d.cs){const cs={cs:[]};for(let k=0;ke.w; + // Each network's list at one rank, in an array by network index: BRX[j] are objects of as many shapes as networks have + // sets of ranks, and reading one per flow made the loop's code give up on them. + const byRank=rank=>{const B=new Array(BRX.length);for(let j=0;j{const o=NRES[i];NRES[i]=o-a;if(o>1e-3&&NRES[i]<=1e-3)EP++;if(!NSEEN[i]){NSEEN[i]=1;NORD.push(i);}NLOAD[i]=(NLOAD[i]||0)+a;}; + // t.g gives each egress a slot in t.v, so adding to it allocates nothing (a Map of numbers boxes every new sum). + const carry=(l,eg,nr,ci,a)=>{let t=l.tr;if(!t){t=l.tr={g:new Map(),v:[],n:[],d:[]};EL.push(l);}const s=t.g.get(eg);if(s===undefined){t.g.set(eg,t.v.length);t.v.push(0+a);}else t.v[s]+=a; + let k=l.i*NKN+nr;if(!SEN[k]){SEN[k]=1;t.n.push(nr);}ACN[k]=(ACN[k]||0)+a;k=l.i*NC+ci;if(!SED[k]){SED[k]=1;t.d.push(ci);}ACD[k]=(ACD[k]||0)+a;}; + // TEF[src*NC+dst]: 1 once no detour exists for that pair (capacity only shrinks, so there never will be one again). + // Most calls are those: a byte read, not a Map lookup. The kept path is still valid while every edge and node on it has room. + const altPath=(src,dst,base)=>{const key=src*NC+dst;if(TEF[key]===1)return null;const c0=teCache.get(key); + if(c0){let ok=true;const p=c0.path,ni=c0.ni;for(let k=0;k1e-3)){ok=false;break;} + if(ok)for(let k=0;k1e-3)){ok=false;break;}} + if(ok)return c0;} + const d=teSearch(src,dst); + if(d===Infinity||d>REACH*base+60){teCache.set(key,false);TEF[key]=1;return null;} + const Q=walkP(DJ.P,src,dst);teCache.set(key,Q);return Q;}; + /* Auto-TE's detour search (the one 10-dijkstra.js does to a stop), step for step: neighbours in the same order, the same heap, written into the + same DJ (walkP() reads its paths, the reuse above its marks). Only on the graph as flat arrays (TEO offsets by city, + TEN neighbours, TEE edges, made once per routing in run()) and with the test of an edge written out: no pairs to + unpack and no call for each edge. On a big network with Auto-TE these searches were most of the routing. */ + /* A search from one POP picks up where the last one from there stopped (LR*), as a search of its own would run: every + city closer than the last exit is settled and the heap holds the rest, so the next exit has its answer already or + the search goes on from there, the last exit's neighbours first (it stops before them). One that found nothing has + walked all src reaches and answers the next ones at once. Only on the same graph: EP counts the edges and routers + that run out of room (capacity only shrinks while routing, so none comes back), and another search in DJ moves its + mark. An exit out of the core is let into a graph of its own (OUT[dst]): that search neither resumes nor is resumed. + On a big network nearly all detours of a flow are found from one search (extreme: 3.6 M heap steps a routing → 0.17 M). */ + let TEO=null,TEN=null,TEE=null,LRs=-1,LRe=-1,LRg=-1,LRD=null,LRu=-1,LRm=0; + const teSearch=(src,dst)=>{const n=TEO.length-1,core=CORE,cut=!!(core&&OUT[dst]),go=LRs===src&&LRe===EP&&LRg===DJ.g&&LRD===DJ.D&&!cut; + let g,m=0,u=-1; + if(go){g=LRg;if(DJ.done[dst]===g)return DJ.D[dst];m=LRm;u=LRu;} + else{if(DJ.n!==n||DJ.g>2e9){DJ.n=n;DJ.D=new Float64Array(n);DJ.P=new Array(n);DJ.S=new Int32Array(n);DJ.done=new Int32Array(n);DJ.g=0;}g=++DJ.g;} + const D=DJ.D,P=DJ.P,ST=DJ.S,DN=DJ.done;let hd=DJ.hd,hu=DJ.hu,cap=hd.length; + if(!go){D[src]=0;ST[src]=g; + if(m===cap){cap*=2;const a=new Float64Array(cap),b=new Int32Array(cap);a.set(hd);b.set(hu);hd=DJ.hd=a;hu=DJ.hu=b;} + let i=m++;hd[i]=0;hu[i]=src;while(i>0){const p=(i-1)>>1;if(hd[p]<=hd[i])break;const td=hd[p],tu=hu[p];hd[p]=hd[i];hu[p]=hu[i];hd[i]=td;hu[i]=tu;i=p;}} + for(;;){ + if(u>=0){const d=D[u];for(let k=TEO[u],K=TEO[u+1];k0){const p=(i-1)>>1;if(hd[p]<=hd[i])break;const td=hd[p],tu=hu[p];hd[p]=hd[i];hu[p]=hu[i];hd[i]=td;hu[i]=tu;i=p;}}}} + if(!m){u=-1;break;} + const d=hd[0];u=hu[0];m--; + if(m){hd[0]=hd[m];hu[0]=hu[m];let i=0;for(;;){const l=2*i+1,r=l+1;let k=i;if(l(ST[u]===g?D[u]:Infinity)){u=-1;continue;}DN[u]=g;if(u===dst)break;} + // An exit out of the core that was found sits in the heap of its own graph; one never found was never let in. + if(cut&&u>=0)LRs=-1;else{LRs=src;LRe=EP;LRg=g;LRD=D;LRu=u;LRm=m;} + return ST[dst]===g?D[dst]:Infinity;}; + /* The routing loops work on exits by number (xi, their index in E): what they read for every exit they look at sits in + columns, USED, CAPX, CIX, HPX, DTX and VISX, and the lists they scan hold numbers (lst.x, lst.xat). Scanning objects of + thirty fields each read one cache line per exit; the columns keep a list's exits side by side. The objects are only + read for an exit that takes traffic (push()), and e.used is written back once routing is done (run()). */ + // An exit the flow can still try: not visited yet, not full when the flow started, reachable, and not on a saturated path. + const usable=x=>VISX[x]!==vis&&USED[x]HPX[a]-HPX[b]||Dv[CIX[a]]-Dv[CIX[b]]+(DTX[a]-DTX[b]); + // Push up to `want` along one path to exit e; returns what fitted. Index loops: for…of allocates an iterator until optimised. Capacity only shrinks while routing, so a + // path with one full link or node fits nothing for the rest of this pass: mark it, and later tries cost nothing + // (most tries are into paths that filled up earlier). + const push=(x,P,want,keep=0)=>{if(P.dead)return 0;const e=E[x];const path=P.path,ni=P.ni;let amt=want; + const PN=path.length;for(let k=0;k1e-3&&l.res<=1e-3)EP++;l.ld+=amt;carry(l,ek,nr,ci,amt);} + USED[x]+=amt;FREM[F]-=amt; + {let ok=amt;const cf=CNB[ci],ce=e.cn,cn=NCN[nr],bz=FBZ[F]===1; + if(cf!==ce||(cf&&!cn)||(!cf&&cn&&e.k==='t')){const prem=(e.k==='t'&&e.prem)||(e.k==='p'&&e.pnc)||path.some(x=>x.links&&x.links.some(y=>y.premium));const loss=prem?0.03:(ev.gfw>day?0.35:0.2);R2.gfwLost+=amt*loss;R2.gfwCross+=amt;ok=amt*(1-loss);} + SRV[ci]+=ok;R2.served+=ok;if(bz)R2.bizSrv[CITIES[ci].id]+=ok;if(e.rank<=2)R2.peered+=ok;if(e.k==='t'){const k=e.ts*NN+nr;if(!TNS[k]){TNS[k]=1;if(!TNK[e.ts]){TNK[e.ts]=[];TORD.push(e);}TNK[e.ts].push(nr);}TNA[k]+=ok;} + // Backhaul to transit: users notice the round trip to the exit. Up to 20 ms (about 1,500 km) is free, then + // it gets worse, and past 50 ms broadband and internet services count as bad service (#…). Under sender pays, + // Korean users notice any exit far away, transit or not, and much sooner. + if(path.length&&(e.k==='t'||snd&&CITIES[ci].kr)){let ms=P.ms;if(ms<0){ms=0;for(let k=0;k(kl?KR_LAT_OK:LAT_OK)){LATP[ci]+=ok*(kl?krLatLoss(rtt):latLoss(rtt));if(!P.fg){P.fg=0;FARL.push(P,ci,e.ci);}P.fg+=ok;}}const kx=e.kx;if(EGT[kx]===0&&!EGO.includes(kx))EGO.push(kx);EGT[kx]+=ok;if(!NEG[nr]){NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0};NEO.push(nr);}NEA[nr*4+kx]+=ok; + // Where each flow left (the PNI list asks what a PNI here would take over, #175). + if(e.J>0){const m=e.byC||(e.byC=new Map()),v=m.get(ci);if(v){v[0]+=ok;if(bz)v[1]+=ok;}else m.set(ci,[ok,bz?ok:0]);} + if(e.k==='t'){FGT[F]+=ok;FTI[F]=e.xi;}else if(e.k==='ix'){FGI[F]+=ok;FII[F]=e.xi;}else if(e.k==='p'){if(e.ci===ci&&e.net===NET_IDS[nr])FGH[F]+=ok;else FGP[F]+=ok;}} + return amt; + }; + // Shortest path first. With Auto-TE, whatever does not fit spills onto up to two alternate paths + // with free capacity, as long as they are no more than twice as long. + const send=(x,want)=>{ + let room=Math.min(want,FREM[F],CAPX[x]-USED[x]);if(room<=1e-6)return;const ci=CIX[x]; + const P=PP[ci]||(PP[ci]=walkP(Pv,fci,ci));if(!P.dead)room-=push(x,P,room,TE&&SPILL<1?1-SPILL:0); + if(TE&&room>1e-6&&FREM[F]>1e-6&&USED[x]1e-6;k++){const P=altPath(fci,ci,Dv[ci]);if(!P)break;const m=push(x,P,Math.min(room,CAPX[x]-USED[x]));if(m<=1e-6)break;room-=m;R2.teMoved+=m;} + }; + // Exits with the same preference and the same IGP distance form an ECMP group: spread the flow over + // them by free capacity (like several transits in one city), then top up in order if one fills. + // The group is g[i0..i1): plain loops and one scratch array, since this runs for most flows (same sums, same order). + // Without Auto-TE an exit whose path from here is full takes nothing: skip the call (and the numbers it boxes). + const gone=x=>{if(TE)return false;const P=PP[CIX[x]];return !!P&&P.dead===1;}; + // Every exit of a remembered group still has room (a loop, not every(): no closure per flow). NOG: no group. + const allRoom=c=>{for(let k=0;k{const ci=CIX[x],P=PP[ci];return P!==undefined&&P.dead===1&&(!TE||ci===fci||TEF[fci*NC+ci]===1);}; + const ROOM=[],sendGroup=(g,i0=0,i1=g.length)=>{let live=false;for(let k=i0;k1){let tot=0;for(let k=i0;k1e-6)for(let k=i0;k{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}}; + // The direct ranks (25-direct-egress.js) have short lists, one carrier's ports, so they take the plain way: the usable exits + // nearest first, tried in groups of equal distance, as the reference loop in the tests does. A direct rank only concerns the + // networks that have an exit there, and nothing from a POP where Prefer has the say; a port that is not set to carry only takes + // its own POP's traffic. Kept in a function of its own so the loop below stays as it was: the JIT is sensitive to its shape. + const directPass=(rank,pass)=>{const DS=DSEL.get(rank),LOC=rank===DIRECT_RANK[1][1]||rank===DIRECT_RANK[2][1],BR=byRank(rank); + for(F=0;F1e-6;){let j=i+1;while(j{const BR=byRank(rank); + for(F=0;F=CAPX[A[k]]||gone(A[k])))k++;cur[fci]=k; + if(k===N){gc[fci]=NOG;continue;} + const bh=HPX[A[k]],bd=Dv[CIX[A[k]]]+DTX[A[k]]; + for(let i=k;i=1e-6)break;if(USED[x]=CAPX[e]||gone(e)){full++;continue;}const d=Dv[CIX[e]]+DTX[e];if(d===Infinity)continue;const h=HPX[e]; + if(h=L.length&&full){let k=0;for(let li=0;li1){gc[fci]=NOG;continue;} + if(GN>1)sortD(G,GN);gc[fci]=G.slice(0,GN);}}} + sendGroup(G,0,GN); + if(FREM[F]<=1e-6)continue; + // What is left, nearest first: with a kept order, the usable exits in it (the order sorting them gives). + REST.length=0;if(lst.ord){const A=xOrder(lst,fci);for(let i=lst.cur[fci]|0;i1e-6;){ + let j=i+1;while(j{let a=lst.ord[ci];if(a===undefined){const b=[],L=lst.x; + for(let i=0;i{let w=W.get(A);if(w)return w; + if(A.cs)w={x:null,xat:null,hi:false,mx:false,gc:null,ord:null,cur:null,cs:A.cs.map(s=>s&&lx(s,W))}; + else{const xat=new Map();for(const [c,h] of A.at)xat.set(c,h.map(e=>e.xi));w={x:A.map(e=>e.xi),xat,hi:!!A.hi,mx:!!A.mx,gc:[],ord:A.ord||null,cur:[],cs:null};} + W.set(A,w);return w;}; + return {nodeTake,carry, + nodes(o){({NRES,NSEEN,NORD,NLOAD,EL,NKN,SEN,ACN,NC,SED,ACD}=o);}, + run(o,PASSES,RANKS,RSET){({E,HAS,PASS,R2,FREM,FNJ,FCI,FCA,FBZ,NF,CNB,NCN,ev,day,SRV,TNS,NN,TNK,TORD,TNA,snd,LATP,FARL,EGT,EGO,NEG,NEO,NEA,FGT,FTI,FGI,FII,FGH,FGP, + TE,SPILL,teCache,adj,CORE,OUT,REACH,DSEL,PREFC,BRJ,COMP,SDi,SPi,ALONE,TR,base,eff}=o); + NX=E.length;USED=new Float64Array(NX);CAPX=new Float64Array(NX);CIX=new Int32Array(NX);HPX=new Uint8Array(NX);DTX=new Float64Array(NX);VISX=new Int32Array(NX); + for(let x=0;xday?new Set(ev.rpki.nets):null;if(rp)for(const e of E){if(e.k==='c')continue;const o=e.ok;e.ok=n=>!rp.has(n)&&o(n);} // Who reaches whom and each network's exit lists, kept while the exits are the same (XC, 29-route-cache.js). - const XK=RCON?rcExitKey(GK,E,(depeer?1:0)+'|'+leak+'|'+(snd?1:0)+'|'+(rp?ev.rpki.nets.join(','):'')+'|'+(TRX?S.te.script:'')+'|'+(PX&&PX.bh.length?PX.bh.map(e=>e.bh.city+'>'+e.bh.land).join(','):'')):null,XC=XK!==null&&RC.xk===XK?RC.x:null; + // A backhaul through a dish changes a cut-off POP's distances: lists kept in order (29-route-cache.js) only for the same one. + const BHK=PX&&PX.bh.length?PX.bh.map(e=>e.bh.city+'>'+e.bh.land).join(','):''; + const XK=RCON?rcExitKey(GK,E,(depeer?1:0)+'|'+leak+'|'+(snd?1:0)+'|'+(rp?ev.rpki.nets.join(','):'')+'|'+(TRX?S.te.script:'')+'|'+BHK):null,XC=XK!==null&&RC.xk===XK?RC.x:null; // coneOf: does exit e reach network n only through a peer's customer cone (#85)? const coneOf=(e,n)=>e.k==='p'?e.net!==n:!!e.dir&&!e.dir.has(n); // ok() cannot change during one compute, so ask each exit once per network, not once per flow and rank. @@ -125,12 +127,10 @@ function compute(){RW.gen++; for(const k in edges)edges[k].res=edges[k].cap;for(const c of pops){const i=CIDX[c];NRES[i]=nodeCap(c);HAS[i]=1;R2.nodeCap[c]=NRES[i];} // The core (backbone technology): what passing-through traffic costs each router, and which POPs TE must avoid. const CORE=coreOn()&&S.core.run>=1;if(CORE)for(const c of pops){const i=CIDX[c];PASS[i]=corePass(c);OUT[i]=inCore(c)?0:1;} - const nodeTake=(i,a)=>{NRES[i]-=a;if(!NSEEN[i]){NSEEN[i]=1;NORD.push(i);}NLOAD[i]=(NLOAD[i]||0)+a;}; const TNA=new Float64Array(TSL.size*NN),TNS=new Uint8Array(TSL.size*NN),TNK=[],TORD=[]; const NKN=NN+3,ACN=new Float64Array(NE*NKN),SEN=new Uint8Array(NE*NKN),ACD=new Float64Array(NE*NC),SED=new Uint8Array(NE*NC),EL=[]; - // t.g gives each egress a slot in t.v, so adding to it allocates nothing (a Map of numbers boxes every new sum). - const carry=(l,eg,nr,ci,a)=>{let t=l.tr;if(!t){t=l.tr={g:new Map(),v:[],n:[],d:[]};EL.push(l);}const s=t.g.get(eg);if(s===undefined){t.g.set(eg,t.v.length);t.v.push(0+a);}else t.v[s]+=a; - let k=l.i*NKN+nr;if(!SEN[k]){SEN[k]=1;t.n.push(nr);}ACN[k]=(ACN[k]||0)+a;k=l.i*NC+ci;if(!SED[k]){SED[k]=1;t.d.push(ci);}ACD[k]=(ACD[k]||0)+a;}; + // The routing core (29-route-core.js) adds up what floods and circuits carry too. + ROUTE.nodes({NRES,NSEEN,NORD,NLOAD,EL,NKN,SEN,ACN,NC,SED,ACD});const nodeTake=ROUTE.nodeTake,carry=ROUTE.carry; R2.l2route={};R2.l2split={}; /* compute() reads only S, except here: scenic circuits remember yesterday's path in R.l2route and R.efg (deliberate hysteresis, so they do not flap). A second call can still move one; the third gives the same answer as the second. */ @@ -187,11 +187,6 @@ function compute(){RW.gen++; for(const i of ni)if(HAS[i])nodeTake(i,amt);for(const l of path){l.res-=amt;l.ld+=amt;if(!clean){l.jk=(l.jk||0)+amt;JK=1;}carry(l,ek,clean?NN+2:NN,Bi,amt);}return amt;}; const DD=ddosLoad({E,okBy,R2,autoBh:!!(TRX&&TRX.blackhole),haul,ms:msOf}); const TE=!!(S.te&&S.te.on)&&coreTE(),TR=TE?teRules():null,REACH=coreTeReach();R2.teMoved=0;const teCache=new Map(),SPILL=TR&&TR.spill?TR.spill:1; - const altPath=(src,dst,base)=>{const key=src*NC+dst,c0=teCache.get(key); - if(c0===false)return null;if(c0&&c0.path.every(l=>l.res>1e-3)&&c0.ni.every(i=>!HAS[i]||NRES[i]>1e-3))return c0; - const {D,P}=dijkstra(adj,src,wW,(v,e)=>e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(CORE&&OUT[v]&&v!==dst),dst); - if(D[dst]===Infinity||D[dst]>REACH*base+60){teCache.set(key,false);return null;} - const Q=walkP(P,src,dst);teCache.set(key,Q);return Q;}; /* A network in a peer's customer cone, reached through that peer (#85): after every network you peer with directly (its AS path is one hop longer) and before transit. */ const base=(e,n)=>e.pref==='low'?4:e.rank>0&&e.rank<3&&coneOf(e,n)?CONE_RANK:e.rank; @@ -215,123 +210,13 @@ function compute(){RW.gen++; if(dm&&r===3&&e.k==='t'){const t=dm.get(e.net);if(t){const d=DIRECT_RANK[t][e.carry?0:1];let ds=DSEL.get(d);if(!ds)DSEL.set(d,ds=new Uint8Array(NET_IDS.length));ds[j]=1;RSET.add(d);(m[d]||(m[d]=[])).push(e);}}} for(const r in m){const L=m[r],key=L.map(e=>e.xi).join(','),o=LIST.get(key);if(o){m[r]=o;continue;} let V=L;if(NCOMP>1){const cs=[];for(const e of L){const k=COMP[e.ci];if(k>=0)(cs[k]||(cs[k]=[])).push(e);}for(const s of cs)if(s)idx(s);V={cs};}else idx(L);LIST.set(key,V);m[r]=V;}} - if(RCH)rcExitKeep(XK,E,okBy,RCH,LIST,BRJ,DSEL,RSET);})(); - // The flow being routed, and from its POP: distances, arriving edges and the paths walked so far, by city index. - // F: its index; fci, fnj: its city and network index. - let F=0,fci=0,fnj=0,Dv=null,Pv=null,PP=null,vis=0;const walked=[],G=[],REST=[],NEG=[];let GN=0; + if(RCH)rcExitKeep(XK,E,okBy,RCH,LIST,BRJ,DSEL,RSET,BHK);})(); /* What each kind of exit served, in all (EGT) and per network (NEA, by network index and kind): numbers in typed arrays, where a sum by key would box every new value. EGO and NEO keep the order each first got traffic, which is the order - R.egT and R.netEg list them in; both are written back after routing. */ - const EGT=new Float64Array(4),EGO=[],NEA=new Float64Array(NN*4),NEO=[]; - // An exit the flow can still try: not visited yet, not full when the flow started, reachable, and not on a saturated path. - // byDist: (Da-Db)+(dta-dtb), so exits of one kind at one distance compare as exactly equal and keep their order. - const usable=e=>e.vis!==vis&&e.useda.hp-b.hp||Dv[a.ci]-Dv[b.ci]+(a.dt-b.dt); - // Push up to `want` along one path to exit e; returns what fitted. Index loops: for…of allocates an iterator until optimised. Capacity only shrinks while routing, so a - // path with one full link or node fits nothing for the rest of this pass: mark it, and later tries cost nothing - // (most tries are into paths that filled up earlier). - const push=(e,P,want,keep=0)=>{if(P.dead)return 0;const path=P.path,ni=P.ni;let amt=want; - const PN=path.length;for(let k=0;kx.links&&x.links.some(y=>y.premium));const loss=prem?0.03:(ev.gfw>day?0.35:0.2);R2.gfwLost+=amt*loss;R2.gfwCross+=amt;ok=amt*(1-loss);} - SRV[ci]+=ok;R2.served+=ok;if(bz)R2.bizSrv[CITIES[ci].id]+=ok;if(e.rank<=2)R2.peered+=ok;if(e.k==='t'){const k=e.ts*NN+nr;if(!TNS[k]){TNS[k]=1;if(!TNK[e.ts]){TNK[e.ts]=[];TORD.push(e);}TNK[e.ts].push(nr);}TNA[k]+=ok;} - // Backhaul to transit: users notice the round trip to the exit. Up to 20 ms (about 1,500 km) is free, then - // it gets worse, and past 50 ms broadband and internet services count as bad service (#…). Under sender pays, - // Korean users notice any exit far away, transit or not, and much sooner. - if(path.length&&(e.k==='t'||snd&&CITIES[ci].kr)){let ms=P.ms;if(ms<0){ms=0;for(let k=0;k(kl?KR_LAT_OK:LAT_OK)){LATP[ci]+=ok*(kl?krLatLoss(rtt):latLoss(rtt));if(!P.fg){P.fg=0;FARL.push(P,ci,e.ci);}P.fg+=ok;}}const kx=e.kx;if(EGT[kx]===0&&!EGO.includes(kx))EGO.push(kx);EGT[kx]+=ok;if(!NEG[nr]){NEG[nr]=R2.netEg[NET_IDS[nr]]={c:0,p:0,ix:0,t:0};NEO.push(nr);}NEA[nr*4+kx]+=ok; - // Where each flow left (the PNI list asks what a PNI here would take over, #175). - if(e.J>0){const m=e.byC||(e.byC=new Map()),v=m.get(ci);if(v){v[0]+=ok;if(bz)v[1]+=ok;}else m.set(ci,[ok,bz?ok:0]);} - if(e.k==='t'){FGT[F]+=ok;FTI[F]=e.xi;}else if(e.k==='ix'){FGI[F]+=ok;FII[F]=e.xi;}else if(e.k==='p'){if(e.ci===ci&&e.net===NET_IDS[nr])FGH[F]+=ok;else FGP[F]+=ok;}} - return amt; - }; - // Shortest path first. With Auto-TE, whatever does not fit spills onto up to two alternate paths - // with free capacity, as long as they are no more than twice as long. - const send=(e,want)=>{ - let room=Math.min(want,FREM[F],e.cap-e.used);if(room<=1e-6)return; - const P=PP[e.ci]||(PP[e.ci]=walkP(Pv,fci,e.ci));if(!P.dead)room-=push(e,P,room,TE&&SPILL<1?1-SPILL:0); - if(TE&&room>1e-6&&FREM[F]>1e-6&&e.used1e-6;k++){const P=altPath(fci,e.ci,Dv[e.ci]);if(!P)break;const m=push(e,P,Math.min(room,e.cap-e.used));if(m<=1e-6)break;room-=m;R2.teMoved+=m;} - }; - // Exits with the same preference and the same IGP distance form an ECMP group: spread the flow over - // them by free capacity (like several transits in one city), then top up in order if one fills. - // The group is g[i0..i1): plain loops and one scratch array, since this runs for most flows (same sums, same order). - // Without Auto-TE an exit whose path from here is full takes nothing: skip the call (and the numbers it boxes). - const gone=e=>{if(TE)return false;const P=PP[e.ci];return !!P&&P.dead===1;}; - // Every exit of a remembered group still has room (a loop, not every(): no closure per flow). NOG: no group. - const allRoom=c=>{for(let k=0;k{for(let k=i0;k1){let tot=0;for(let k=i0;k1e-6)for(let k=i0;k{if(n>24){if(n===a.length)a.sort(byDist);else{const t=a.slice(0,n).sort(byDist);for(let i=0;i=0&&byDist(a[j],x)>0){a[j+1]=a[j];j--;}a[j+1]=x;}}; - // The direct ranks (25-direct-egress.js) have short lists, one carrier's ports, so they take the plain way: the usable exits - // nearest first, tried in groups of equal distance, as the reference loop in the tests does. A direct rank only concerns the - // networks that have an exit there, and nothing from a POP where Prefer has the say; a port that is not set to carry only takes - // its own POP's traffic. Kept in a function of its own so the loop below stays as it was: the JIT is sensitive to its shape. - const directPass=(rank,pass)=>{const DS=DSEL.get(rank),LOC=rank===DIRECT_RANK[1][1]||rank===DIRECT_RANK[2][1]; - for(F=0;F1e-6;){let j=i+1;while(j{ - for(F=0;F=e.cap||gone(e)){full++;continue;}const d=Dv[e.ci]+e.dt;if(d===Infinity)continue;const h=e.hp; - if(h=lst.length&&full){let k=0;for(let li=0;li1){gc[fci]=NOG;continue;} - if(GN>1)sortD(G,GN);gc[fci]=G.slice(0,GN);}} - sendGroup(G,0,GN); - if(FREM[F]<=1e-6)continue; - REST.length=0;for(let li=0;li1e-6;){ - let j=i+1;while(j{ diff --git a/tests/helpers/reference-allocation.js b/tests/helpers/reference-allocation.js index ca0541d..d771194 100644 --- a/tests/helpers/reference-allocation.js +++ b/tests/helpers/reference-allocation.js @@ -1,7 +1,8 @@ // Deliberately scan all exits for every flow, without byRank, local-city // shortcuts or first-group selection. Stable sorting preserves E order on ties. // Without Auto-TE, an exit whose shortest path from this POP is already full fits nothing, so it counts as -// unusable, like a full exit (#210). +// unusable, like a full exit (#210). It runs inside the routing core (29-route-core.js), which reads what exits have +// used from its columns (USED, CAPX) and sends to exits by number (xi). module.exports = ` // A carrier's own port (dr) is also a plain transit exit. A POP with a Prefer-marked transit port keeps what Prefer // says, and a port not set to carry only takes its own POP's traffic. @@ -13,14 +14,14 @@ module.exports = ` for(F=0;Fe.usedUSED[e.xi](a.pref==='high'?0:1)-(b.pref==='high'?0:1)||Dv[CIDX[a.city]]-Dv[CIDX[b.city]]+(tie(a)-tie(b))); for(let i=0;i1e-6;){ let j=i+1;while(je.xi));i=j; } } } diff --git a/tests/route-cache.test.js b/tests/route-cache.test.js index 79d1093..4a0af1d 100644 --- a/tests/route-cache.test.js +++ b/tests/route-cache.test.js @@ -86,3 +86,45 @@ test('days in a row reuse the kept tables and route like a fresh start every day } assert.deepEqual(errors, []); }); + +test('with Auto-TE, kept orders and the no-detour marks route like a fresh start every day', () => { + // The routing core (29-route-core.js) keeps each list's order for each city with the tables, and Auto-TE marks the + // pairs with no detour: both have to give exactly what scanning and searching afresh give. The network is made busy + // (half its link capacity) so that flows spill, detours are searched and many fail. + const { ev, errors } = open(); + ev("S.core.run=2;S.te={on:true,day:S.day,script:'spill at 85%'};for(const b of S.biz)if(BIZ_T[b.type].l2&&b.city2)b.route='scenic';for(const l of S.links)if(l.kind==='wave')l.cap=(l.cap||0)/2;compute();compute();"); + assert.equal(ev('!!(S.te.on&&coreTE())'), true); + let tried = 0; + for (let d = 0; d < 4; d++) { + ev('dayTick()'); + const r = step(ev, ''); + assert.ok(r.same, `day ${d}: the same routing with and without the kept tables`); + assert.ok(!r.g && !r.x, `day ${d}: a day of demand only keeps the tables`); + tried = Math.max(tried, ev('R.teMoved')); + } + assert.ok(tried > 0, 'Auto-TE moved traffic onto detours'); + assert.deepEqual(errors, []); +}); + +// When the exits change but the graph does not (a network leaves an exchange because its PNI came up), the lists that +// did not change keep the orders routing worked out for them (the same arrays, used the same day), the ones that did +// start again, and routing is still exactly routing without the kept tables, day after day. +test('lists an exit change leaves alone keep their orders; routing stays the same as without the kept tables', () => { + const { ev, errors } = open(); + // Two days on the kept build: the lists get their orders. + ev('compute();compute();'); + ev('window.__ords=new Set(RC.x.lists.flatMap(d=>d.cs?d.ord.filter(Boolean):[d.ord]).filter(o=>Object.keys(o).length));'); + assert.ok(ev('__ords.size') > 5, `orders worked out: ${ev('__ords.size')}`); + // One network leaves one exchange: the exits key moves, the graph does not. + const r = step(ev, `(()=>{const ix=Object.keys(S.ixp).find(x=>ixSessions(x).size>3),n=[...ixSessions(ix)][0];window.__left=n+'@'+ix; + S.peers[n+'@'+ix]=false;delete S.peers[n+'@'+ix];S.rs[ix]=false;S.pni.push({id:'pk1',net:n,city:ixCity(ix),tier:1,n:1});})()`); + assert.equal(r.g, false, 'same graph'); + assert.equal(r.x, true, 'the exits changed'); + assert.equal(r.same, true, 'the same routing as without the kept tables'); + const kept = ev('RC.x.lists.flatMap(d=>d.cs?d.ord.filter(Boolean):[d.ord]).filter(o=>__ords.has(o)).length'); + const all = ev('RC.x.lists.length'); + assert.ok(kept > 0 && kept < ev('__ords.size') + 1, `lists that kept their orders: ${kept} of ${all}`); + // And a few more days, with the next change between them. + for (let d = 0; d < 3; d++) assert.equal(step(ev, 'for(const c in S.pops)if(S.pops[c].bb)S.pops[c].bb.d*=1.03').same, true, `day ${d + 1} after`); + assert.deepEqual(errors, []); +}); diff --git a/tests/routing-reference.test.js b/tests/routing-reference.test.js index 0524726..5f60938 100644 --- a/tests/routing-reference.test.js +++ b/tests/routing-reference.test.js @@ -22,15 +22,16 @@ function network(ev) { } 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 pass of PASSES)for(const rank of RANKS){'); - const end = source.indexOf(' for(const i of NORD)', start); + // 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= 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 ref = (source.slice(0,start)+referenceAllocation+source.slice(end)).replace('RCON=RC.on', 'RCON=false'); - ev(`window.referenceCompute=eval('('+${lit(ref)}+')')`); + 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', () => { diff --git a/tests/speed.test.js b/tests/speed.test.js index 500405f..f768206 100644 --- a/tests/speed.test.js +++ b/tests/speed.test.js @@ -289,28 +289,41 @@ test('the map is not drawn while the dashboard covers the whole screen', () => { // compare the two on a busy network (Auto-TE, then a RouteScript that keeps headroom on every link) and on a small one // where Auto-TE has to route around a full link, so it keeps holding when the game's numbers change. function plainCompute(ev) { - const src = ev('compute.toString()'); - let plain = src + const src = ev('compute.toString()'), csrc = ev('routeCore.toString()'); + const plain = src + .replace('const ns=e.nets||NET_IDS;', 'const ns=NET_IDS;') + // ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh, + // which also leaves the lists without kept orders, so the first group and the rest are scanned and sorted. + .replace('RCON=RC.on', 'RCON=false'); + // The routing core (29-route-core.js) with its shortcuts off: Auto-TE's no-detour marks and its searches picked up + // where the last one stopped back to the cache lookup and a fresh dijkstra() for every detour. + let core = csrc .replace('if(!lst.hi&&!lst.mx&&ALONE[fci])', 'if(false)') .replace('if(!GN){\n // One scan', 'if(false){\n // One scan') - .replace('const ns=e.nets||NET_IDS;', 'const ns=NET_IDS;') .replace('if(P.dead)return 0;', '').replaceAll('if(r<=1e-6){P.dead=1;return 0;}', '') - .replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=e=>\{[^\n]*\};/, 'const gone=e=>false;') - // ...and without what routing keeps between calls (29-route-cache.js): the plain path builds everything afresh. - .replace('RCON=RC.on', 'RCON=false'); + .replace('if(!P.dead)room-=push(', 'room-=push(').replace(/const gone=x=>\{[^\n]*\};/, 'const gone=x=>false;') + // ...and every exit is called, even one whose path is full with no detour (dud()). + .replace(/const dud=x=>\{[^\n]*\};/, 'const dud=x=>false;') + .replace('if(TEF[key]===1)return null;const c0=teCache.get(key);', 'const c0=teCache.get(key);if(c0===false)return null;') + .replace('const d=teSearch(src,dst);', 'const d=dijkstraTo(adj,src,wW,(v,e)=>e.res<=1e-3||(HAS[v]&&NRES[v]<=1e-3)||(CORE&&OUT[v]&&v!==dst),dst);') + .replace('const d=dijkstraTo(adj,src,wW,', 'const {D:D0,P:P0}=dijkstra(adj,src,wW,').replace(',dst);\n if(d===Infinity', ',dst),d=D0[dst];\n if(d===Infinity') + .replace('walkP(DJ.P,src,dst)', 'walkP(P0,src,dst)'); // ...and the group split and sorting written the plain way, with array copies, as before they were made allocation-free - const sg = plain.indexOf('const ROOM=[],sendGroup='), sd = plain.indexOf('const sortD='); + const sg = core.indexOf('const ROOM=[],sendGroup='), sd = core.indexOf('const sortD='); assert.ok(sg > 0 && sd > sg, 'found the group sender and the sort'); - plain = plain.slice(0, sg) + `const ROOM=[],sendGroup=(g0,i0=0,i1=g0.length)=>{const g=g0.slice(i0,i1);for(const e of g)e.vis=vis; - if(g.length>1){const room=g.map(e=>Math.max(0,e.cap-e.used)),tot=room.reduce((a,b)=>a+b,0),want=FREM[F];if(tot>1e-6)g.forEach((e,k)=>send(e,want*room[k]/tot));} - for(const e of g){if(FREM[F]<=1e-6)break;send(e,FREM[F]);}};\n ` + plain.slice(sd).replace(/^const sortD=\(a,n=a\.length\)=>\{[^\n]*\};/, 'const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);for(let i=0;i{const g=g0.slice(i0,i1);for(const x of g)VISX[x]=vis; + if(g.length>1){const room=g.map(x=>Math.max(0,CAPX[x]-USED[x])),tot=room.reduce((a,b)=>a+b,0),want=FREM[F];if(tot>1e-6)g.forEach((x,k)=>send(x,want*room[k]/tot));} + for(const x of g){if(FREM[F]<=1e-6)break;send(x,FREM[F]);}};\n ` + core.slice(sd).replace(/^const sortD=\(a,n=a\.length\)=>\{[^\n]*\};/, 'const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);for(let i=0;ifalse;'), 'every exit of a group is called'); assert.ok(plain.includes('RCON=false') && !plain.includes('RC.on'), 'the kept routing tables were switched off'); - assert.ok(plain.includes('const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);') && plain.includes('g.forEach('), 'the plain group split and sort are in'); - ev('window.__plain=' + plain.replace(/^function compute/, 'function')); + assert.ok(!core.includes('TEF[key]===1') && !core.includes('dijkstraTo(') && !core.includes('const d=teSearch(') && core.includes('if(c0===false)return null;'), "Auto-TE's shortcuts were switched off"); + assert.ok(core.includes('const sortD=(a,n=a.length)=>{const t=a.slice(0,n).sort(byDist);') && core.includes('g.forEach('), 'the plain group split and sort are in'); + ev('window.__plainC=' + plain.replace(/^function compute/, 'function') + ';window.__plainR=(' + core.replace(/^function routeCore/, 'function') + ')();'); + ev('window.__plain=function(){const r=ROUTE;ROUTE=__plainR;try{__plainC();}finally{ROUTE=r;}};'); } test('fast routing gives exactly the same result as the plain path it replaces', () => { @@ -365,6 +378,51 @@ test('Auto-TE routes around a full link exactly like the plain path', () => { assert.deepEqual(errors, []); }); +// Auto-TE's searches for a detour, on meshes of thin and thick links where POPs fill up and some sit outside the core +// (TE never steers through them, but may end there): a search from a city picks up where the last one from there +// stopped until a link or router runs out of room, and an exit whose full path has no detour is not called. Several +// seeded networks, from light to heavy load, must route exactly as the plain path that searches every time. +test('Auto-TE detours route like the plain path on crowded meshes with POPs outside the core', () => { + const { ev, errors } = load(); + plainCompute(ev); + // The fast core, counting the searches that were picked up: answered at once, or gone on from where the last stopped. + const csrc = ev('routeCore.toString()'), counted = csrc.replace('if(go){g=LRg;if(DJ.done[dst]===g)return DJ.D[dst];', 'if(go){g=LRg;if(DJ.done[dst]===g){__TE.had++;return DJ.D[dst];}__TE.went++;'); + assert.notEqual(counted, csrc, 'found the picked-up search'); + ev('window.__TE={had:0,went:0};ROUTE=(' + counted.replace(/^function routeCore/, 'function') + ')();'); + const CS = ['fra', 'ams', 'bru', 'par', 'lon', 'zrh', 'mil', 'vie', 'prg', 'ber', 'muc', 'mad', 'cph', 'waw', 'mrs', 'ham', 'dus']; + let moved = 0, outs = 0; + for (let seed = 1; seed <= 6; seed++) { + let a = seed * 7919; + const rnd = () => { a = (a * 16807) % 2147483647; return a / 2147483647; }, int = (n) => Math.floor(rnd() * n); + const cs = CS.filter(() => rnd() < 0.75), links = []; + for (let i = 1; i < cs.length; i++) links.push([cs[int(i)], cs[i], int(4)]); + for (let k = 0; k < cs.length; k++) { const x = cs[int(cs.length)], y = cs[int(cs.length)]; if (x !== y) links.push([x, y, int(4)]); } + const out = cs.filter(() => rnd() < 0.3), up = cs.filter((c) => out.includes(c) || rnd() < 0.3).concat(cs[0]); + ev(`ACT.start({});S.cash=1e12;S.lvl=13;for(const g of GOALS)S.done[g.id]=0;S.pops={};S.links=[];S.transit=[]; + const cs=${JSON.stringify(cs)},out=${JSON.stringify(out)},IN=RG.findIndex(g=>g.cr>=3&&!g.noMpls),OUT=RG.findIndex(g=>g.cr<2); + for(const c of cs){ACT.buildPop({city:c});const p=S.pops[c];p.rgen=RG_TOP;p.routers=RMAX;if(!p.bb)ACT.launchBB({city:c});} + const L=(a,b,t)=>{UI.via=null;UI.lmode='wave';UI.route='main';try{openLinkModal(a,b);ACT.buildLink({tier:t});}catch(e){}closeModal();}; + for(const [a,b,t] of ${JSON.stringify(links)})L(a,b,t); + for(const c of ${JSON.stringify(up)}){const t=NETS.find(n=>n.price&&n.pops.includes(c)&&(n.type==='t1'||n.type==='t2'));if(t)ACT.buyTransit({city:c,net:t.id,tier:3});} + // Then the routers: some too old for the core, and few enough that POPs fill up too. + for(const c of cs){const p=S.pops[c];p.rgen=out.includes(c)?OUT:IN;p.routers=1+${int(3)};} + S.ixp={};S.pni=[];S.caches=[];S.biz=[];S.te={on:true,day:0};S.core={lv:3,run:2};S.nextFail=1e9;S.nextEvent=1e9;`); + assert.ok(ev('coreOn()&&coreTE()'), 'the core rules apply and Auto-TE runs'); + outs += ev('Object.keys(S.pops).filter(c=>!inCore(c)).length'); + const sig = () => ev('JSON.stringify({served:R.served,teMoved:R.teMoved,egT:R.egT,load:S.links.map(l=>R.load[l.id]||0),node:R.nodeLoad,t:R.useT,f:R.fl.map(f=>[f.got,f.via])})'); + for (const load of [0.3, 0.8, 1.6, 3]) { + ev(`for(const c in S.pops)S.pops[c].bb.d=CITY[c].P*${load}`); + ev('compute()'); const fast = sig(); moved += ev('R.teMoved'); + ev('__plain()'); const ref = sig(); + assert.equal(fast, ref, `network ${seed} at load ${load}: the fast and the plain path route the same`); + } + } + assert.ok(moved > 100, 'Auto-TE found detours'); + assert.ok(outs > 0, 'some POPs are outside the core'); + assert.ok(ev('__TE.had') > 0 && ev('__TE.went') > 0, 'searches were picked up: some answered at once, some gone on'); + assert.deepEqual(errors, []); +}); + test('fiber customers are checked by link id the same way with or without the lookup tables', () => { const { ev, errors } = load(); ev("ACT.start({});S.cash=1e8;for(const g of GOALS)S.done[g.id]=0;ACT.launchBB({city:'fra'});ACT.buildPop({city:'ams'})");