Commit 0dae347f authored by Olav Kvittem's avatar Olav Kvittem

added ajajx progress callback giving 50ms burst resolution - used if better than average

parent 7016e0fc
......@@ -16,4 +16,5 @@
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
<script type="text/javascript" src="/js/jquery-1.10.1.min.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.10.3/jquery-1.9.1.js">
<!-- <script type="text/javascript" src="../js/jquery-ui-1.11.0-beta.2.custom/jquery.js"></script> -->
......@@ -65,7 +65,7 @@ at about <td><form name="speedtest" id="speedform">
<p>The measurements will show you ballpark figures and no analysis. If you want a
more indepth and accurate measurement go to the
<span id=advanced></span>
<script>$("#advanced").html('<a class="btn btn-primary btn-lg" href="http://' + document.location.hostname + '/iperf/">Advanced throughput test</a>');</script>
<script>$("#advanced").html('<a class="btn btn-primary btn-lg" href="http://' + document.location.hostname + '/iperf/">Advanced throughput test</a>');</script>. Accuracy is best for lower speeds and lower for gigabit speeds.
<h3>About</h3>
......
<!DOCTYPE html> <html lang="en">
<head>
<?php include '../head-bs.html'?>
<script type="text/javascript" src="/js/jquery-ui-1.10.3/jquery-1.9.1.js"></script>
<script type="text/javascript" src="/js/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
<!-- <script type="text/javascript" src="../js/jquery-ui-1.10.3/jquery-1.9.1.js"></script> -->
<script type="text/javascript" src="../js/jquery-ui-1.10.3/ui/jquery-ui.js"></script>
<!--
<script type="text/javascript" src="../js/jquery-ui-1.11.0-beta.2.custom/jquery.js"></script>
<script type="text/javascript" src="../js/jquery-ui-1.11.0-beta.2.custom/jquery-ui.js"></script>
-->
<style type="text/css" title="currentStyle">
@import "/js/jquery-ui-1.10.3/themes/base/jquery-ui.css";
/* @import "../js/jquery-ui-1.11.0-beta.2.custom/jquery-ui.css"; */
@import "/js/demo_page.css";
@import "../js/jquery-ui-1.10.3/themes/base/jquery-ui.css";
</style>
<script type="text/javascript" src=jsbandwidth.js> </script>
......
// measure bandwidth with http upload and download
// olav.kvittem@uninett.no 31/3-2014
var opera=navigator.userAgent.match(/^Opera/) != null;
var firefox=navigator.userAgent.match(/Firefox/) != null;
var min_test_length= 0.5; // minimum testing period seconds
var min_progress_interval = 30; // min interval to be used for estimates
var testno=0;
var start = 0;
var end = 0;
......@@ -15,15 +20,86 @@ var dlsum=0, dldiffsum=0, dlno=0 ;
var dlavg=0, ulavg=0;
var speeds=["100k", "1M", "10M", "100M"];
function get_progress(e, pregress) { // to be called onprogress in ajax
if (e.lengthComputable) {
var now=new Date().getTime();
// var now=e.timeStamp;
// if ( firefox) { // bug in firefox returns microseconds
// // now=now/1000;
// now=new Date().getTime(); // bug in fire
// console.log('firefox event time ' + e.timeStamp + ' clock ' + now + ' diff ' + (e.timeStamp/1000 - now) );
// }
var dt=now-pregress.start;
var loaded=e.loaded;
if ( opera && pregress.dir == "down" ) { loaded = loaded / 2;} // bug in Opera
if ( dt > min_progress_interval ){
var dload=loaded-pregress.loaded;
var mbps = dload*8/dt/1000;
pregress.pslope= pregress.slope;
pregress.slope= mbps/pregress.nmbps;
console.log( 'Progress log ' + mbps.toFixed(2) + 'mbps ' + dt + 'ms ' + (loaded / e.total * 100).toFixed(2) + '%' + ' slope ' + pregress.slope.toFixed(2) + ' - ' + loaded + ' - ' + e.total + ' now ' + now + ' then ' + pregress.start);
if ( pregress.nmbps > 0 ){ // try for the next to last one
pregress.mbps=pregress.nmbps;
} else { // first one and only ?
pregress.mbps=mbps;
}
pregress.nmbps= mbps;
pregress.n++;
pregress.loaded=loaded;
pregress.start=now;
pregress.interval=dt;
} else {
console.log( 'progress err ' + dt + 'ms ' + (loaded / e.total * 100).toFixed(2) + '%' + ' - ' + loaded + ' - ' + e.total + ' now ' + e.timeStamp + ' then ' + pregress.start);
}
}
}
function progress_speed( pregress, speed){
var nspeed=speed;
if (pregress.n > 1 ) {
var used=false;
if ( pregress.interval > min_progress_interval &&
pregress.slope > 0.85 && pregress.slope < 1.15 &&
pregress.nmbps > speed ){ // use last value
nspeed=pregress.nmbps; used=true;
} else if ( pregress.mbps > speed &&
pregress.pslope > 0.85 && pregress.pslope < 1.15 ){
nspeed=pregress.mbps; used=true;
}
}
if (used) {
console.log('Progress use ' + nspeed.toFixed(2) + ' average ' + speed.toFixed(2) + ' ' + pregress.n + ' reports');
}
return nspeed;
}
function TestDownload(speedclass) {
start = new Date().getTime();
var pregress={
dir: "down",
loaded: 0,
start: start,
mbps: 0,
nmbps: 0,
n: 0
}
$("#dlspeed").html('<img src=busy.gif>');
// speedclasse=speeds.indexOf(speedclass)+1;
console.log('### start download ' + speeds[speedclass]);
$.ajax({
type: "GET",
url: "bigfile-" + speeds[speedclass] + ".bin?id=" + start,
dataType: 'text',
xhrFields: {
onprogress: function(e) { get_progress(e, pregress); return 0;}
},
success: function(msg) {
binfile[speedclass] = msg;
......@@ -31,13 +107,13 @@ function TestDownload(speedclass) {
dldiff = (end - start) / 1000;
bytes = msg.length;
dlspeed = (bytes * 8 / dldiff) / 1000/ 1000;
dlspeed = Math.round(dlspeed*100)/100;
$("#dlspeed").html(dlspeed);
dlspeed = progress_speed( pregress, dlspeed );
$("#dlspeed").html( dlspeed );
},
complete: function(xhr, textStatus) {
if ( dldiff > 0.5 || (speedclass+1) >= (speeds.length) ){
console.log( 'class ' + speedclass + 'down time ' + dldiff);
if ( dldiff > min_test_length || (speedclass+1) >= (speeds.length) ){
console.log( 'class ' + speedclass + ' down time ' + dldiff);
TestUpload(0);
} else {
TestDownload(speedclass+1);
......@@ -53,24 +129,45 @@ function TestDownload(speedclass) {
function TestUpload(speedclass) {
start = new Date().getTime();
$("#ulspeed").html('<img src=busy.gif>');
var pregress={
dir: "up",
loaded: 0,
start: start,
mbps: 0,
nmbps: 0,
n: 0
}
$.ajax({
console.log('### start upload ' + speeds[speedclass]);
$.ajax({
type: "POST",
url: "post.aspx?id=" + start,
// url: "post.aspx?id=" + start,
url: "mini.html?id=" + start,
data: binfile[speedclass],
dataType: 'text',
async: true,
xhr: function(){ // to get upload progress events
var xhr = $.ajaxSettings.xhr() ;
xhr.upload.onprogress = function(e) {progress_last= get_progress(e, pregress); return 0; };
// xhr.upload.addEventListener('progress', function(e) {progress_last= get_progress(e, pregress); }, false);
return xhr ;
},
// this this works for get
// xhrFields: {
// onprogress: function(e) {progress_last= get_progress(e, pregress); return 0;}
// },
success: function(msg) {
end = new Date().getTime();
uldiff = (end - start) / 1000;
if ( uldiff < 0.5 && (speedclass+1) < (binfile.length)){
console.log( 'class ' + speedclass + 'up time ' + uldiff);
uldiff = ( end - start ) / 1000; // ms to sec
if ( uldiff < min_test_length && (speedclass+1) < (binfile.length)){
console.log( 'class ' + speedclass + ' up time ' + uldiff);
TestUpload(speedclass+1);
} else {
bytes = binfile[speedclass].length;
ulspeed = (bytes * 8 / uldiff) / 1000 / 1000;
ulspeed = Math.round(ulspeed*100)/100;
$("#ulspeed").html(ulspeed);
ulspeed=progress_speed( pregress, ulspeed );
dlsum += dlspeed; dldiffsum += dldiff; dlno++;
dlavg=Math.round(dlsum/dlno*10)/10;
......@@ -164,4 +261,4 @@ function test_performance(){
document.getElementById("test").disabled = false;
testno++;
// });
}
\ No newline at end of file
}
<html>
<body>
Dummy download
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment