Skip to content

Instantly share code, notes, and snippets.

@tai2
Created October 11, 2018 14:17
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 tai2/d4010a42e255de6f9c695c2c20e131c9 to your computer and use it in GitHub Desktop.
Save tai2/d4010a42e255de6f9c695c2c20e131c9 to your computer and use it in GitHub Desktop.
measure local network speed
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<h1>SPEED TEST</h2>
<div id="results">
</div>
</body>
<script>
function measure(suffix, callback) {
const results = document.getElementById('results')
const row = document.createElement('div')
const progress = document.createElement('span')
const result = document.createElement('span')
row.appendChild(progress)
row.appendChild(result)
results.appendChild(row)
const t0 = performance.now()
const xhr = new XMLHttpRequest()
xhr.addEventListener('load', (ev) => {
const t1 = performance.now()
const d = (t1 - t0) / 1000
const bps = 0.001 * 0.001 * ev.total / d
result.innerHTML = ` ${d.toFixed(2)} sec ${bps.toFixed(1)} Mbps`
callback(null)
})
xhr.addEventListener('progress', (ev) => {
const p = 100 * ev.loaded / ev.total
progress.innerHTML = `${p.toFixed(1)}%`
})
xhr.addEventListener('error', (ev) => {
callback(ev)
})
xhr.open('GET', `100mb.bin?${suffix}`)
xhr.send()
}
let count = 0
function iterateMeasure() {
measure(count++, error => {
if (!error) {
iterateMeasure()
}
})
}
iterateMeasure()
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment