这是一个ping多个主机并跟踪其
的程序这是违规函数
function parseHistory(){
for(i = 0; i < hosts.length; i++){
var countTotal = 0;
var countSuccessful = 0;
var timeTotal = 0;
for(j = 0; j < history[hosts[i]].length; j++){
if(history[hosts[i]][j].success){
countTotal++;
countSuccessful++;
timeTotal += history[hosts[i]][j].time;
} else {
countTotal++;
}
}
var uptimePercent = Math.round(countSuccessful / countTotal * 10000) / 100 + '%';
var averageTime = Math.round(timeTotal / countSuccessful * 100) / 100;
console.log('--- ' + hosts[i] + ' has ' + uptimePercent + ' uptime with an average round-trip time of ' + averageTime + 'ms');
}
console.log('---');
}
其中hosts
是一个字符串数组,history
是一个存储多个结果对象数组的对象。
我的主要问题是这个for
循环,它似乎是错误的数学,但我不知道在哪里。
这是我的程序在大约30秒后产生的输出
--- google.com has 100% uptime with an average round-trip time of 33ms
--- yahoo.com has 100% uptime with an average round-trip time of 112ms
--- <redacted> has 0% uptime with an average round-trip time of NaNms
--- 127.0.0.1 has 100% uptime with an average round-trip time of 2ms
---
--- google.com has 100% uptime with an average round-trip time of 11.67ms
--- yahoo.com has 100% uptime with an average round-trip time of 38ms
--- <redacted> has 66.67% uptime with an average round-trip time of 1ms
--- 127.0.0.1 has 100% uptime with an average round-trip time of 1.33ms
---
--- google.com has 100% uptime with an average round-trip time of 7.6ms
--- yahoo.com has 100% uptime with an average round-trip time of 23.4ms
--- <redacted> has 80% uptime with an average round-trip time of 0.75ms
--- 127.0.0.1 has 100% uptime with an average round-trip time of 1.2ms
每次ping后平均时间似乎都在减少,但无缘无故。对我来说这看起来像是一个数学错误,但我无法找到它。
你能告诉我我的错误在哪里吗?
答案 0 :(得分:0)
从你的pastebin,我注意到你不断加长history[host]
列表:
setInterval(ping, pingIntervalMs, host, function(result){
history[host].push(result);
historyChanged = true;
});
&#34;结果&#34;结构看起来像?也许你正在推送默认为&#34; .success&#34;但是没有&#34; .time&#34;。