From 046137cf6ce6fe4ceccf5d2e97421c3a7eafaa68 Mon Sep 17 00:00:00 2001 From: dosse91 Date: Mon, 30 Oct 2017 12:35:49 +0100 Subject: [PATCH] Ping test can now use timing from Performance API; New examples; Minor changes --- doc.md | 138 +++++++++++---- example1.html => example-basic.html | 8 +- example5.html => example-chart.html | 11 +- example-customSettings.html | 199 +++++++++++++++++++++ example-customSettings2.html | 172 ++++++++++++++++++ example-gauges.html | 259 ++++++++++++++++++++++++++++ example-pretty.html | 193 +++++++++++++++++++++ example-progressBar.html | 215 +++++++++++++++++++++++ example-telemetryEnabled.html | 194 +++++++++++++++++++++ example2.html | 91 ---------- example3.html | 133 -------------- example4.html | 197 --------------------- example6.html | 104 ----------- example7.html | 116 ------------- example8_telemetry.html | 105 ----------- getIP.php | 1 + speedtest_worker.js | 56 ++++-- speedtest_worker.min.js | 2 +- 18 files changed, 1391 insertions(+), 803 deletions(-) rename example1.html => example-basic.html (88%) rename example5.html => example-chart.html (96%) create mode 100644 example-customSettings.html create mode 100644 example-customSettings2.html create mode 100644 example-gauges.html create mode 100644 example-pretty.html create mode 100644 example-progressBar.html create mode 100644 example-telemetryEnabled.html delete mode 100644 example2.html delete mode 100644 example3.html delete mode 100644 example4.html delete mode 100644 example6.html delete mode 100644 example7.html delete mode 100644 example8_telemetry.html diff --git a/doc.md b/doc.md index 8afae69..755fcfe 100644 --- a/doc.md +++ b/doc.md @@ -1,7 +1,7 @@ # HTML5 Speedtest > by Federico Dossena -> Version 4.4, October 14 2017 +> Version 4.5, November 1 2017 > [https://github.com/adolfintel/speedtest/](https://github.com/adolfintel/speedtest/) @@ -41,14 +41,47 @@ Later we'll see how to use the test without PHP, and how to configure the teleme __Important:__ keep all the files together; all paths are relative to the js file +## Basic usage +You can start using this speedtest on your site without any special knowledge. +Start by copying one of the included examples. Here's a description for each of them: +* `example-basic.html`: This example shows the most basic configuration possible. Everything runs with the default settings, in a very simple page where the output is shown +* `example-pretty.html`: This is a more sophisticated example, with a nicer layout and a start/stop button. __This is the best starting point for most users__ +* `example-progressBar.html`: A modified version of `example-pretty.html` with a progress indicator +* `example-customSettings.html`: A modified version of `example-pretty.html` showing how the test can be started with custom parameters +* `example-customSettings2.html`: A modified version of `example-pretty.html` showing how to make a custom test with only download and upload +* `example-gauges.html`: The most sophisticated example, with the same functions as `example-pretty.html` but also gauges and progress indicators for each test. This is the nicest example included, and also a good starting point, but drawing the gauges may slow down the test on slow devices like a Raspberry Pi +* `example-chart.html`: The old example5.html, showing how to use the test with the Chart.js library +* `example-telemetry.html`: A modified version of `example-pretty.html` with basic telemetry turned on. See the section on Telemetry for details -## Usage -You can modify one of the examples or start from scratch. If you are just editing one of the example, skip to the "test parameters" section. +### Customizing your example +The included examples are good starting places if you want to have a simple speedtest on your site. +Once you've tested everything and you're sure that everything works, edit it and add some custom stuff like your logo or new colors. +If you want to change the test parameters, for instance to make the download test shorter, you can do so in every example: +Look for the line that contains `postMessage('start ` +This is where custom parameters can be passed to the test as a JSON string. You can write the string manually or use ``JSON.stringify`` to do that for you. +Here's an example: +```js +w.postMessage('start {"time_dl":"10"}); +``` +This starts the test with default settings, but sets the download test to last only 10 seconds. +Here's a cleaner version using ``JSON.stringify``: +```js +var params={ + time_dl:10 +} +w.postMessage('start '+JSON.stringify(params)) +``` +Notice that there is a space after the word `start`, don't forget that! + +For a list of all test settings, look below, under Test parameters and Advanced test parameters. __Do not change anything if you don't know what you're doing.__ + +## Advanced usage +If you don't want to start from one of the examples, here's how to use the worker. Examples are still good for reference, so keep them handy. To run the test, you need to do 3 things: * Create the worker -* Write some code that handles the responses coming from the worker +* Write some code that handles the data coming from the worker * Start the test ### Creating the worker @@ -78,6 +111,9 @@ w.onmessage = function (event) { var pingStatus = data[3] var jitterStatus = data[5] var clientIp = data[4] + var dlProgress = data[6] + var ulProgress = data[7] + var pingProgress = data[8] if (testState >= 4) { clearInterval(timer) // test is finished or aborted } @@ -89,7 +125,7 @@ w.onmessage = function (event) { The response from the worker is composed of values separated by `;` (semicolon) in this format: -`testState;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus` +`testState;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus;dlProgress;ulProgress;pingProgress` * __testState__ is an integer 0-5 * `0` = Test starting @@ -117,6 +153,11 @@ format: * Empty string (not started or aborted) * Estimated jitter in milliseconds as a number with 2 decimals (lower = stable connection) * The string "Fail" (test failed) +* __dlProgress__: the progress of the download test as a number between 0 and 1 +* __ulProgress__: the progress of the upload test as a number between 0 and 1 +* __pingProgress__: the progress of the ping+jitter test as a number between 0 and 1 + +Note: clientIp appears before jitterStatus. This is not a mistake, it's to keep the js file compatible with older pages from before the jitter test was introduced. ### Starting the test To start the test with the default settings, which is usually the best choice, send the start command to the worker: @@ -130,6 +171,15 @@ If you want, you can change these settings and pass them to the worker as JSON w ```js w.postMessage('start {"param1": "value1", "param2": "value2", ...}') ``` +or this: +```js +var params{ + param1:value1, + param2:value2, + ... +} +w.postMessage('start '+JSON.stringify(params)) +``` #### 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. @@ -191,13 +241,19 @@ w.postMessage('start {"param1": "value1", "param2": "value2", ...}') * __time_ulGraceTime__: How long to wait (in seconds) before actually measuring the upload speed. This is a good idea because we want to wait for the buffers to be full (avoids the peak at the beginning of the test) * Default: `3` * Recommended: `>=1` +* __ping_allowPerformanceApi__: toggles use of Performance API to improve accuracy of Ping/Jitter test on browsers that support it. + * Default: `true` +* __useMebibits__: use mebibits/s instead of megabits/s for the speeds + * Default: `false` * __overheadCompensationFactor__: compensation for HTTP and network overhead. Default value assumes typical MTUs used over the Internet. You might want to change this if you're using this in your internal network with different MTUs, or if you're using IPv6 instead of IPv4. - * Default: `1.13359567567567567568` (1048576/925000) assumes HTTP+TCP+IPv4+ETH with typical MTUs used over the Internet - * `1.0513`: HTTP+TCP+IPv6+ETH, over the Internet (empirically tested, not calculated) + * Default: `1.06` probably a decent estimate for all overhead. This was measured empirically by comparing the measured speed and the speed reported by my the network adapter. + * `1048576/925000`: old default value. This is probably too high. + * `1.0513`: HTTP+TCP+IPv6+ETH, over the Internet (empirically tested, not calculated) * `1.0369`: Alternative value for HTTP+TCP+IPv4+ETH, over the Internet (empirically tested, not calculated) - * `1460 / 1514`: TCP+IPv4+ETH, ignoring HTTP overhead - * `1440 / 1514`: TCP+IPv6+ETH, ignoring HTTP overhead - * `1`: ignore overheads. This measures the speed at which you actually download and upload files + * `1.081`: Yet another alternative value for over the Internet (empirically tested, not calculated) + * `1514 / 1460`: TCP+IPv4+ETH, ignoring HTTP overhead + * `1514 / 1440`: TCP+IPv6+ETH, ignoring HTTP overhead + * `1`: ignore overheads. This measures the speed at which you actually download and upload files rather than the raw connection speed ### Aborting the test prematurely The test can be aborted at any time by sending an abort command to the worker: @@ -210,22 +266,12 @@ This will terminate all network activity and stop the worker. __Important:__ do not simply kill the worker while it's running, as it may leave pending XHR requests! -## Troubleshooting -These are the most common issues reported by users, and how to fix them +### Important notice on backwards compatibility +__Do NOT link the js file from github or fdossena.com directly into your html file. __ -#### Download test gives very low result -Are garbage.php and empty.php (or your replacements) reachable? -Press F12, select network and start the test. Do you see errors? (cancelled requests are not errors) -If a small download starts, open it in a text editor. Does it say it's missing openssl_random_pseudo_bytes()? In this case, install OpenSSL (this is usually included when you install Apache and PHP on most distros). - -#### Upload test is inaccurate, and I see lag spikes -Check your server's maximum POST size, make sure it's at least 20Mbytes, possibly more - -#### All tests are wrong, give extremely high results, browser lags/crashes, ... -You're running the test on localhost, therefore it is trying to measure the speed of your loopback interface. The test is meant to be run over an Internet connection, from a different machine. - -#### Ping test shows double the actual ping -Make sure your server is sending the ```Connection:keep-alive``` header +A lot of web developers think that referring to the latest version of a library in their project is a good thing. It is not. +Things may change and I don't want to break your project, so do yourself a favor, and keep all files on your server. +You have been warned. ## 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. @@ -289,16 +335,40 @@ Example: w.postMessage('start {"telemetry_level":"basic"}') ``` -Also, see example8_telemetry.html +Also, see example-telemetry.html -### See the results +### Seeing the results At the moment there is no front-end to see the telemetry data; you can connect to the database and see the collected results in the `speedtest_users` table. +## Troubleshooting +These are the most common issues reported by users, and how to fix them. If you still need help, contact me at [dosse91@paranoici.org](mailto:dosse91@paranoici.org). + +#### Download test gives very low result +Are garbage.php and empty.php (or your replacements) reachable? +Press F12, select network and start the test. Do you see errors? (cancelled requests are not errors) +If a small download starts, open it in a text editor. Does it say it's missing openssl_random_pseudo_bytes()? In this case, install OpenSSL (this is usually included when you install Apache and PHP on most distros). + +#### Upload test is inaccurate, and/or I see lag spikes +Check your server's maximum POST size, make sure it's at least 20Mbytes, possibly more + +#### Download and/or upload results are slightly too optimistic +The test was fine tuned to run over a typical IPv4 internet connection. If you're using it under different conditions, see the ``overheadCompensationFactor`` parameter. + +#### All tests are wrong, give extremely high results, browser lags/crashes, ... +You're running the test on localhost, therefore it is trying to measure the speed of your loopback interface. The test is meant to be run over an Internet connection, from a different machine. + +#### Ping test shows double the actual ping +Make sure your server is sending the ```Connection:keep-alive``` header + ## Known bugs and limitations -* The ping/jitter test is measured by seeing how long it takes for an empty XHR to complete. It is not an acutal ICMP ping -* __IE11, Edge 15 and 16 (only):__ the upload test is not precise, especially on very fast connections -* __IE11:__ the upload test may not work over HTTPS -* __Firefox:__ on some Linux systems with hardware acceleration turned off, the page rendering makes the browser lag, reducing the accuracy of the ping/jitter test +### General +* The ping/jitter test is measured by seeing how long it takes for an empty XHR to complete. It is not an acutal ICMP ping. Different browsers may also show different results, especially on very fast connections on slow devices. +### IE-Specific +* The upload test is not precise on very fast connections with high latency (will probably be fixed by Edge 17) +* On IE11, a same origin policy error is erroneously triggered under unknown conditions. Seems to be related to running the test from unusual URLs like a top level domain (for instance http://abc/speedtest). These are bugs in IE11's implementation of the same origin policy, not in the speedtest itself. +* On IE11, under unknown circumstances, on some systems the test can only be run once, after which speedtest_worker.js will not be loaded by IE until the browser is restarted. This is a rare bug in IE11. +### Firefox-Specific +* On some Linux systems with hardware acceleration turned off, the page rendering makes the browser lag, reducing the accuracy of the ping/jitter test ## Making changes Since this is an open source project, you can modify it. @@ -308,10 +378,10 @@ To make changes to the speedtest itself, edit `speedtest_worker.js` To create the minified version, use UglifyJS like this: ``` -uglifyjs -c --screw-ie8 speedtest_worker.js > speedtest_worker.min.js +uglifyjs -c speedtest_worker.js > speedtest_worker.min.js ``` -Pull requests are much appreciated. If you don't use github (or git), simply contact me at dosse91@paranoici.org. +Pull requests are much appreciated. If you don't use github (or git), simply contact me at [dosse91@paranoici.org](mailto:dosse91@paranoici.org). __Important:__ please add your name to modified versions to distinguish them from the main project. @@ -321,3 +391,5 @@ This software is under the GNU LGPL license, Version 3 or newer. To put it short: you are free to use, study, modify, and redistribute this software and modified versions of it, for free or for money. You can also use it in proprietary software but all changes to this software must remain under the same GNU LGPL license. + +Contact me at [dosse91@paranoici.org](mailto:dosse91@paranoici.org) for other licensing models. diff --git a/example1.html b/example-basic.html similarity index 88% rename from example1.html rename to example-basic.html index 6bf96f1..6dbe4a5 100644 --- a/example1.html +++ b/example-basic.html @@ -1,11 +1,12 @@ + - Speedtest + HTML5 Speedtest -

Speedtest

+

HTML5 Speedtest

IP Address

@@ -31,5 +32,8 @@ } w.postMessage('start') // start the speedtest (default params. keep garbage.php and empty.dat in the same directory as the js file) + + Source code + diff --git a/example5.html b/example-chart.html similarity index 96% rename from example5.html rename to example-chart.html index 48f012b..6243b1a 100644 --- a/example5.html +++ b/example-chart.html @@ -1,7 +1,8 @@ - Speedtest + + HTML5 Speedtest + + + +

HTML5 Speedtest - Custom settings example

+
+
+
+
+
Download
+
+
Mbps
+
+
+
Upload
+
+
Mbps
+
+
+
+
+
Ping
+
+
ms
+
+
+
Jitter
+
+
ms
+
+
+
+ IP Address: +
+
+
Custom parameters:
+Source code + + + \ No newline at end of file diff --git a/example-customSettings2.html b/example-customSettings2.html new file mode 100644 index 0000000..8a9353a --- /dev/null +++ b/example-customSettings2.html @@ -0,0 +1,172 @@ + + + + + +HTML5 Speedtest + + + + +

HTML5 Speedtest - Custom settings example

+
+
+
+
+
Download
+
+
Mbps
+
+
+
Upload
+
+
Mbps
+
+
+
+Source code + + + \ No newline at end of file diff --git a/example-gauges.html b/example-gauges.html new file mode 100644 index 0000000..f281590 --- /dev/null +++ b/example-gauges.html @@ -0,0 +1,259 @@ + + + + + +HTML5 Speedtest + + + + +

HTML5 Speedtest

+
+
+
+
+
Download
+ +
+
Mbps
+
+
+
Upload
+ +
+
Mbps
+
+
+
+
+
Ping
+ +
+
ms
+
+
+
Jitter
+ +
+
ms
+
+
+
+ IP Address: +
+
+Source code + + + \ No newline at end of file diff --git a/example-pretty.html b/example-pretty.html new file mode 100644 index 0000000..e601abd --- /dev/null +++ b/example-pretty.html @@ -0,0 +1,193 @@ + + + + + +HTML5 Speedtest + + + + +

HTML5 Speedtest

+
+
+
+
+
Download
+
+
Mbps
+
+
+
Upload
+
+
Mbps
+
+
+
+
+
Ping
+
+
ms
+
+
+
Jitter
+
+
ms
+
+
+
+ IP Address: +
+
+Source code + + + \ No newline at end of file diff --git a/example-progressBar.html b/example-progressBar.html new file mode 100644 index 0000000..cf2b51d --- /dev/null +++ b/example-progressBar.html @@ -0,0 +1,215 @@ + + + + + +HTML5 Speedtest + + + + +

HTML5 Speedtest

+
+
+
+
+
+
Download
+
+
Mbps
+
+
+
Upload
+
+
Mbps
+
+
+
+
+
Ping
+
+
ms
+
+
+
Jitter
+
+
ms
+
+
+
+ IP Address: +
+
+Source code + + + \ No newline at end of file diff --git a/example-telemetryEnabled.html b/example-telemetryEnabled.html new file mode 100644 index 0000000..ba253cb --- /dev/null +++ b/example-telemetryEnabled.html @@ -0,0 +1,194 @@ + + + + + +HTML5 Speedtest + + + + +

HTML5 Speedtest - Telemetry example

+
+
+
+
+
Download
+
+
Mbps
+
+
+
Upload
+
+
Mbps
+
+
+
+
+
Ping
+
+
ms
+
+
+
Jitter
+
+
ms
+
+
+
+ IP Address: +
+
+
Basic telemetry is active; results will be saved in your database, without the full log. If the results don't appear, check the settings in telemetry.php
+Source code + + + \ No newline at end of file diff --git a/example2.html b/example2.html deleted file mode 100644 index cf228ba..0000000 --- a/example2.html +++ /dev/null @@ -1,91 +0,0 @@ - - - - Speedtest - - - - -

Speedtest

-
-
Download
-
  
-
Mbit/s
-
-
-
Upload
-
  
-
Mbit/s
-
-
-
Latency
-
  
-
ms
-
-
-
Jitter
-
  
-
ms
-
-
- - - diff --git a/example3.html b/example3.html deleted file mode 100644 index f7b0d5e..0000000 --- a/example3.html +++ /dev/null @@ -1,133 +0,0 @@ - - - - Speedtest - - - - - -

Speedtest

- - Run speedtest - - diff --git a/example4.html b/example4.html deleted file mode 100644 index 09519ab..0000000 --- a/example4.html +++ /dev/null @@ -1,197 +0,0 @@ - - - - Speedtest - - - - - - - - -

Speed Test

-
-
-
-
-
-
-
-
- Start - -
-

Fonts: Google Fonts | Gauges: justgage.com

- - diff --git a/example6.html b/example6.html deleted file mode 100644 index 3a420d5..0000000 --- a/example6.html +++ /dev/null @@ -1,104 +0,0 @@ - - - - - - - Speedtest - - - - - - -
-
-
-

- Speedtest - - -

-

- Your IP: -

-
-
-

Download

-

-

Mbit/s

-
-
-

Upload

-

-

Mbit/s

-
-
-

Ping

-

-

ms

-
-
-

Jitter

-

-

ms

-
-
-
- - - - diff --git a/example7.html b/example7.html deleted file mode 100644 index 0e1d184..0000000 --- a/example7.html +++ /dev/null @@ -1,116 +0,0 @@ - - - - - - - Speedtest - - - - - - -
-
-
-

- Speedtest - - -

-

- Your IP: - -

-
-
-

Download

-

-

Mbit/s

-

-
-
-

Upload

-

-

Mbit/s

-

-
-
-

Ping

-

-

ms

-

-
-
-

Jitter

-

-

ms

-
-
-
- - - - diff --git a/example8_telemetry.html b/example8_telemetry.html deleted file mode 100644 index 122c9ac..0000000 --- a/example8_telemetry.html +++ /dev/null @@ -1,105 +0,0 @@ - - - - - - - Speedtest - - - - - - -
-
-
-

- Speedtest - - -

Basic telemetry is active; results will be saved in your database. If the results don't appear, check the settings in telemetry.php

-

-

- Your IP: -

-
-
-

Download

-

-

Mbit/s

-
-
-

Upload

-

-

Mbit/s

-
-
-

Ping

-

-

ms

-
-
-

Jitter

-

-

ms

-
-
-
- - - - diff --git a/getIP.php b/getIP.php index 0b55b77..b6b2fbe 100644 --- a/getIP.php +++ b/getIP.php @@ -1,3 +1,4 @@ diff --git a/speedtest_worker.js b/speedtest_worker.js index 5be6802..347754d 100644 --- a/speedtest_worker.js +++ b/speedtest_worker.js @@ -1,5 +1,5 @@ /* - HTML5 Speedtest v4.4 + HTML5 Speedtest v4.5 by Federico Dossena https://github.com/adolfintel/speedtest/ GNU LGPLv3 License @@ -12,6 +12,9 @@ var ulStatus = '' // upload speed in megabit/s with 2 decimal digits var pingStatus = '' // ping in milliseconds with 2 decimal digits var jitterStatus = '' // jitter in milliseconds with 2 decimal digits var clientIp = '' // client's IP address as reported by getIP.php +var dlProgress = 0 //progress of download test 0-1 +var ulProgress = 0 //progress of upload test 0-1 +var pingProgress = 0 //progress of ping+jitter test 0-1 var log='' //telemetry log function tlog(s){log+=Date.now()+': '+s+'\n'} @@ -35,7 +38,9 @@ var settings = { xhr_dlUseBlob: false, // if set to true, it reduces ram usage but uses the hard drive (useful with large garbagePhp_chunkSize and/or high xhr_dlMultistream) garbagePhp_chunkSize: 20, // size of chunks sent by garbage.php (can be different if enable_quirks is active) enable_quirks: true, // enable quirks for specific browsers. currently it overrides settings to optimize for specific browsers, unless they are already being overridden with the start command - overheadCompensationFactor: 1048576/925000, //compensation for HTTP+TCP+IP+ETH overhead. 925000 is how much data is actually carried over 1048576 (1mb) bytes downloaded/uploaded. This default value assumes HTTP+TCP+IPv4+ETH with typical MTUs over the Internet. You may want to change this if you're going through your local network with a different MTU or if you're going over IPv6 (see doc.md for some other values) + ping_allowPerformanceApi: true, // if enabled, the ping test will attempt to calculate the ping more precisely using the Performance API. Currently works perfectly in Chrome, badly in Edge, and not at all in Firefox. If Performance API is not supported or the result is obviously wrong, a fallback is provided. + overheadCompensationFactor: 1.06, //can be changed to compensatie for transport overhead. (see doc.md for some other values) + useMebibits: false, //if set to true, speed will be reported in mebibits/s instead of megabits/s telemetry_level: 0, // 0=disabled, 1=basic (results only), 2=full (results+log) url_telemetry: 'telemetry.php' // path to the script that adds telemetry data to the database } @@ -52,7 +57,7 @@ function url_sep (url) { return url.match(/\?/) ? '&' : '?'; } /* listener for commands from main thread to this worker. commands: - -status: returns the current status as a string of values spearated by a semicolon (;) in this order: testStatus;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus + -status: returns the current status as a string of values spearated by a semicolon (;) in this order: testStatus;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus;dlProgress;ulProgress;pingProgress -abort: aborts the current test -start: starts the test. optionally, settings can be passed as JSON. example: start {"time_ul":"10", "time_dl":"10", "count_ping":"50"} @@ -60,7 +65,7 @@ function url_sep (url) { return url.match(/\?/) ? '&' : '?'; } this.addEventListener('message', function (e) { var params = e.data.split(' ') if (params[0] === 'status') { // return status - postMessage(testStatus + ';' + dlStatus + ';' + ulStatus + ';' + pingStatus + ';' + clientIp + ';' + jitterStatus) + postMessage(testStatus + ';' + dlStatus + ';' + ulStatus + ';' + pingStatus + ';' + clientIp + ';' + jitterStatus + ';' + dlProgress + ';' + ulProgress + ';' + pingProgress) } if (params[0] === 'start' && testStatus === 0) { // start new test testStatus = 1 @@ -93,10 +98,9 @@ this.addEventListener('message', function (e) { // edge more precise with 3 download streams settings.xhr_dlMultistream = 3 } - if (/Edge\/15.(\d+)/i.test(ua) || /Edge\/16.(\d+)/i.test(ua)) { - //Edge 15 introduced a bug that causes onprogress events to not get fired, so for Edge 15, we have to use the "small chunks" workaround that reduces accuracy - settings.forceIE11Workaround = true - } + //Edge 15 introduced a bug that causes onprogress events to not get fired, we have to use the "small chunks" workaround that reduces accuracy + //TODO: MOVE OUT OF QUIRKS! + settings.forceIE11Workaround = true } if (/Chrome.(\d+)/i.test(ua) && (!!self.fetch)) { if(typeof s.xhr_dlMultistream === 'undefined'){ @@ -113,13 +117,14 @@ this.addEventListener('message', function (e) { // run the tests tlog(JSON.stringify(settings)) test_pointer=0; + var iRun=false,dRun=false,uRun=false,pRun=false; 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 'I':{test_pointer++; if(iRun) {runNextTest(); return;} else iRun=true; getIp(runNextTest);} break; + case 'D':{test_pointer++; if(dRun) {runNextTest(); return;} else dRun=true; testStatus=1; dlTest(runNextTest);} break; + case 'U':{test_pointer++; if(uRun) {runNextTest(); return;} else uRun=true; testStatus=3; ulTest(runNextTest);} break; + case 'P':{test_pointer++; if(pRun) {runNextTest(); return;} else pRun=true; testStatus=2; pingTest(runNextTest);} break; case '_':{test_pointer++; setTimeout(runNextTest,1000);} break; default: test_pointer++; } @@ -221,6 +226,7 @@ function dlTest (done) { interval = setInterval(function () { tlog('DL: '+dlStatus+(graceTimeDone?'':' (in grace time)')) var t = new Date().getTime() - startT + if (graceTimeDone) dlProgress = t / (settings.time_dl * 1000) if (t < 200) return if (!graceTimeDone){ if (t > 1000 * settings.time_dlGraceTime){ @@ -232,11 +238,12 @@ function dlTest (done) { } }else{ var speed = totLoaded / (t / 1000.0) - dlStatus = ((speed * 8 * settings.overheadCompensationFactor)/1048576).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 to go to megabits/s + dlStatus = ((speed * 8 * settings.overheadCompensationFactor)/(settings.useMebibits?1048576:1000000)).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 or 1000000 to go to megabits/mebibits if (((t / 1000.0) > settings.time_dl && dlStatus > 0) || failed) { // test is over, stop streams and timer if (failed || isNaN(dlStatus)) dlStatus = 'Fail' clearRequests() clearInterval(interval) + dlProgress = 1 tlog('dlTest finished '+dlStatus) done() } @@ -337,6 +344,7 @@ function ulTest (done) { interval = setInterval(function () { tlog('UL: '+ulStatus+(graceTimeDone?'':' (in grace time)')) var t = new Date().getTime() - startT + if (graceTimeDone) ulProgress = t / (settings.time_ul * 1000) if (t < 200) return if (!graceTimeDone){ if (t > 1000 * settings.time_ulGraceTime){ @@ -348,11 +356,12 @@ function ulTest (done) { } }else{ var speed = totLoaded / (t / 1000.0) - ulStatus = ((speed * 8 * settings.overheadCompensationFactor)/1048576).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 to go to megabits/s + ulStatus = ((speed * 8 * settings.overheadCompensationFactor)/(settings.useMebibits?1048576:1000000)).toFixed(2) // speed is multiplied by 8 to go from bytes to bits, overhead compensation is applied, then everything is divided by 1048576 or 1000000 to go to megabits/mebibits if (((t / 1000.0) > settings.time_ul && ulStatus > 0) || failed) { // test is over, stop streams and timer if (failed || isNaN(ulStatus)) ulStatus = 'Fail' clearRequests() clearInterval(interval) + ulProgress = 1 tlog('ulTest finished '+ulStatus) done() } @@ -373,6 +382,7 @@ function pingTest (done) { // ping function var doPing = function () { tlog('ping') + pingProgress = i / settings.count_ping prevT = new Date().getTime() xhr[0] = new XMLHttpRequest() xhr[0].onload = function () { @@ -381,7 +391,21 @@ function pingTest (done) { if (i === 0) { prevT = new Date().getTime() // first pong } else { - var instspd = (new Date().getTime() - prevT) + var instspd = new Date().getTime() - prevT + if(settings.ping_allowPerformanceApi){ + try{ + //try to get accurate performance timing using performance api + var p=performance.getEntries() + p=p[p.length-1] + var d = p.responseStart - p.requestStart //best precision: chromium-based + if (d<=0) d=p.duration //edge: not so good precision because it also considers the overhead and there is no way to avoid it + if (d>0&&dloadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].onload=function(){tlog("dl stream finished "+i);try{xhr[i].abort()}catch(e){}testStream(i,0)}.bind(this),xhr[i].onerror=function(){tlog("dl stream failed "+i),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this);try{settings.xhr_dlUseBlob?xhr[i].responseType="blob":xhr[i].responseType="arraybuffer"}catch(e){}xhr[i].open("GET",settings.url_dl+url_sep(settings.url_dl)+"r="+Math.random()+"&ckSize="+settings.garbagePhp_chunkSize,!0),xhr[i].send()}}.bind(this),1+delay)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);dlStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_dl&&dlStatus>0||failed)&&((failed||isNaN(dlStatus))&&(dlStatus="Fail"),clearRequests(),clearInterval(interval),tlog("dlTest finished "+dlStatus),done())}else t>1e3*settings.time_dlGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function ulTest(done){if(tlog("ulTest"),!ulCalled){ulCalled=!0;var totLoaded=0,startT=(new Date).getTime(),graceTimeDone=!1,failed=!1;xhr=[];for(var testStream=function(i,delay){setTimeout(function(){if(3===testStatus){tlog("ul test stream started "+i+" "+delay);var prevLoaded=0,x=new XMLHttpRequest;xhr[i]=x;var ie11workaround;if(settings.forceIE11Workaround)ie11workaround=!0;else try{xhr[i].upload.onprogress,ie11workaround=!1}catch(e){ie11workaround=!0}ie11workaround?(xhr[i].onload=function(){tlog("ul stream progress event (ie11wa)"),totLoaded+=262144,testStream(i,0)},xhr[i].onerror=function(){tlog("ul stream failed (ie11wa)"),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)},xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(reqsmall)):(xhr[i].upload.onprogress=function(event){if(tlog("ul stream progress event "+i+" "+event.loaded),3!==testStatus)try{x.abort()}catch(e){}var loadDiff=event.loaded<=0?0:event.loaded-prevLoaded;isNaN(loadDiff)||!isFinite(loadDiff)||0>loadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].upload.onload=function(){tlog("ul stream finished "+i),testStream(i,0)}.bind(this),xhr[i].upload.onerror=function(){tlog("ul stream failed "+i),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this),xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(req))}}.bind(this),1)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);ulStatus=(8*speed*settings.overheadCompensationFactor/1048576).toFixed(2),(t/1e3>settings.time_ul&&ulStatus>0||failed)&&((failed||isNaN(ulStatus))&&(ulStatus="Fail"),clearRequests(),clearInterval(interval),tlog("ulTest finished "+ulStatus),done())}else t>1e3*settings.time_ulGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function pingTest(done){if(tlog("pingTest"),!ptCalled){ptCalled=!0;var prevT=null,ping=0,jitter=0,i=0,prevInstspd=0;xhr=[];var doPing=function(){tlog("ping"),prevT=(new Date).getTime(),xhr[0]=new XMLHttpRequest,xhr[0].onload=function(){if(tlog("pong"),0===i)prevT=(new Date).getTime();else{var instspd=(new Date).getTime()-prevT,instjitter=Math.abs(instspd-prevInstspd);1===i?ping=instspd:(ping=.9*ping+.1*instspd,jitter=instjitter>jitter?.2*jitter+.8*instjitter:.9*jitter+.1*instjitter),prevInstspd=instspd}pingStatus=ping.toFixed(2),jitterStatus=jitter.toFixed(2),i++,tlog("PING: "+pingStatus+" JITTER: "+jitterStatus),i1?log:""),xhr.send(fd)}catch(ex){var postData="dl="+encodeURIComponent(dlStatus)+"&ul="+encodeURIComponent(ulStatus)+"&ping="+encodeURIComponent(pingStatus)+"&jitter="+encodeURIComponent(jitterStatus)+"&log="+encodeURIComponent(settings.telemetry_level>1?log:"");xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),xhr.send(postData)}}}var testStatus=0,dlStatus="",ulStatus="",pingStatus="",jitterStatus="",clientIp="",log="",settings={test_order:"ID_U_P",time_ul:15,time_dl:15,time_ulGraceTime:3,time_dlGraceTime:1.5,count_ping:35,url_dl:"garbage.php",url_ul:"empty.php",url_ping:"empty.php",url_getIp:"getIP.php",xhr_dlMultistream:10,xhr_ulMultistream:3,xhr_ignoreErrors:1,xhr_dlUseBlob:!1,garbagePhp_chunkSize:20,enable_quirks:!0,overheadCompensationFactor:1048576/925e3,telemetry_level:0,url_telemetry:"telemetry.php"},xhr=null,interval=null,test_pointer=0;this.addEventListener("message",function(e){var params=e.data.split(" ");if("status"===params[0]&&postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus+";"+clientIp+";"+jitterStatus),"start"===params[0]&&0===testStatus){testStatus=1;try{var s={};try{var ss=e.data.substring(5);ss&&(s=JSON.parse(ss))}catch(e){twarn("Error parsing custom settings JSON. Please check your syntax")}for(var key in s)"undefined"!=typeof settings[key]?settings[key]=s[key]:twarn("Unknown setting ignored: "+key);if(settings.enable_quirks||"undefined"!=typeof s.enable_quirks&&s.enable_quirks){var ua=navigator.userAgent;/Firefox.(\d+\.\d+)/i.test(ua)&&("undefined"==typeof s.xhr_ulMultistream&&(settings.xhr_ulMultistream=1),"undefined"==typeof s.test_order&&(settings.test_order="ID_P_U")),/Edge.(\d+\.\d+)/i.test(ua)&&("undefined"==typeof s.xhr_dlMultistream&&(settings.xhr_dlMultistream=3),(/Edge\/15.(\d+)/i.test(ua)||/Edge\/16.(\d+)/i.test(ua))&&(settings.forceIE11Workaround=!0)),/Chrome.(\d+)/i.test(ua)&&self.fetch&&"undefined"==typeof s.xhr_dlMultistream&&(settings.xhr_dlMultistream=5)}"undefined"!=typeof s.telemetry_level&&(settings.telemetry_level="basic"===s.telemetry_level?1:"full"===s.telemetry_level?2:0),settings.test_order=settings.test_order.toUpperCase()}catch(e){twarn("Possible error in custom test settings. Some settings may not be applied. Exception: "+e)}tlog(JSON.stringify(settings)),test_pointer=0;var runNextTest=function(){if(test_pointer>=settings.test_order.length)return testStatus=4,void sendTelemetry();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++,setTimeout(runNextTest,1e3);break;default:test_pointer++}};runNextTest()}"abort"===params[0]&&(tlog("manually aborted"),clearRequests(),runNextTest=null,interval&&clearInterval(interval),settings.telemetry_level>1&&sendTelemetry(),testStatus=5,dlStatus="",ulStatus="",pingStatus="",jitterStatus="")});var ipCalled=!1,dlCalled=!1,r=new ArrayBuffer(1048576);try{r=new Float32Array(r);for(var i=0;ii;i++)req.push(r);req=new Blob(req),r=new ArrayBuffer(262144);try{r=new Float32Array(r);for(var i=0;iloadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].onload=function(){tlog("dl stream finished "+i);try{xhr[i].abort()}catch(e){}testStream(i,0)}.bind(this),xhr[i].onerror=function(){tlog("dl stream failed "+i),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this);try{settings.xhr_dlUseBlob?xhr[i].responseType="blob":xhr[i].responseType="arraybuffer"}catch(e){}xhr[i].open("GET",settings.url_dl+url_sep(settings.url_dl)+"r="+Math.random()+"&ckSize="+settings.garbagePhp_chunkSize,!0),xhr[i].send()}}.bind(this),1+delay)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);dlStatus=(8*speed*settings.overheadCompensationFactor/(settings.useMebibits?1048576:1e6)).toFixed(2),(t/1e3>settings.time_dl&&dlStatus>0||failed)&&((failed||isNaN(dlStatus))&&(dlStatus="Fail"),clearRequests(),clearInterval(interval),dlProgress=1,tlog("dlTest finished "+dlStatus),done())}else t>1e3*settings.time_dlGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function ulTest(done){if(tlog("ulTest"),!ulCalled){ulCalled=!0;var totLoaded=0,startT=(new Date).getTime(),graceTimeDone=!1,failed=!1;xhr=[];for(var testStream=function(i,delay){setTimeout(function(){if(3===testStatus){tlog("ul test stream started "+i+" "+delay);var prevLoaded=0,x=new XMLHttpRequest;xhr[i]=x;var ie11workaround;if(settings.forceIE11Workaround)ie11workaround=!0;else try{xhr[i].upload.onprogress,ie11workaround=!1}catch(e){ie11workaround=!0}ie11workaround?(xhr[i].onload=function(){tlog("ul stream progress event (ie11wa)"),totLoaded+=262144,testStream(i,0)},xhr[i].onerror=function(){tlog("ul stream failed (ie11wa)"),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)},xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(reqsmall)):(xhr[i].upload.onprogress=function(event){if(tlog("ul stream progress event "+i+" "+event.loaded),3!==testStatus)try{x.abort()}catch(e){}var loadDiff=event.loaded<=0?0:event.loaded-prevLoaded;isNaN(loadDiff)||!isFinite(loadDiff)||0>loadDiff||(totLoaded+=loadDiff,prevLoaded=event.loaded)}.bind(this),xhr[i].upload.onload=function(){tlog("ul stream finished "+i),testStream(i,0)}.bind(this),xhr[i].upload.onerror=function(){tlog("ul stream failed "+i),0===settings.xhr_ignoreErrors&&(failed=!0);try{xhr[i].abort()}catch(e){}delete xhr[i],1===settings.xhr_ignoreErrors&&testStream(i,100)}.bind(this),xhr[i].open("POST",settings.url_ul+url_sep(settings.url_ul)+"r="+Math.random(),!0),xhr[i].setRequestHeader("Content-Encoding","identity"),xhr[i].send(req))}}.bind(this),1)}.bind(this),i=0;it))if(graceTimeDone){var speed=totLoaded/(t/1e3);ulStatus=(8*speed*settings.overheadCompensationFactor/(settings.useMebibits?1048576:1e6)).toFixed(2),(t/1e3>settings.time_ul&&ulStatus>0||failed)&&((failed||isNaN(ulStatus))&&(ulStatus="Fail"),clearRequests(),clearInterval(interval),ulProgress=1,tlog("ulTest finished "+ulStatus),done())}else t>1e3*settings.time_ulGraceTime&&(totLoaded>0&&(startT=(new Date).getTime(),totLoaded=0),graceTimeDone=!0)}.bind(this),200)}}function pingTest(done){if(tlog("pingTest"),!ptCalled){ptCalled=!0;var prevT=null,ping=0,jitter=0,i=0,prevInstspd=0;xhr=[];var doPing=function(){tlog("ping"),pingProgress=i/settings.count_ping,prevT=(new Date).getTime(),xhr[0]=new XMLHttpRequest,xhr[0].onload=function(){if(tlog("pong"),0===i)prevT=(new Date).getTime();else{var instspd=(new Date).getTime()-prevT;if(settings.ping_allowPerformanceApi)try{var p=performance.getEntries();p=p[p.length-1];var d=p.responseStart-p.requestStart;0>=d&&(d=p.duration),d>0&&instspd>d&&(instspd=d)}catch(e){tlog("Performance API not supported, using estimate")}var instjitter=Math.abs(instspd-prevInstspd);1===i?ping=instspd:(ping=.9*ping+.1*instspd,jitter=instjitter>jitter?.2*jitter+.8*instjitter:.9*jitter+.1*instjitter),prevInstspd=instspd}pingStatus=ping.toFixed(2),jitterStatus=jitter.toFixed(2),i++,tlog("PING: "+pingStatus+" JITTER: "+jitterStatus),i1?log:""),xhr.send(fd)}catch(ex){var postData="dl="+encodeURIComponent(dlStatus)+"&ul="+encodeURIComponent(ulStatus)+"&ping="+encodeURIComponent(pingStatus)+"&jitter="+encodeURIComponent(jitterStatus)+"&log="+encodeURIComponent(settings.telemetry_level>1?log:"");xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"),xhr.send(postData)}}}var testStatus=0,dlStatus="",ulStatus="",pingStatus="",jitterStatus="",clientIp="",dlProgress=0,ulProgress=0,pingProgress=0,log="",settings={test_order:"ID_U_P",time_ul:15,time_dl:15,time_ulGraceTime:3,time_dlGraceTime:1.5,count_ping:35,url_dl:"garbage.php",url_ul:"empty.php",url_ping:"empty.php",url_getIp:"getIP.php",xhr_dlMultistream:10,xhr_ulMultistream:3,xhr_ignoreErrors:1,xhr_dlUseBlob:!1,garbagePhp_chunkSize:20,enable_quirks:!0,ping_allowPerformanceApi:!0,overheadCompensationFactor:1.06,useMebibits:!1,telemetry_level:0,url_telemetry:"telemetry.php"},xhr=null,interval=null,test_pointer=0;this.addEventListener("message",function(e){var params=e.data.split(" ");if("status"===params[0]&&postMessage(testStatus+";"+dlStatus+";"+ulStatus+";"+pingStatus+";"+clientIp+";"+jitterStatus+";"+dlProgress+";"+ulProgress+";"+pingProgress),"start"===params[0]&&0===testStatus){testStatus=1;try{var s={};try{var ss=e.data.substring(5);ss&&(s=JSON.parse(ss))}catch(e){twarn("Error parsing custom settings JSON. Please check your syntax")}for(var key in s)"undefined"!=typeof settings[key]?settings[key]=s[key]:twarn("Unknown setting ignored: "+key);if(settings.enable_quirks||"undefined"!=typeof s.enable_quirks&&s.enable_quirks){var ua=navigator.userAgent;/Firefox.(\d+\.\d+)/i.test(ua)&&("undefined"==typeof s.xhr_ulMultistream&&(settings.xhr_ulMultistream=1),"undefined"==typeof s.test_order&&(settings.test_order="ID_P_U")),/Edge.(\d+\.\d+)/i.test(ua)&&("undefined"==typeof s.xhr_dlMultistream&&(settings.xhr_dlMultistream=3),settings.forceIE11Workaround=!0),/Chrome.(\d+)/i.test(ua)&&self.fetch&&"undefined"==typeof s.xhr_dlMultistream&&(settings.xhr_dlMultistream=5)}"undefined"!=typeof s.telemetry_level&&(settings.telemetry_level="basic"===s.telemetry_level?1:"full"===s.telemetry_level?2:0),settings.test_order=settings.test_order.toUpperCase()}catch(e){twarn("Possible error in custom test settings. Some settings may not be applied. Exception: "+e)}tlog(JSON.stringify(settings)),test_pointer=0;var iRun=!1,dRun=!1,uRun=!1,pRun=!1,runNextTest=function(){if(test_pointer>=settings.test_order.length)return testStatus=4,void sendTelemetry();switch(settings.test_order.charAt(test_pointer)){case"I":if(test_pointer++,iRun)return void runNextTest();iRun=!0,getIp(runNextTest);break;case"D":if(test_pointer++,dRun)return void runNextTest();dRun=!0,testStatus=1,dlTest(runNextTest);break;case"U":if(test_pointer++,uRun)return void runNextTest();uRun=!0,testStatus=3,ulTest(runNextTest);break;case"P":if(test_pointer++,pRun)return void runNextTest();pRun=!0,testStatus=2,pingTest(runNextTest);break;case"_":test_pointer++,setTimeout(runNextTest,1e3);break;default:test_pointer++}};runNextTest()}"abort"===params[0]&&(tlog("manually aborted"),clearRequests(),runNextTest=null,interval&&clearInterval(interval),settings.telemetry_level>1&&sendTelemetry(),testStatus=5,dlStatus="",ulStatus="",pingStatus="",jitterStatus="")});var ipCalled=!1,dlCalled=!1,r=new ArrayBuffer(1048576);try{r=new Float32Array(r);for(var i=0;ii;i++)req.push(r);req=new Blob(req),r=new ArrayBuffer(262144);try{r=new Float32Array(r);for(var i=0;i