vardlStatus="";// download speed in megabit/s with 2 decimal digits
varulStatus="";// upload speed in megabit/s with 2 decimal digits
varpingStatus="";// ping in milliseconds with 2 decimal digits
varjitterStatus="";// jitter in milliseconds with 2 decimal digits
varclientIp="";// client's IP address as reported by getIP
vardlProgress=0;//progress of download test 0-1
varulProgress=0;//progress of upload test 0-1
varpingProgress=0;//progress of ping+jitter test 0-1
vartestId=null;//test ID (sent back by telemetry if used, null otherwise)
varlog="";//telemetry log
functiontlog(s){
if(settings.telemetry_level>=2){
log+=Date.now()+": "+s+"\n";
}
}
functiontverb(s){
if(settings.telemetry_level>=3){
log+=Date.now()+": "+s+"\n";
}
}
functiontwarn(s){
if(settings.telemetry_level>=2){
log+=Date.now()+" WARN: "+s+"\n";
}
console.warn(s);
}
// test settings. can be overridden by sending specific values with the start command
varsettings={
mpot:false,//set to true when in MPOT mode
test_order:"IP_D_U",//order in which tests will be performed as a string. D=Download, U=Upload, P=Ping+Jitter, I=IP, _=1 second delay
time_ul_max:15,// max duration of upload test in seconds
time_dl_max:15,// max duration of download test in seconds
time_auto:true,// if set to true, tests will take less time on faster connections
time_ulGraceTime:3,//time to wait in seconds before actually measuring ul speed (wait for buffers to fill)
time_dlGraceTime:1.5,//time to wait in seconds before actually measuring dl speed (wait for TCP window to increase)
count_ping:10,// number of pings to perform in ping test
url_dl:"backend/garbage",// path to a large file or garbage.php, used for download test. must be relative to this js file
url_ul:"backend/empty",// path to an empty file, used for upload test. must be relative to this js file
url_ping:"backend/empty",// path to an empty file, used for ping test. must be relative to this js file
url_getIp:"backend/getIP",// path to getIP.php relative to this js file, or a similar thing that outputs the client's ip
getIp_ispInfo:true,//if set to true, the server will include ISP info with the IP address
getIp_ispInfo_distance:"km",//km or mi=estimate distance from server in km/mi; set to false to disable distance estimation. getIp_ispInfo must be enabled in order for this to work
xhr_dlMultistream:6,// number of download streams to use (can be different if enable_quirks is active)
xhr_ulMultistream:3,// number of upload streams to use (can be different if enable_quirks is active)
xhr_multistreamDelay:300,//how much concurrent requests should be delayed
xhr_ignoreErrors:1,// 0=fail on errors, 1=attempt to restart a stream if it fails, 2=ignore all errors
xhr_dlUseBlob:false,// if set to true, it reduces ram usage but uses the hard drive (useful with large garbagePhp_chunkSize and/or high xhr_dlMultistream)
xhr_ul_blob_megabytes:20,//size in megabytes of the upload blobs sent in the upload test (forced to 4 on chrome mobile)
garbagePhp_chunkSize:100,// size of chunks sent by garbage.php (can be different if enable_quirks is active)
enable_quirks:true,// enable quirks for specific browsers. currently it overrides settings to optimize for specific browsers, unless they are already being overridden with the start command
ping_allowPerformanceApi:true,// if enabled, the ping test will attempt to calculate the ping more precisely using the Performance API. Currently works perfectly in Chrome, badly in Edge, and not at all in Firefox. If Performance API is not supported or the result is obviously wrong, a fallback is provided.
overheadCompensationFactor:1.06,//can be changed to compensatie for transport overhead. (see doc.md for some other values)
useMebibits:false,//if set to true, speed will be reported in mebibits/s instead of megabits/s
xhr[i].open("GET",settings.url_dl+url_sep(settings.url_dl)+(settings.mpot?"cors=true&":"")+"r="+Math.random()+"&ckSize="+settings.garbagePhp_chunkSize,true);// random string to prevent caching
xhr[i].send();
}.bind(this),
1+delay
);
}.bind(this);
// open streams
for(vari=0;i<settings.xhr_dlMultistream;i++){
testStream(i,settings.xhr_multistreamDelay*i);
}
// every 200ms, update dlStatus
interval=setInterval(
function(){
tverb("DL: "+dlStatus+(graceTimeDone?"":" (in grace time)"));
// if the connection is so slow that we didn't get a single chunk yet, do not reset
startT=newDate().getTime();
bonusT=0;
totLoaded=0.0;
}
graceTimeDone=true;
}
}else{
varspeed=totLoaded/(t/1000.0);
if(settings.time_auto){
//decide how much to shorten the test. Every 200ms, the test is shortened by the bonusT calculated here
varbonus=(6.4*speed)/100000;
bonusT+=bonus>800?800:bonus;
}
//update status
dlStatus=((speed*8*settings.overheadCompensationFactor)/(settings.useMebibits?1048576:1000000)).toFixed(2);// speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 or 1000000 to go to megabits/mebibits
// IE11 workarond: xhr.upload does not work properly, therefore we send a bunch of small 256k requests and use the onload event as progress. This is not precise, especially on fast connections
xhr[i].onload=xhr[i].onerror=function(){
tverb("ul stream progress event (ie11wa)");
totLoaded+=reqsmall.size;
testStream(i,0);
};
xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+(settings.mpot?"cors=true&":"")+"r="+Math.random(),true);// random string to prevent caching
try{
xhr[i].setRequestHeader("Content-Encoding","identity");// disable compression (some browsers may refuse it, but data is incompressible anyway)
}catch(e){}
//No Content-Type header in MPOT branch because it triggers bugs in some browsers
xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+(settings.mpot?"cors=true&":"")+"r="+Math.random(),true);// random string to prevent caching
try{
xhr[i].setRequestHeader("Content-Encoding","identity");// disable compression (some browsers may refuse it, but data is incompressible anyway)
}catch(e){}
//No Content-Type header in MPOT branch because it triggers bugs in some browsers
// if the connection is so slow that we didn't get a single chunk yet, do not reset
startT=newDate().getTime();
bonusT=0;
totLoaded=0.0;
}
graceTimeDone=true;
}
}else{
varspeed=totLoaded/(t/1000.0);
if(settings.time_auto){
//decide how much to shorten the test. Every 200ms, the test is shortened by the bonusT calculated here
varbonus=(6.4*speed)/100000;
bonusT+=bonus>800?800:bonus;
}
//update status
ulStatus=((speed*8*settings.overheadCompensationFactor)/(settings.useMebibits?1048576:1000000)).toFixed(2);// speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 or 1000000 to go to megabits/mebibits
tlog("ulTest: "+ulStatus+", took "+(newDate().getTime()-startT)+"ms");
done();
}
}
}.bind(this),
200
);
}.bind(this);
if(settings.mpot){
tverb("Sending POST request before performing upload test");
xhr=[];
xhr[0]=newXMLHttpRequest();
xhr[0].onload=xhr[0].onerror=function(){
tverb("POST request sent, starting upload test");
testFunction();
}.bind(this);
xhr[0].open("POST",settings.url_ul);
xhr[0].send();
}elsetestFunction();
}
// ping+jitter test, function done is called when it's over
varptCalled=false;// used to prevent multiple accidental calls to pingTest
functionpingTest(done){
tverb("pingTest");
if(ptCalled)return;
elseptCalled=true;// pingTest already called?
varstartT=newDate().getTime();//when the test was started
varprevT=null;// last time a pong was received
varping=0.0;// current ping value
varjitter=0.0;// current jitter value
vari=0;// counter of pongs received
varprevInstspd=0;// last ping time, used for jitter calculation
xhr=[];
// ping function
vardoPing=function(){
tverb("ping");
pingProgress=i/settings.count_ping;
prevT=newDate().getTime();
xhr[0]=newXMLHttpRequest();
xhr[0].onload=function(){
// pong
tverb("pong");
if(i===0){
prevT=newDate().getTime();// first pong
}else{
varinstspd=newDate().getTime()-prevT;
if(settings.ping_allowPerformanceApi){
try{
//try to get accurate performance timing using performance api
varp=performance.getEntries();
p=p[p.length-1];
vard=p.responseStart-p.requestStart;
if(d<=0)d=p.duration;
if(d>0&&d<instspd)instspd=d;
}catch(e){
//if not possible, keep the estimate
tverb("Performance API not supported, using estimate");
}
}
//noticed that some browsers randomly have 0ms ping
if(instspd<1)instspd=prevInstspd;
if(instspd<1)instspd=1;
varinstjitter=Math.abs(instspd-prevInstspd);
if(i===1)ping=instspd;
/* first ping, can't tell jitter yet*/else{
if(instspd<ping)ping=instspd;// update ping, if the instant ping is lower
if(i===2)jitter=instjitter;
//discard the first jitter measurement because it might be much higher than it should be
elsejitter=instjitter>jitter?jitter*0.3+instjitter*0.7:jitter*0.8+instjitter*0.2;// update jitter, weighted average. spikes in ping values are given more weight.
tlog("ping: "+pingStatus+" jitter: "+jitterStatus+", took "+(newDate().getTime()-startT)+"ms");
done();
}
}
}.bind(this);
// send xhr
xhr[0].open("GET",settings.url_ping+url_sep(settings.url_ping)+(settings.mpot?"cors=true&":"")+"r="+Math.random(),true);// random string to prevent caching