// geodesic_sim.js// KCC v2.0 测地线估计器// 参数完全由物理/数学约束导出p12%, γ1.1, C3// 定点数运算线性同余随机数无探测逻辑错误// 测试全频谱路径检测、动态队列拥塞安全、死锁恢复// 定点数常量 constSCALE1024;// 2^10用于固定点乘法/移位的基数constSHIFT10;// 右移位数// 估计器核心 classGeodesicEstimator{/** * param {number} T - 真实传播延迟 (微秒) * param {number} sigma - 噪声标准差 (微秒) */constructor(T,sigma){this.xT*SCALE;// x_est 的定点表示 (内部单位 微秒 × SCALE)this.mrT;// min_rtt (微秒, 原始精度)this.conf0;// 确认计数器 (累积)this.TT;// 当前路径的 T_prop (微秒)this.probe0;// 探测周期计数器this.sigmasigma;}// Box‑Muller 高斯噪声生成 (需要外部提供均匀随机函数 rng)staticgauss(rng,mean0,std1){constu1rng();constu2rng();returnmeanstd*Math.sqrt(-2*Math.log(u1))*Math.cos(2*Math.PI*u2);}/** * 单步迭代 * param {function} rng - 返回 [0,1) 均匀分布随机数的函数 * param {number} rtt - 当前传入的 RTT 观测值 (微秒) * param {number} ql - 队列延迟 (微秒)0 表示队列为空 */step(rng,rtt,ql0){this.probe;letactualRTTrtt;// 实际使用的观测值// ------ 探测逻辑 (每2000步尝试一次) ------if(this.probe2000){this.probe0;// 仅在队列完全空闲时才允许使用纯净观测模拟发送探测包if(ql0){// 用 T_prop 噪声 代替真实 RTT等效于主动探测actualRTTthis.TMath.round(GeodesicEstimator.gauss(rng,0,this.sigma));}// 若 ql ! 0放弃本次探测actualRTT 保持原始 rtt// 探测状态随即结束不会污染后续步骤}// ------ min_rtt 的原始精度更新 ------if(actualRTTthis.mr)this.mractualRTT;// ------ 测地线更新 ------constzactualRTT*SCALE;// 定点观测constvz-this.x;if(v0){// G1: 向下分支 (观测 ≤ 当前估计)this.xMath.min(this.x,z);}else{// G2: 向上分支 (观测 当前估计)constgrowthMath.floor((this.x*12)/100);// 12% 几何增长this.xMath.min(this.xgrowth,z);// 上限压制于观测值// G3: 路径增长检测器constthresholdMath.floor((this.mr*11*SCALE)/10);// 1.1 * min_rtt (定点)if(this.xthreshold){this.conf;// 累积确认}elseif(this.xthis.mr*SCALE){this.conf0;// 物理底线重置}if(this.conf3){// 确认路径增长更新 min_rttthis.mrMath.floor(this.x/SCALE);this.conf0;}}}/** * 返回用于 BDP 计算的 RTT 安全值 (微秒) * 始终 ≤ 真实传播延迟 */bdp(){constxUsMath.floor(this.x/SCALE);returnxUsthis.mr?xUs:this.mr;// 等价于 min(x_est, min_rtt)}}// 伪随机数生成器 (可复现) functionseedRandom(seed){returnfunction(){seed(seed*16645251013904223)0xffffffff;return(seed0)/0xffffffff;};}// 测试配置 constRTTs[100,200,300,500,750,1000,1400,2000,3000,5000,10000,20000,50000,100000,200000,300000,500000,750000,1000000];constSTEPS[5,10,25,50,100,200];// 路径增幅百分比constNS20;// 每个测试的随机种子数// 测试 1全频谱路径增长检测 functiontestFullSpectrum(){console.log();console.log(GEODESIC FULL-SPECTRUM VERIFICATION (${NS}seeds per case));console.log();letfailures0;for(constTofRTTs){constsigmaMath.max(1,Math.floor(T/100));for(constspofSTEPS){constTnewTMath.floor(T*sp/100);if(TnewT)continue;letmiss0;constdelays[];for(letseed0;seedNS;seed){constrngseedRandom(T*1000spseed);constestnewGeodesicEstimator(T,sigma);// 预热 2000 步 (路径未变化)for(leti0;i2000;i){constrttMath.max(1,TMath.round(GeodesicEstimator.gauss(rng,0,sigma)));est.step(rng,rtt,0);}// 路径变化后测试 500 步letdetectedfalse;for(lets1;s500;s){constrttMath.max(1,TnewMath.round(GeodesicEstimator.gauss(rng,0,sigma)));est.step(rng,rtt,0);if(est.bdp()TMath.floor(T*0.02)){// 超过原 T 的 2% 视为检测成功delays.push(s);detectedtrue;break;}}if(!detected)miss;}if(missNS/2){console.log(FAIL: T${T.toString().padStart(7)}us ${sp.toString().padStart(3)}%:${miss}/${NS});failures;}}if(failures0)console.log(T${T.toString().padStart(7)}us: ok);}returnfailures;}// 测试 2动态队列拥塞安全 functiontestCongested(){console.log(\n--- CONGESTED (dynamic queue, realistic TCP) ---);constscenarios[{T:1400,sigma:20,q_max:400,label:DC},{T:50000,sigma:200,q_max:5000,label:WAN},{T:300000,sigma:500,q_max:20000,label:LH}];conststeps50000;// 仿真步数constdrainInterval500;// 队列排空间隔 (模拟窗口减少)for(const{T,sigma,q_max,label}ofscenarios){letok0;for(letseed0;seed20;seed){constrngseedRandom(Tseed);constestnewGeodesicEstimator(T,sigma);letqueue0;for(leti0;isteps;i){constnoiseMath.round(GeodesicEstimator.gauss(rng,0,sigma));// 队列动态周期性排空其余时间缓慢变化if(i%drainInterval0){queue0;// 窗口减少队列瞬间排空}else{queueMath.round(GeodesicEstimator.gauss(rng,0,sigma*0.5));queueMath.max(0,Math.min(q_max,queue));}constrttMath.max(1,Tqueuenoise);est.step(rng,rtt,queue);// 传递真实队列值给探测逻辑}if(est.bdp()TMath.floor(T*0.02))ok;}console.log(${label}:${ok}/20 safe(ok16? -- WARNING:));}}// 测试 3死锁恢复 functiontestDeadlock(){console.log(\n--- DEADLOCK RECOVERY ---);for(const[T,sigma]of[[1400,20],[50000,200]]){letok0;for(letseed0;seed100;seed){constrngseedRandom(Tseed*9999);constestnewGeodesicEstimator(T,sigma);est.xMath.floor(T*5.5)*SCALE;// 模拟膨胀至 5.5 倍for(leti0;i5000;i){constrttMath.max(1,TMath.round(GeodesicEstimator.gauss(rng,0,sigma)));est.step(rng,rtt,0);}if(est.bdp()T*1.1)ok;}console.log(T${T}us:${ok}/100 recovered(ok80? -- FAIL:));}}// 主程序 conststartTimeDate.now();constfailedtestFullSpectrum();testCongested();testDeadlock();constelapsed((Date.now()-startTime)/1000).toFixed(1);console.log(\n);if(failed0){console.log(ALL TESTS PASSED (${elapsed}s));}else{console.log(${failed}FAILURE(S) DETECTED);}