add support to disable specific tests when pass "-1" (#52)

* add support to disable specific tests when pass "-1"

* add checkbox on example7.html & remove unnecessary codes in js
This commit is contained in:
Larry Song 2017-07-19 16:04:37 +10:00 committed by Federico Dossena
parent b20c13728f
commit f669b56f98
4 changed files with 127 additions and 1 deletions

2
doc.md
View file

@ -129,6 +129,8 @@ If you want, you can change these settings and pass them to the worker as JSON w
w.postMessage('start {"param1": "value1", "param2": "value2", ...}')
```
Pass "-1" to disable specific tests
#### Test parameters
* __time_dl__: How long the download test should be in seconds. The test will continue regardless of this limit if the speed is still 0.00 when the limit is reached.
* Default: `15`

119
example7.html Normal file
View file

@ -0,0 +1,119 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta name="referrer" content="no-referrer" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no, user-scalable=no" />
<title>Speedtest</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" rel="stylesheet" />
<style type="text/css">
.st-block {
text-align: center;
}
.st-btn {
margin-top: -0.5rem;
margin-left: 1.5rem;
}
.st-value>span:empty::before {
content: "0.00";
color: #636c72;
}
#st-ip:empty::before {
content: "___.___.___.___";
color: #636c72;
}
</style>
</head>
<body class="my-4">
<div class="container">
<div class="row">
<div class="col-sm-12 mb-3">
<p class="h1">
Speedtest
<button id="st-start" class="btn btn-outline-primary st-btn" onclick="startTest()">Start</button>
<button id="st-stop" class="btn btn-danger st-btn" onclick="stopTest()" hidden="true">Stop</button>
</p>
<p class="lead">
Your IP: <span id="st-ip"></span>
<input type="checkbox" id="st-ip-checkbox">
</p>
</div>
<div class="col-lg-3 col-md-6 mb-3 st-block">
<h3>Download</h3>
<p class="display-4 st-value"><span id="st-download"></span></p>
<p class="lead">Mbit/s</p>
<p><input type="checkbox" id="st-download-checkbox"></p>
</div>
<div class="col-lg-3 col-md-6 mb-3 st-block">
<h3>Upload</h3>
<p class="display-4 st-value"><span id="st-upload"></span></p>
<p class="lead">Mbit/s</p>
<p><input type="checkbox" id="st-upload-checkbox"></p>
</div>
<div class="col-lg-3 col-md-6 mb-3 st-block">
<h3>Ping</h3>
<p class="display-4 st-value"><span id="st-ping"></span></p>
<p class="lead">ms</p>
<p><input type="checkbox" id="st-ping-checkbox" checked></p>
</div>
<div class="col-lg-3 col-md-6 mb-3 st-block">
<h3>Jitter</h3>
<p class="display-4 st-value"><span id="st-jitter"></span></p>
<p class="lead">ms</p>
</div>
</div>
</div>
<script type="text/javascript">
var worker = null
function startTest() {
document.getElementById('st-start').hidden = true
document.getElementById('st-stop').hidden = false
worker = new Worker('speedtest_worker.min.js')
var interval = setInterval(function () { worker.postMessage('status') }, 100)
worker.onmessage = function (event) {
var download = document.getElementById('st-download')
var upload = document.getElementById('st-upload')
var ping = document.getElementById('st-ping')
var jitter = document.getElementById('st-jitter')
var ip = document.getElementById('st-ip')
var data = event.data.split(';')
var status = Number(data[0])
if (status >= 4) {
clearInterval(interval)
document.getElementById('st-start').hidden = false
document.getElementById('st-stop').hidden = true
w = null
}
if (status === 5) {
// speedtest cancelled, clear output data
data = []
}
download.textContent = (status==1&&data[1]==0)?"Starting":data[1]
upload.textContent = (status==3&&data[2]==0)?"Starting":data[2]
ping.textContent = data[3]
ip.textContent = data[4]
jitter.textContent = data[5]
}
var ip_checkbox = document.getElementById("st-ip-checkbox")
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"')}
worker.postMessage('start {' + str_parameters.join(",") + '}')
}
function stopTest() {
if (worker) worker.postMessage('abort')
}
</script>
</body>
</html>

View file

@ -94,6 +94,7 @@ this.addEventListener('message', function (e) {
settings.xhr_dlMultistream = 5
}
}
if (typeof s.count_ping !== 'undefined') settings.count_ping = s.count_ping // number of pings for ping test
if (typeof s.xhr_dlMultistream !== 'undefined') settings.xhr_dlMultistream = s.xhr_dlMultistream // number of download streams
if (typeof s.xhr_ulMultistream !== 'undefined') settings.xhr_ulMultistream = s.xhr_ulMultistream // number of upload streams
@ -129,6 +130,7 @@ function clearRequests () {
}
// gets client's IP using url_getIp, then calls the done function
function getIp (done) {
if (settings.url_getIp == "-1") {done(); return}
xhr = new XMLHttpRequest()
xhr.onload = function () {
clientIp = xhr.responseText
@ -144,6 +146,7 @@ function getIp (done) {
var dlCalled = false // used to prevent multiple accidental calls to dlTest
function dlTest (done) {
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
@ -225,6 +228,7 @@ reqsmall = new Blob(reqsmall)
var ulCalled = false // used to prevent multiple accidental calls to ulTest
function ulTest (done) {
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
@ -319,6 +323,7 @@ function ulTest (done) {
var ptCalled = false // used to prevent multiple accidental calls to pingTest
function pingTest (done) {
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

File diff suppressed because one or more lines are too long