I'm working which detect the download speed of a given system ,and the approach I took to find that is as follow
Download files of know size and then based upon the estimated time taken calculate the download speed
Here are code look
function startDownload() {
$('div.loading').show();
var startedAt,endedAt;
// Size in KB
var size = 16384;
startedAt = (new Date().getTime());
console.log('.....Started.....');
// window.earlier_entries = window.performance.getEntries().length;
var settings = {cached: false,datatype: 'html'}
var time = (new Date().getTime());
$.when(
$.ajax('/sample128k_2',settings),
$.ajax('/sample128k',settings),
$.ajax('/sample256k',settings),
$.ajax('/sample8192k',settings),
$.ajax('/sample512k',settings),
$.ajax('/sample1024k',settings),
$.ajax('/sample2048k',settings),
$.ajax('/sample4096k',settings),
$.ajax('/sample8192k',settings)
).done(function(a1,a2,a3,a4,a5,a6,a7){
endedAt = new Date().getTime();
var totalTime = (endedAt - startedAt);
console.log(totalTime);
// find_latency();
var totalsec = totalTime/ 1000;
// var totalsec = (totalTime - window.latency)/ 1000;
console.log(totalsec);
var size_mb = (size/1024/totalsec);
var data,identifier,speed;
if (totalsec > 0) {
if (size_mb >= 1) {
data = size_mb ;
identifier = ' MB/s';
}
else {
data = size/totalsec;
identifier = ' KB/s';
}
console.log(data);
speed = (data.toFixed(2) + identifier).fixed(2);
}
else {
speed = 'Too fast seem like around certainly >= 8MB/s';
}
$('span.speed').html(speed);
$('div.loading').hide();
$('div.text').show();
})
};
$(document).ready(function(){
$("a#start").bind('click',function(event){
$('div.text').hide();
event.preventDefault();
startDownload();
})
});
Now The code work as expected the file get downloaded but I fail to understand I see my bandwidth get listed as
But I fail to understand as to why I see huge difference(most of time) in result in
http://jsfiddle.net/yahavbr/tEFpC/2/
BTW mine is
http://speedboom.herokuapp.com/
Reason for difference I believe One reason I can think of that difference is latency since I downloading more then one file I quite naturally that download latency in my case is more
but
-- Even when I removed the latency (only work google chrome btw) I don't see a significant difference
function find_latency() {
var new_entries = window.performance.getEntries().length;
var latency = 0;
for(var i=window.earlier_entries;i< new_entries ; i++) {
latency += (window.performance.getEntries()[i].responseStart - window.performance.getEntries()[i].requestStart);
}
window.latency = latency;
console.log(latency);
};
But that too didn't make any difference to speed in term of result of speedof.me and other jsfiddle result
Now the question ?
-- Whom do I trust in term of exact download speed ?
-- How do better my approach ? If I can ?