No backend (nginx only) example

This commit is contained in:
Pilskalns 2017-10-29 12:35:29 +02:00
parent 57463bd314
commit 699efa1ecd
3 changed files with 147 additions and 1 deletions

26
doc.md
View file

@ -228,7 +228,9 @@ You're running the test on localhost, therefore it is trying to measure the spee
Make sure your server is sending the ```Connection:keep-alive``` header
## Using the test without PHP
If your server does not support PHP, or you're using something newer like Node.js, you can still use this test by replacing `garbage.php`, `empty.php` and `getIP.php` with equivalents.
If your server **does not support PHP**, maybe you have only **nginx** for static files, or you're using something newer like **Node.js**, you can still use this test by replacing `garbage.php`, `empty.php` and `getIP.php` with equivalents.
For **nginx** see comments below and [example 9](example9_no_backend.html#L115)
### Replacements
@ -241,12 +243,34 @@ If you're using Node.js or some other server, your replacement should accept the
It is important here to turn off compression, and generate incompressible data.
A symlink to `/dev/urandom` is also ok.
**nginx**
Use tool above to generate static known file size. Upload to server so it is available something like `/speedtest/20MB.file`
#### Replacement for `empty.php`
Your replacement must simply respond with a HTTP code 200 and send nothing else. You may want to send additional headers to disable caching. The test assumes that Connection:keep-alive is sent by the server.
**nginx**
1. Increase allowed POST body size
2. Create empty text file `empty.file`
3. Allow HTTP POST method to static/empty file and return HTTP 200 even nothing is saved or processed by server
```nginx
location /speedtest/empty.file {
client_max_body_size 30M;
error_page 405 =200 $uri;
}
```
#### Replacement for `getIP.php`
Your replacement must simply respond with the client's IP as plaintext. Nothing fancy.
**nginx**
```nginx
location /speedtest/getip {
return 200 $remote_addr;
}
```
#### JS
You need to start the test with your replacements like this:

BIN
empty.file Normal file

Binary file not shown.

122
example9_no_backend.html Normal file
View file

@ -0,0 +1,122 @@
<!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 rel="icon" href="favicon.png" />
<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>
</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>
</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>
</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>
</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 class="row">
<textarea id="cp-box" rows="6" cols="40" readonly style="display: none; margin: 50px auto; resize: none;">
</textarea>
</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 cp = document.getElementById('cp-box')
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]
if (status < 4 ){
cp.style.display = "none"
}
if (status === 4 ){
cp.textContent= "Download " + download.textContent + " Mbit/s\n" +
"Upload " + upload.textContent + " Mbit/s\n" +
"Ping " + ping.textContent + " ms\n" +
"Jitter " + jitter.textContent + " ms\n" +
"IP " + ip.textContent + "\n" +
"Date " + new Date().toLocaleString()
cp.style.display = "block"
}
}
worker.postMessage('start {"url_dl": "/speedtest/20MB.file", "url_ul": "/speedtest/empty.file", "url_ping": "/speedtest/empty.file", "url_getIp": "/speedtest/getip"}')
}
function stopTest() {
if (worker) worker.postMessage('abort')
}
</script>
</body>
</html>