Skip to content

Commit

Permalink
Merge pull request #104 from rotosonic/EricTest
Browse files Browse the repository at this point in the history
Update xmlhttprequest timeout and percentage complete
  • Loading branch information
rotosonic authored Aug 16, 2017
2 parents c00ca20 + 9d1ae69 commit 2b7c05f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 69 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "speed-testjs",
"version": "1.0.27",
"version": "1.0.28",
"description": "measure internet bandwidth",
"main": "index.js",
"author": "Maulan Byron",
Expand Down
16 changes: 8 additions & 8 deletions public/examples/download/downloadApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@
startTestButton.innerHTML = 'Testing in Progress ...';
//disable button
startTestButton.disabled = true;
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', true);
}
Expand Down Expand Up @@ -217,7 +217,7 @@
startTestButton.innerHTML = 'Start Test';
option.series[0].data[0].value = 0;
option.series[0].data[0].name = 'Test Complete';
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', false);
startTestButton.disabled = false;
Expand Down Expand Up @@ -248,14 +248,14 @@
option.series[0].data[0].value = 0;
//updat test status to complete
option.series[0].data[0].name = 'Test Failed';
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', false);
//update button text to communicate current state of test as In Progress
startTestButton.innerHTML = 'Start Test';
//enable start button
startTestButton.disabled = false;
//hide current test value in chart
//hide current test value in chart
option.series[0].detail.show = false;
//update gauge
myChart.setOption(option, true);
Expand All @@ -271,14 +271,14 @@
option.series[0].data[0].value = 0;
//updat test status to complete
option.series[0].data[0].name = 'Test Failed';
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', false);
//update button text to communicate current state of test as In Progress
startTestButton.innerHTML = 'Start Test';
//enable start button
startTestButton.disabled = false;
//hide current test value in chart
//hide current test value in chart
option.series[0].detail.show = false;
//update gauge
myChart.setOption(option, true);
Expand All @@ -294,14 +294,14 @@
option.series[0].data[0].value = 0;
//updat test status to complete
option.series[0].data[0].name = 'Test Failed';
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', false);
//update button text to communicate current state of test as In Progress
startTestButton.innerHTML = 'Start Test';
//enable start button
startTestButton.disabled = false;
//hide current test value in chart
//hide current test value in chart
option.series[0].detail.show = false;
//update gauge
myChart.setOption(option, true);
Expand Down
6 changes: 3 additions & 3 deletions public/examples/upload/uploadApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@
startTestButton.innerHTML = 'Testing in Progress ...';
//disable button
startTestButton.disabled = true;
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', true);
}
Expand Down Expand Up @@ -257,14 +257,14 @@
option.series[0].data[0].value = 0;
//updat test status to complete
option.series[0].data[0].name = 'Test Failed';
//set accessiblity aria-disabled state.
//set accessiblity aria-disabled state.
//This will also effect the visual look by corresponding css
startTestButton.setAttribute('aria-disabled', false);
//update button text to communicate current state of test as In Progress
startTestButton.innerHTML = 'Start Test';
//enable start button
startTestButton.disabled = false;
//hide current test value in chart
//hide current test value in chart
option.series[0].detail.show = false;
//update gauge
myChart.setOption(option, true);
Expand Down
52 changes: 31 additions & 21 deletions public/lib/downloadHttpConcurrentProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,17 @@
* @return error object
*/
downloadHttpConcurrentProgress.prototype.onTestError = function (result) {
if (this._running) {
this.clientCallbackError(result);
clearInterval(this.interval);
if (this._running) {
if ((Date.now() - this._beginTime) > this.testLength) {
this.endTest();
}
else{
this._running = false;
}
clearInterval(this.interval);
this.clientCallbackError(result);
this.abortAll();
}
}
};
/**
* onAbort method
Expand All @@ -98,13 +104,7 @@
downloadHttpConcurrentProgress.prototype.onTestTimeout = function () {
if(this._running) {
if ((Date.now() - this._beginTime) > this.testLength) {
clearInterval(this.interval);
if (this.downloadResults && this.downloadResults.length) {
this.clientCallbackComplete(this.downloadResults);
} else {
this.clientCallbackError('no measurements obtained');
}
this._running = false;
this.endTest();
}

}
Expand Down Expand Up @@ -133,6 +133,10 @@
if (!this._running) {
return;
}
//check for end of test
if ((Date.now() - this._beginTime) > this.testLength) {
this.endTest();
}
this.totalBytes = this.totalBytes + result.loaded;
this._storeResults(result);

Expand Down Expand Up @@ -224,18 +228,24 @@

}
//check for end of test
if ((Date.now() - this._beginTime) > (this.testLength)) {
this._running = false;
clearInterval(this.interval);
if (this.downloadResults && this.downloadResults.length) {
this.clientCallbackComplete(this.downloadResults);
} else {
this.clientCallbackError('no measurements obtained');
}
this.abortAll();
if ((Date.now() - this._beginTime) > this.testLength) {
this.endTest();
}

};
/**
* end test method
*/
downloadHttpConcurrentProgress.prototype.endTest = function(){
this._running = false;
clearInterval(this.interval);
if (this.downloadResults && this.downloadResults.length) {
this.clientCallbackComplete(this.downloadResults);
} else {
this.clientCallbackError('no measurements obtained');
}
this.abortAll();
};

/**
* reset test variables
Expand All @@ -255,4 +265,4 @@
};

window.downloadHttpConcurrentProgress = downloadHttpConcurrentProgress;
})();
})();
55 changes: 29 additions & 26 deletions public/lib/uploadHttpConcurrentProgress.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,15 @@
*/
uploadHttpConcurrentProgress.prototype.onTestError = function (result) {
if (this._running) {
this.clientCallbackError(result);
clearInterval(this.interval);
if ((Date.now() - this._beginTime) > this.testLength) {
this.endTest();
}
else{
this._running = false;
clearInterval(this.interval);
this.clientCallbackError(result);
this.abortAll();
}
}
};
/**
Expand All @@ -106,13 +112,7 @@
uploadHttpConcurrentProgress.prototype.onTestTimeout = function () {
if (this._running) {
if ((Date.now() - this._beginTime) > this.testLength) {
clearInterval(this.interval);
if (this.uploadResults && this.uploadResults.length) {
this.clientCallbackComplete(this.uploadResults);
} else {
this.clientCallbackError('no measurements obtained');
}
this._running = false;
this.endTest();
}

}
Expand Down Expand Up @@ -277,30 +277,33 @@
}
};


/**
* end of test
*/
uploadHttpConcurrentProgress.prototype.endTest = function () {
this._running = false;
this.abortAll();
clearInterval(this.interval);
if (this.uploadResults && this.uploadResults.length) {
var uploadResults = this.uploadResults;
var dataLength = uploadResults.length;
var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength);
data = data.sort(numericComparator);
var result = meanCalculator(data);
this.clientCallbackComplete(result);
} else {
this.clientCallbackError('no measurements obtained');
}
};
/**
* Monitor testSeries
*/
uploadHttpConcurrentProgress.prototype._monitor = function () {
this._calculateResults();
//check for end of test
if ((Date.now() - this._beginTime) > (this.testLength)) {
this.abortAll();
this._running = false;
clearInterval(this.interval);
if (this.uploadResults && this.uploadResults.length) {
var uploadResults = this.uploadResults;
var dataLength = uploadResults.length;
var data = slicing(uploadResults, Math.round(dataLength * 0.4), dataLength);
data = data.sort(numericComparator);
var result = meanCalculator(data);
this.clientCallbackComplete(result);
} else {
this.clientCallbackError('no measurements obtained');
}

if ((Date.now() - this._beginTime) > this.testLength) {
this.endTest();
}

};

/**
Expand Down
28 changes: 18 additions & 10 deletions public/lib/xmlhttprequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@
this._request.onload = this._handleLoad.bind(this);
this._request.onabort = this._handleAbort.bind(this);
this._request.timout = this._handleTimeout.bind(this);
this.requestTimeout= setTimeout(this._request.abort.bind(this._request), this.timeout);
this._request.onerror = this._handleError.bind(this);
this._request.onreadystatechange = this._handleOnReadyStateChange.bind(this);
if(this.method==='GET') {
Expand All @@ -84,7 +83,7 @@
this.id = id;
this.transferSize = size;
this._request.open(this.method, this.url, true);
this._request.timeout = this.timeout;
this.requestTimeout= setTimeout(this._internalAbort.bind(this), this.timeout);
if(this.method==='POST') {
this._request.send(payload);
}
Expand All @@ -93,6 +92,16 @@
}

};

/**
* internal timed abort
*/
xmlHttpRequest.prototype._internalAbort = function() {
if((this._request)&&(this._request.readyState!==4)){
this._request.abort();
}
};

/**
* Mark the start time of the request
*/
Expand All @@ -104,6 +113,7 @@
* Handle eror event
*/
xmlHttpRequest.prototype._handleError = function() {
clearTimeout(this.requestTimeout);
var err = {
statusText: this._request.statusText,
status: this._request.status
Expand All @@ -114,6 +124,7 @@
* Handle the timeout event on the wrapped request
*/
xmlHttpRequest.prototype._handleTimeout = function(response) {
clearTimeout(this.requestTimeout);
this.totalTime = this.endTime - this.startTime;
var transferSizeMbs = (response.loaded * 8) / 1000000;
var transferDurationSeconds = this.totalTime/1000;
Expand Down Expand Up @@ -149,7 +160,10 @@
* Close the request explicitly
*/
xmlHttpRequest.prototype.close = function () {
clearTimeout(this.requestTimeout);
if((this._request)&&(this._request.readyState!==4)){
this._request.abort();
}
};

/**
Expand All @@ -158,6 +172,7 @@
xmlHttpRequest.prototype._handleOnReadyStateChange = function () {

if(this._request.readyState === 4 && this._request.status === 200) {
clearTimeout(this.requestTimeout);
var result = {};
result.totalTime = Date.now() - this.startTime;
result.id = this.id;
Expand All @@ -176,20 +191,13 @@
}

}
if(this._request.status > 399){
var err = {
statusText: this._request.statusText,
status: this._request.status
};
this.callbackError(err);
return;
}
};

/**
* Handle the load event on the wrapped request
*/
xmlHttpRequest.prototype._handleLoad = function (response) {
clearTimeout(this.requestTimeout);
this.totalTime = Date.now() - this.startTime;
var result = {};
result.time = this.totalTime;
Expand Down

0 comments on commit 2b7c05f

Please sign in to comment.