Skip to content

Instantly share code, notes, and snippets.

@bassosimone
Created May 19, 2020 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bassosimone/168caa00207172361b6b26d815692368 to your computer and use it in GitHub Desktop.
Save bassosimone/168caa00207172361b6b26d815692368 to your computer and use it in GitHub Desktop.
<!doctype html>
<html lang='en'>
<head><meta charset='utf-8'><title>ndt7</title></head>
<body><script type='text/javascript'>
/* jshint esversion: 6, asi: true */
console.log('creating the downloader')
let downloader = new Worker('ndt7-minimal.js')
downloader.postMessage({
url: window.location.href,
subtest: 'download',
})
downloader.onmessage = function (ev) {
const maxsecs = 11
if (ev.data !== null && ev.data.elapsed < maxsecs) {
console.log(ev.data.elapsed + ' ' + ev.data.numBytes)
return
}
console.log('terminating the downloader')
// Implementation note: calling terminate() causes the JavaScript engine
// to destroy the worker. A side effect is that the WebSocket is also
// destroyed. Therefore, the TCP connection is reset. This makes the real
// download time close to the expected download time also in cases where
// we've queued lot of data and the download would be very long.
downloader.terminate()
console.log('creating the uploader')
let uploader = new Worker('ndt7-minimal.js')
uploader.postMessage({
url: window.location.href,
subtest: 'upload',
})
uploader.onmessage = function (ev) {
console.log('terminating the uploader')
// Not strictly necessary because we should be able to control the
// maximum duration of the upload already. Done for symmetry.
uploader.terminate()
}
}
</script></body></html>
/* jshint esversion: 6, asi: true, worker: true */
onmessage = function (ev) {
'use strict'
let url = new URL(ev.data.url)
url.protocol = 'wss'
const proto = 'net.measurementlab.ndt.v7'
if (ev.data.subtest === 'download') {
url.pathname = '/ndt/v7/download'
const sock = new WebSocket(url.toString(), proto)
sock.onclose = function () {
postMessage(null)
}
sock.onopen = function () {
const start = new Date().getTime()
let previous = start
let total = 0
sock.onmessage = function (ev) {
total += (ev.data instanceof Blob) ? ev.data.size : ev.data.length
let now = new Date().getTime()
const every = 250 // ms
if (now - previous > every) {
const elapsed = (now - start) / 1000
postMessage({
elapsed: elapsed,
numBytes: total,
})
previous = now
}
}
}
} else {
url.pathname = '/ndt/v7/upload'
const sock = new WebSocket(url.toString(), proto)
sock.onclose = function () {
console.log('# ' + new Date() + ' stop upload')
postMessage(null)
}
function loop(socket, data, start) {
let now = new Date().getTime()
const duration = 10000 // millisecond
if (now - start > duration) {
sock.close()
return
}
// TODO(bassosimone): refine to ensure this works well across a wide
// range of CPU speed/network speed/browser combinations
const underbuffered = 7 * data.length
while (sock.bufferedAmount < underbuffered) {
sock.send(data)
}
setTimeout(function() {
loop(sock, data, start)
}, 0)
}
sock.onopen = function () {
console.log('# ' + new Date() + ' start upload')
const data = new Uint8Array(1<<13)
sock.binarytype = 'arraybuffer'
loop(sock, data, new Date().getTime())
}
}
}
© 2020 GitHub, Inc.
@rahulchauhan9999
Copy link

Hi @bassosimone

I have use that code but have no idea how to '/ndt/v7/download' get that url.

I have run that code but showing WebSocket connection to 'wss://whatismywifispeed.com/ndt/v7/upload' failed: that error please help me how to resolved that issue am work on almost 2days but not get how it solved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment