Skip to content

Commit

Permalink
Test order can now be changed from the settings
Browse files Browse the repository at this point in the history
  • Loading branch information
adolfintel committed Oct 13, 2017
1 parent b1149b3 commit 60c707d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
14 changes: 9 additions & 5 deletions doc.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# HTML5 Speedtest

> by Federico Dossena
> Version 4.3.2 Hotfix 1, October 12 2017
> Version 4.4, October 14 2017
> [https://github.com/adolfintel/speedtest/](https://github.com/adolfintel/speedtest/)

Expand Down Expand Up @@ -143,22 +143,26 @@ w.postMessage('start {"param1": "value1", "param2": "value2", ...}')
* Recommended: `>=20`
* __url_dl__: path to garbage.php or a large file to use for the download test.
* Default: `garbage.php`
* The string "-1" disables the test
* __Important:__ path is relative to js file
* __url_ul__: path to an empty file or empty.php to use for the upload test
* Default: `empty.php`
* The string "-1" disables the test
* __Important:__ path is relative to js file
* __url_ping__: path to an empty file or empty.php to use for the ping test
* Default: `empty.php`
* The string "-1" disables the test
* __Important:__ path is relative to js file
* __url_getIp__: path to getIP.php or replacement
* Default: `getIP.php`
* The string "-1" disables the test
* __Important:__ path is relative to js file

#### Advanced test parameters
* __test_order__: the order in which tests will be performed. Each character represents an operation:
* `I`: get IP
* `D`: download test
* `U`: upload test
* `P`: ping + jitter test
* `_`: delay 1 second
* Default test order: `ID_U_P`
* __Important:__ Tests can only be run once
* __enable_quirks__: enables browser-specific optimizations. These optimizations override some of the default settings. They do not override settings that are explicitly set.
* Default: `true`
* __garbagePhp_chunkSize__: size of chunks sent by garbage.php in megabytes
Expand Down
9 changes: 3 additions & 6 deletions example7.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,11 @@ <h3>Jitter</h3>
var dl_checkbox = document.getElementById("st-download-checkbox")
var ul_checkbox = document.getElementById("st-upload-checkbox")
var ping_checkbox = document.getElementById("st-ping-checkbox")
var str_parameters = []

if (!ip_checkbox.checked) {str_parameters.push('"url_getIp": "-1"')}
if (!dl_checkbox.checked) {str_parameters.push('"url_dl": "-1"')}
if (!ul_checkbox.checked) {str_parameters.push('"url_ul": "-1"')}
if (!ping_checkbox.checked) {str_parameters.push('"url_ping": "-1"')}
var test_order=(ip_checkbox.checked?"I":"")+(dl_checkbox.checked?"D":"")+(ul_checkbox.checked?"U":"")+(ping_checkbox.checked?"P":"")

worker.postMessage('start {"test_order":"'+test_order+'" }')

worker.postMessage('start {' + str_parameters.join(",") + '}')
}
function stopTest() {
if (worker) worker.postMessage('abort')
Expand Down
29 changes: 22 additions & 7 deletions speedtest_worker.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
HTML5 Speedtest v4.3.2 Hotfix 1
HTML5 Speedtest v4.4
by Federico Dossena
https://github.com/adolfintel/speedtest/
GNU LGPLv3 License
Expand All @@ -19,6 +19,7 @@ function twarn(s){log+=Date.now()+' WARN: '+s+'\n'; console.warn(s)}

// test settings. can be overridden by sending specific values with the start command
var settings = {
test_order: "ID_U_P", //order in which tests will be performed as a string. D=Download, U=Upload, P=Ping+Jitter, I=IP, _=1 second delay
time_ul: 15, // duration of upload test in seconds
time_dl: 15, // duration of download test in seconds
time_ulGraceTime: 3, //time to wait in seconds before actually measuring ul speed (wait for buffers to fill)
Expand All @@ -41,6 +42,8 @@ var settings = {

var xhr = null // array of currently active xhr requests
var interval = null // timer used in tests
var delayTimer = null // another timer used in test
var test_pointer = 0 //pointer to the next test to run inside settings.test_order

/*
this function is used on URLs passed in the settings to determine whether we need a ? or an & as a separator
Expand Down Expand Up @@ -69,6 +72,7 @@ this.addEventListener('message', function (e) {
var ss = e.data.substring(5)
if (ss) s = JSON.parse(ss)
}catch(e){ twarn('Error parsing custom settings JSON. Please check your syntax') }
if (typeof s.test_order !== 'undefined') settings.test_order = s.test_order.toUpperCase() // test order
if (typeof s.url_dl !== 'undefined') settings.url_dl = s.url_dl // download url
if (typeof s.url_ul !== 'undefined') settings.url_ul = s.url_ul // upload url
if (typeof s.url_ping !== 'undefined') settings.url_ping = s.url_ping // ping url
Expand Down Expand Up @@ -110,11 +114,24 @@ this.addEventListener('message', function (e) {
} catch (e) { twarn('Possible error in custom test settings. Some settings may not be applied. Exception: '+e) }
// run the tests
tlog(JSON.stringify(settings))
getIp(function () { dlTest(function () { testStatus = 2; pingTest(function () { testStatus = 3; ulTest(function () { testStatus = 4; sendTelemetry() }) }) }) })
test_pointer=0;
var runNextTest=function(){
if(test_pointer>=settings.test_order.length){testStatus=4; sendTelemetry(); return;}
switch(settings.test_order.charAt(test_pointer)){
case 'I':{test_pointer++; getIp(runNextTest);} break;
case 'D':{test_pointer++; testStatus=1; dlTest(runNextTest);} break;
case 'U':{test_pointer++; testStatus=3; ulTest(runNextTest);} break;
case 'P':{test_pointer++; testStatus=2; pingTest(runNextTest);} break;
case '_':{test_pointer++; delayTimer=setTimeout(runNextTest,1000);} break;
default: test_pointer++;
}
}
runNextTest()
}
if (params[0] === 'abort') { // abort command
tlog('manually aborted')
clearRequests() // stop all xhr activity
runNextTest=null;
if (interval) clearInterval(interval) // clear timer if present
if (settings.telemetry_level > 1) sendTelemetry()
testStatus = 5; dlStatus = ''; ulStatus = ''; pingStatus = ''; jitterStatus = '' // set test as aborted
Expand All @@ -134,9 +151,10 @@ function clearRequests () {
}
}
// gets client's IP using url_getIp, then calls the done function
var ipCalled = false // used to prevent multiple accidental calls to getIp
function getIp (done) {
tlog('getIp')
if (settings.url_getIp == "-1") {done(); return}
if (ipCalled) return; else ipCalled = true // getIp already called?
xhr = new XMLHttpRequest()
xhr.onload = function () {
tlog("IP: "+xhr.responseText)
Expand All @@ -155,7 +173,6 @@ var dlCalled = false // used to prevent multiple accidental calls to dlTest
function dlTest (done) {
tlog('dlTest')
if (dlCalled) return; else dlCalled = true // dlTest already called?
if (settings.url_dl === '-1') {done(); return}
var totLoaded = 0.0, // total number of loaded bytes
startT = new Date().getTime(), // timestamp when test was started
graceTimeDone = false, //set to true after the grace time is past
Expand Down Expand Up @@ -244,7 +261,6 @@ var ulCalled = false // used to prevent multiple accidental calls to ulTest
function ulTest (done) {
tlog('ulTest')
if (ulCalled) return; else ulCalled = true // ulTest already called?
if (settings.url_ul === '-1') {done(); return}
var totLoaded = 0.0, // total number of transmitted bytes
startT = new Date().getTime(), // timestamp when test was started
graceTimeDone = false, //set to true after the grace time is past
Expand Down Expand Up @@ -339,7 +355,7 @@ function ulTest (done) {
if (failed || isNaN(ulStatus)) ulStatus = 'Fail'
clearRequests()
clearInterval(interval)
tlog('ulTest finished '+ulStatus)
tlog('ulTest finished '+ulStatus)
done()
}
}
Expand All @@ -350,7 +366,6 @@ var ptCalled = false // used to prevent multiple accidental calls to pingTest
function pingTest (done) {
tlog('pingTest')
if (ptCalled) return; else ptCalled = true // pingTest already called?
if (settings.url_ping === '-1') {done(); return}
var prevT = null // last time a pong was received
var ping = 0.0 // current ping value
var jitter = 0.0 // current jitter value
Expand Down
Loading

0 comments on commit 60c707d

Please sign in to comment.