Created
June 2, 2016 23:40
-
-
Save tomvangoethem/eaf9312401ab6098282eb7631bff8547 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const D = 20 * 1024; | |
const rMax = 100 * 1024; | |
const resourceSize = D + 500; | |
const observations = []; | |
const numRequests = 50; | |
const overhead = []; | |
function roundUp(value) { | |
return Math.ceil(value/D) * D; | |
} | |
function getRandom() { | |
return Math.floor(Math.random() * rMax); | |
} | |
for (let j = 0; j < 100000; j++) { | |
const measurements = []; | |
for (let i = 0; i < numRequests; i++) { | |
measurements.push(roundUp(resourceSize + getRandom())) | |
} | |
const ratio = measurements.filter(function(x) { | |
return x === roundUp(resourceSize); | |
}).length / measurements.length; | |
observations.push(roundUp(resourceSize) - ratio * rMax); | |
overhead.push.apply(overhead, measurements.map(function(x) {return x - resourceSize})); | |
} | |
const avgOff = observations.reduce(function(totalDiff, x) { | |
return totalDiff + Math.abs(x - resourceSize); | |
}, 0) / observations.length; | |
const avgOverhead = overhead.reduce(function(x, y){return x+y}) / overhead.length; | |
console.log('On average, the measurements using', numRequests, 'requests, is off by', avgOff.toFixed(2), 'bytes, for D =', D, ', rMax =', rMax); | |
console.log('Average "overhead":', avgOverhead.toFixed(2), "bytes"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment