speedtest_worker.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. HTML5 Speedtest v4.2.3
  3. by Federico Dossena
  4. https://github.com/adolfintel/speedtest/
  5. GNU LGPLv3 License
  6. */
  7. // data reported to main thread
  8. var testStatus = 0 // 0=not started, 1=download test, 2=ping+jitter test, 3=upload test, 4=finished, 5=abort/error
  9. var dlStatus = '' // download speed in megabit/s with 2 decimal digits
  10. var ulStatus = '' // upload speed in megabit/s with 2 decimal digits
  11. var pingStatus = '' // ping in milliseconds with 2 decimal digits
  12. var jitterStatus = '' // jitter in milliseconds with 2 decimal digits
  13. var clientIp = '' // client's IP address as reported by getIP.php
  14. // test settings. can be overridden by sending specific values with the start command
  15. var settings = {
  16. time_ul: 15, // duration of upload test in seconds
  17. time_dl: 15, // duration of download test in seconds
  18. count_ping: 35, // number of pings to perform in ping test
  19. url_dl: 'garbage.php', // path to a large file or garbage.php, used for download test. must be relative to this js file
  20. url_ul: 'empty.php', // path to an empty file, used for upload test. must be relative to this js file
  21. url_ping: 'empty.php', // path to an empty file, used for ping test. must be relative to this js file
  22. url_getIp: 'getIP.php', // path to getIP.php relative to this js file, or a similar thing that outputs the client's ip
  23. xhr_dlMultistream: 10, // number of download streams to use (can be different if enable_quirks is active)
  24. xhr_ulMultistream: 3, // number of upload streams to use (can be different if enable_quirks is active)
  25. xhr_ignoreErrors: 1, // 0=fail on errors, 1=attempt to restart a stream if it fails, 2=ignore all errors
  26. 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)
  27. garbagePhp_chunkSize: 20, // size of chunks sent by garbage.php (can be different if enable_quirks is active)
  28. 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
  29. allow_fetchAPI: false, // enables Fetch API. currently disabled because it leaks memory like no tomorrow
  30. force_fetchAPI: false // when Fetch API is enabled, it will force usage on every browser that supports it
  31. }
  32. var xhr = null // array of currently active xhr requests
  33. var interval = null // timer used in tests
  34. /*
  35. when set to true (automatically) the download test will use the fetch api instead of xhr.
  36. fetch api is used if
  37. -allow_fetchAPI is true AND
  38. -(we're on chrome that supports fetch api AND enable_quirks is true) OR (we're on any browser that supports fetch api AND force_fetchAPI is true)
  39. */
  40. var useFetchAPI = false
  41. /*
  42. listener for commands from main thread to this worker.
  43. commands:
  44. -status: returns the current status as a string of values spearated by a semicolon (;) in this order: testStatus;dlStatus;ulStatus;pingStatus;clientIp;jitterStatus
  45. -abort: aborts the current test
  46. -start: starts the test. optionally, settings can be passed as JSON.
  47. example: start {"time_ul":"10", "time_dl":"10", "count_ping":"50"}
  48. */
  49. this.addEventListener('message', function (e) {
  50. var params = e.data.split(' ')
  51. if (params[0] === 'status') { // return status
  52. postMessage(testStatus + ';' + dlStatus + ';' + ulStatus + ';' + pingStatus + ';' + clientIp + ';' + jitterStatus)
  53. }
  54. if (params[0] === 'start' && testStatus === 0) { // start new test
  55. testStatus = 1
  56. try {
  57. // parse settings, if present
  58. var s = JSON.parse(e.data.substring(5))
  59. if (typeof s.url_dl !== 'undefined') settings.url_dl = s.url_dl // download url
  60. if (typeof s.url_ul !== 'undefined') settings.url_ul = s.url_ul // upload url
  61. if (typeof s.url_ping !== 'undefined') settings.url_ping = s.url_ping // ping url
  62. if (typeof s.url_getIp !== 'undefined') settings.url_getIp = s.url_getIp // url to getIP.php
  63. if (typeof s.time_dl !== 'undefined') settings.time_dl = s.time_dl // duration of download test
  64. if (typeof s.time_ul !== 'undefined') settings.time_ul = s.time_ul // duration of upload test
  65. if (typeof s.enable_quirks !== 'undefined') settings.enable_quirks = s.enable_quirks // enable quirks or not
  66. if (typeof s.allow_fetchAPI !== 'undefined') settings.allow_fetchAPI = s.allow_fetchAPI // allows fetch api to be used if supported
  67. // quirks for specific browsers. more may be added in future releases
  68. if (settings.enable_quirks) {
  69. var ua = navigator.userAgent
  70. if (/Firefox.(\d+\.\d+)/i.test(ua)) {
  71. // ff more precise with 1 upload stream
  72. settings.xhr_ulMultistream = 1
  73. }
  74. if (/Edge.(\d+\.\d+)/i.test(ua)) {
  75. // edge more precise with 3 download streams
  76. settings.xhr_dlMultistream = 3
  77. }
  78. if ((/Safari.(\d+)/i.test(ua)) && !(/Chrome.(\d+)/i.test(ua))) {
  79. // safari more precise with 10 upload streams and 5mb chunks for download test
  80. settings.xhr_ulMultistream = 10
  81. settings.garbagePhp_chunkSize = 5
  82. }
  83. if (/Chrome.(\d+)/i.test(ua) && (!!self.fetch)) {
  84. // chrome can't handle large xhr very well, use fetch api if available and allowed
  85. if (settings.allow_fetchAPI) useFetchAPI = true
  86. // chrome more precise with 5 streams
  87. settings.xhr_dlMultistream = 5
  88. }
  89. }
  90. if (typeof s.count_ping !== 'undefined') settings.count_ping = s.count_ping // number of pings for ping test
  91. if (typeof s.xhr_dlMultistream !== 'undefined') settings.xhr_dlMultistream = s.xhr_dlMultistream // number of download streams
  92. if (typeof s.xhr_ulMultistream !== 'undefined') settings.xhr_ulMultistream = s.xhr_ulMultistream // number of upload streams
  93. if (typeof s.xhr_dlUseBlob !== 'undefined') settings.xhr_dlUseBlob = s.xhr_dlUseBlob // use blob for download test
  94. if (typeof s.garbagePhp_chunkSize !== 'undefined') settings.garbagePhp_chunkSize = s.garbagePhp_chunkSize // size of garbage.php chunks
  95. if (typeof s.force_fetchAPI !== 'undefined') settings.force_fetchAPI = s.force_fetchAPI // use fetch api on all browsers that support it if enabled
  96. if (settings.allow_fetchAPI && settings.force_fetchAPI && (!!self.fetch)) useFetchAPI = true
  97. } catch (e) { }
  98. // run the tests
  99. console.log(settings)
  100. console.log('Fetch API: ' + useFetchAPI)
  101. getIp(function () { dlTest(function () { testStatus = 2; pingTest(function () { testStatus = 3; ulTest(function () { testStatus = 4 }) }) }) })
  102. }
  103. if (params[0] === 'abort') { // abort command
  104. clearRequests() // stop all xhr activity
  105. if (interval) clearInterval(interval) // clear timer if present
  106. testStatus = 5; dlStatus = ''; ulStatus = ''; pingStatus = ''; jitterStatus = '' // set test as aborted
  107. }
  108. })
  109. // stops all XHR activity, aggressively
  110. function clearRequests () {
  111. if (xhr) {
  112. for (var i = 0; i < xhr.length; i++) {
  113. if (useFetchAPI) try { xhr[i].cancelRequested = true } catch (e) { }
  114. try { xhr[i].onprogress = null; xhr[i].onload = null; xhr[i].onerror = null } catch (e) { }
  115. try { xhr[i].upload.onprogress = null; xhr[i].upload.onload = null; xhr[i].upload.onerror = null } catch (e) { }
  116. try { xhr[i].abort() } catch (e) { }
  117. try { delete (xhr[i]) } catch (e) { }
  118. }
  119. xhr = null
  120. }
  121. }
  122. // gets client's IP using url_getIp, then calls the done function
  123. function getIp (done) {
  124. xhr = new XMLHttpRequest()
  125. xhr.onload = function () {
  126. clientIp = xhr.responseText
  127. done()
  128. }
  129. xhr.onerror = function () {
  130. done()
  131. }
  132. xhr.open('GET', settings.url_getIp + '?r=' + Math.random(), true)
  133. xhr.send()
  134. }
  135. // download test, calls done function when it's over
  136. var dlCalled = false // used to prevent multiple accidental calls to dlTest
  137. function dlTest (done) {
  138. if (dlCalled) return; else dlCalled = true // dlTest already called?
  139. var totLoaded = 0.0, // total number of loaded bytes
  140. startT = new Date().getTime(), // timestamp when test was started
  141. failed = false // set to true if a stream fails
  142. xhr = []
  143. // function to create a download stream. streams are slightly delayed so that they will not end at the same time
  144. var testStream = function (i, delay) {
  145. setTimeout(function () {
  146. if (testStatus !== 1) return // delayed stream ended up starting after the end of the download test
  147. if (useFetchAPI) {
  148. xhr[i] = fetch(settings.url_dl + '?r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize).then(function (response) {
  149. var reader = response.body.getReader()
  150. var consume = function () {
  151. return reader.read().then(function (result) {
  152. if (result.done) testStream(i); else {
  153. totLoaded += result.value.length
  154. if (xhr[i].cancelRequested) reader.cancel()
  155. }
  156. return consume()
  157. }.bind(this))
  158. }.bind(this)
  159. return consume()
  160. }.bind(this))
  161. } else {
  162. var prevLoaded = 0 // number of bytes loaded last time onprogress was called
  163. var x = new XMLHttpRequest()
  164. xhr[i] = x
  165. xhr[i].onprogress = function (event) {
  166. if (testStatus !== 1) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the download test
  167. // progress event, add number of new loaded bytes to totLoaded
  168. var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded)
  169. if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case
  170. totLoaded += loadDiff
  171. prevLoaded = event.loaded
  172. }.bind(this)
  173. xhr[i].onload = function () {
  174. // the large file has been loaded entirely, start again
  175. try { xhr[i].abort() } catch (e) { } // reset the stream data to empty ram
  176. testStream(i, 0)
  177. }.bind(this)
  178. xhr[i].onerror = function () {
  179. // error
  180. if (settings.xhr_ignoreErrors === 0) failed=true //abort
  181. try { xhr[i].abort() } catch (e) { }
  182. delete (xhr[i])
  183. if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms
  184. }.bind(this)
  185. // send xhr
  186. try { if (settings.xhr_dlUseBlob) xhr[i].responseType = 'blob'; else xhr[i].responseType = 'arraybuffer' } catch (e) { }
  187. xhr[i].open('GET', settings.url_dl + '?r=' + Math.random() + '&ckSize=' + settings.garbagePhp_chunkSize, true) // random string to prevent caching
  188. xhr[i].send()
  189. }
  190. }.bind(this), 1 + delay)
  191. }.bind(this)
  192. // open streams
  193. for (var i = 0; i < settings.xhr_dlMultistream; i++) {
  194. testStream(i, 100 * i)
  195. }
  196. // every 200ms, update dlStatus
  197. interval = setInterval(function () {
  198. var t = new Date().getTime() - startT
  199. if (t < 200) return
  200. var speed = totLoaded / (t / 1000.0)
  201. dlStatus = ((speed * 8) / 925000.0).toFixed(2) // 925000 instead of 1048576 to account for overhead
  202. if ((t / 1000.0) > settings.time_dl || failed) { // test is over, stop streams and timer
  203. if (failed || isNaN(dlStatus)) dlStatus = 'Fail'
  204. clearRequests()
  205. clearInterval(interval)
  206. done()
  207. }
  208. }.bind(this), 200)
  209. }
  210. // upload test, calls done function whent it's over
  211. // garbage data for upload test
  212. var r = new ArrayBuffer(1048576)
  213. try { r = new Float32Array(r); for (var i = 0; i < r.length; i++)r[i] = Math.random() } catch (e) { }
  214. var req = []
  215. var reqsmall = []
  216. for (var i = 0; i < 20; i++) req.push(r)
  217. req = new Blob(req)
  218. r = new ArrayBuffer(262144)
  219. try { r = new Float32Array(r); for (var i = 0; i < r.length; i++)r[i] = Math.random() } catch (e) { }
  220. reqsmall.push(r)
  221. reqsmall = new Blob(reqsmall)
  222. var ulCalled = false // used to prevent multiple accidental calls to ulTest
  223. function ulTest (done) {
  224. if (ulCalled) return; else ulCalled = true // ulTest already called?
  225. var totLoaded = 0.0 // total number of transmitted bytes
  226. var startT = new Date().getTime() // timestamp when test was started
  227. var failed = false // set to true if a stream fails
  228. xhr = []
  229. // function to create an upload stream. streams are slightly delayed so that they will not end at the same time
  230. var testStream = function (i, delay) {
  231. setTimeout(function () {
  232. if (testStatus !== 3) return // delayed stream ended up starting after the end of the upload test
  233. var prevLoaded = 0 // number of bytes transmitted last time onprogress was called
  234. var x = new XMLHttpRequest()
  235. xhr[i] = x
  236. var ie11workaround
  237. try {
  238. xhr[i].upload.onprogress
  239. ie11workaround = false
  240. } catch (e) {
  241. ie11workaround = true
  242. }
  243. if (ie11workaround) {
  244. // IE11 workarond: xhr.upload does not work properly, therefore we send a bunch of small 256k requests and use the onload event as progress. This is not precise, especially on fast connections
  245. xhr[i].onload = function () {
  246. totLoaded += 262144
  247. testStream(i, 0)
  248. }
  249. xhr[i].onerror = function () {
  250. // error, abort
  251. if (settings.xhr_ignoreErrors === 0) failed = true //abort
  252. try { xhr[i].abort() } catch (e) { }
  253. delete (xhr[i])
  254. if (settings.xhr_ignoreErrors === 1) testStatus(i,100); //restart stream after 100ms
  255. }
  256. xhr[i].open('POST', settings.url_ul + '?r=' + Math.random(), true) // random string to prevent caching
  257. xhr[i].setRequestHeader('Content-Encoding', 'identity') // disable compression (some browsers may refuse it, but data is incompressible anyway)
  258. xhr[i].send(reqsmall)
  259. } else {
  260. // REGULAR version, no workaround
  261. xhr[i].upload.onprogress = function (event) {
  262. if (testStatus !== 3) { try { x.abort() } catch (e) { } } // just in case this XHR is still running after the upload test
  263. // progress event, add number of new loaded bytes to totLoaded
  264. var loadDiff = event.loaded <= 0 ? 0 : (event.loaded - prevLoaded)
  265. if (isNaN(loadDiff) || !isFinite(loadDiff) || loadDiff < 0) return // just in case
  266. totLoaded += loadDiff
  267. prevLoaded = event.loaded
  268. }.bind(this)
  269. xhr[i].upload.onload = function () {
  270. // this stream sent all the garbage data, start again
  271. testStream(i, 0)
  272. }.bind(this)
  273. xhr[i].upload.onerror = function () {
  274. if (settings.xhr_ignoreErrors === 0) failed=true //abort
  275. try { xhr[i].abort() } catch (e) { }
  276. delete (xhr[i])
  277. if (settings.xhr_ignoreErrors === 1) testStream(i, 100) //restart stream after 100ms
  278. }.bind(this)
  279. // send xhr
  280. xhr[i].open('POST', settings.url_ul + '?r=' + Math.random(), true) // random string to prevent caching
  281. xhr[i].setRequestHeader('Content-Encoding', 'identity') // disable compression (some browsers may refuse it, but data is incompressible anyway)
  282. xhr[i].send(req)
  283. }
  284. }.bind(this), 1)
  285. }.bind(this)
  286. // open streams
  287. for (var i = 0; i < settings.xhr_ulMultistream; i++) {
  288. testStream(i, 100 * i)
  289. }
  290. // every 200ms, update ulStatus
  291. interval = setInterval(function () {
  292. var t = new Date().getTime() - startT
  293. if (t < 200) return
  294. var speed = totLoaded / (t / 1000.0)
  295. ulStatus = ((speed * 8) / 925000.0).toFixed(2) // 925000 instead of 1048576 to account for overhead
  296. if ((t / 1000.0) > settings.time_ul || failed) { // test is over, stop streams and timer
  297. if (failed || isNaN(ulStatus)) ulStatus = 'Fail'
  298. clearRequests()
  299. clearInterval(interval)
  300. done()
  301. }
  302. }.bind(this), 200)
  303. }
  304. // ping+jitter test, function done is called when it's over
  305. var ptCalled = false // used to prevent multiple accidental calls to pingTest
  306. function pingTest (done) {
  307. if (ptCalled) return; else ptCalled = true // pingTest already called?
  308. var prevT = null // last time a pong was received
  309. var ping = 0.0 // current ping value
  310. var jitter = 0.0 // current jitter value
  311. var i = 0 // counter of pongs received
  312. var prevInstspd = 0 // last ping time, used for jitter calculation
  313. xhr = []
  314. // ping function
  315. var doPing = function () {
  316. prevT = new Date().getTime()
  317. xhr[0] = new XMLHttpRequest()
  318. xhr[0].onload = function () {
  319. // pong
  320. if (i === 0) {
  321. prevT = new Date().getTime() // first pong
  322. } else {
  323. var instspd = (new Date().getTime() - prevT)
  324. var instjitter = Math.abs(instspd - prevInstspd)
  325. if (i === 1) ping = instspd; /* first ping, can't tell jiutter yet*/ else {
  326. ping = ping * 0.9 + instspd * 0.1 // ping, weighted average
  327. jitter = instjitter > jitter ? (jitter * 0.2 + instjitter * 0.8) : (jitter * 0.9 + instjitter * 0.1) // update jitter, weighted average. spikes in ping values are given more weight.
  328. }
  329. prevInstspd = instspd
  330. }
  331. pingStatus = ping.toFixed(2)
  332. jitterStatus = jitter.toFixed(2)
  333. i++
  334. if (i < settings.count_ping) doPing(); else done() // more pings to do?
  335. }.bind(this)
  336. xhr[0].onerror = function () {
  337. // a ping failed, cancel test
  338. if (settings.xhr_ignoreErrors === 0) { //abort
  339. pingStatus = 'Fail'
  340. jitterStatus = 'Fail'
  341. clearRequests()
  342. done()
  343. }
  344. if (settings.xhr_ignoreErrors === 1) doPing() //retry ping
  345. if(settings.xhr_ignoreErrors === 2){ //ignore failed ping
  346. i++
  347. if (i < settings.count_ping) doPing(); else done() // more pings to do?
  348. }
  349. }.bind(this)
  350. // sent xhr
  351. xhr[0].open('GET', settings.url_ping + '?r=' + Math.random(), true) // random string to prevent caching
  352. xhr[0].send()
  353. }.bind(this)
  354. doPing() // start first ping
  355. }