Skip to content

Instantly share code, notes, and snippets.

@tomvangoethem
Created June 2, 2016 23:40
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 tomvangoethem/eaf9312401ab6098282eb7631bff8547 to your computer and use it in GitHub Desktop.
Save tomvangoethem/eaf9312401ab6098282eb7631bff8547 to your computer and use it in GitHub Desktop.
"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