fix(optimizer): убрать лишние поиски и расчёты маршрутов
This commit is contained in:
1 parent
057f0d352a
commit
4fee9e371f
8 files changed
+164
-9
No files matched your search
@@ -1,9 +1,11 @@
|
||||
// Bounded pure full-route calculation: no API, database, network or browser.
|
||||
import {spawnSync} from 'node:child_process'
|
||||
import assert from 'node:assert/strict'
|
||||
const [wanCount,jhCount,exitCount]=process.argv.slice(2).length?process.argv.slice(2).map(Number):[10,100,100]
|
||||
const args=process.argv.slice(2)
|
||||
if(args.length&&args.length!==3)throw new Error('Provide exactly three dimensions')
|
||||
const [wanCount,jhCount,exitCount]=args.length?args.map(Number):[10,100,100]
|
||||
const combinations=wanCount*jhCount*exitCount
|
||||
if([wanCount,jhCount,exitCount].some(n=>!Number.isInteger(n)||n<1||n>1000)||!Number.isFinite(combinations)||combinations>100000)throw new Error('Three dimensions 1..1000; product <=100000')
|
||||
if([wanCount,jhCount,exitCount].some(n=>!Number.isInteger(n)||n<0||n>1000)||jhCount<1||exitCount<1||!Number.isFinite(combinations)||combinations>100000||jhCount*exitCount>100000)throw new Error('WAN0..1000, JH/Exit1..1000; products <=100000')
|
||||
const code=`
|
||||
const fs=require('node:fs'),ts=require('typescript');
|
||||
const source=ts.transpileModule(fs.readFileSync('lib/route-optimizer-data.ts','utf8'),{compilerOptions:{module:ts.ModuleKind.CommonJS,target:ts.ScriptTarget.ES2022}}).outputText;
|
||||
@@ -12,12 +14,12 @@ const {buildLiveOptimizerData,DEFAULT_ROUTE_AI_OPTIMIZER_SETTINGS}=mod.exports;
|
||||
const common={host:'192.0.2.1',enabled:true,status:'online',latency:10,country:'RU',site:'TEST',model:'CHR'};
|
||||
const rows=[{...common,id:1,name:'Home',type:'home-router',wanUplinks:Array.from({length:${wanCount}},(_,i)=>({id:'w'+i,name:'WAN'+(i+1),iface:'ether'+(i+1),isp:'ISP',ip:'198.51.100.1',maxDl:100,maxUl:100}))},...Array.from({length:${jhCount}},(_,i)=>({...common,id:i+2,name:'Jump'+i,type:'jump-host'})),...Array.from({length:${exitCount}},(_,i)=>({...common,id:i+2+${jhCount},name:'Exit'+i,type:'exit-node'}))];
|
||||
const before=process.memoryUsage().heapUsed,start=performance.now();const entry=buildLiveOptimizerData(rows,[],DEFAULT_ROUTE_AI_OPTIMIZER_SETTINGS).homes[0];
|
||||
console.log(JSON.stringify({elapsedMs:performance.now()-start,heapDeltaBytes:process.memoryUsage().heapUsed-before,wanJhLegs:entry.wanJhLegs.length,routes:entry.fullRoutes.length,firstId:entry.fullRoutes[0]?.id,lastId:entry.fullRoutes.at(-1)?.id,bestMatchesFirst:entry.bestRoute===entry.fullRoutes[0],missingObservations:entry.fullRoutes.every(route=>route.hw.observed.probeId===null&&route.je.observed.probeId===null)}));
|
||||
console.log(JSON.stringify({elapsedMs:performance.now()-start,heapDeltaBytes:process.memoryUsage().heapUsed-before,wanJhLegs:entry.wanJhLegs.length,routes:entry.fullRoutes.length,firstId:entry.fullRoutes[0]?.id,lastId:entry.fullRoutes.at(-1)?.id,bestMatchesFirst:entry.bestRoute===(entry.fullRoutes[0]??null),missingObservations:entry.fullRoutes.every(route=>route.hw.observed.probeId===null&&route.je.observed.probeId===null)}));
|
||||
`;
|
||||
const start=performance.now(),r=spawnSync(process.execPath,['--max-old-space-size=256','-e',code],{cwd:process.cwd(),timeout:10000,encoding:'utf8',windowsHide:true,maxBuffer:16384});
|
||||
const result=r.status===0?JSON.parse(r.stdout):null;
|
||||
const profile={wanCount,jhCount,exitCount,combinations,memoryCapMb:256,timeoutMs:10000,totalMs:Math.round(performance.now()-start),status:r.status,timedOut:r.error?.code==='ETIMEDOUT',result,error:r.stderr.slice(0,2000)};
|
||||
console.log(JSON.stringify(profile,null,2));
|
||||
if(result){assert.equal(result.routes,combinations);assert.equal(result.wanJhLegs,wanCount*jhCount);assert.equal(result.firstId,`1-w0-2-${jhCount+2}`);assert.equal(result.lastId,`1-w${wanCount-1}-${jhCount+1}-${jhCount+exitCount+1}`);assert.equal(result.bestMatchesFirst,true);assert.equal(result.missingObservations,true)}
|
||||
if(result){assert.equal(result.routes,combinations);assert.equal(result.wanJhLegs,wanCount*jhCount);assert.equal(result.firstId,combinations?`1-w0-2-${jhCount+2}`:undefined);assert.equal(result.lastId,combinations?`1-w${wanCount-1}-${jhCount+1}-${jhCount+exitCount+1}`:undefined);assert.equal(result.bestMatchesFirst,true);assert.equal(result.missingObservations,true)}
|
||||
if(r.error&&!profile.timedOut)throw r.error;
|
||||
if(r.status!==0&&!profile.timedOut)process.exitCode=1;
|
||||
Reference in new issue
Block a user