Skip to content

Instantly share code, notes, and snippets.

@dtex
Last active July 8, 2018 18:55
Show Gist options
  • Save dtex/8d24cbd5608c1bcb6b62456bf965ee61 to your computer and use it in GitHub Desktop.
Save dtex/8d24cbd5608c1bcb6b62456bf965ee61 to your computer and use it in GitHub Desktop.
Tests the performance of temporal using different delays and resolutions.
let temporal = require("./../lib/temporal.js");
let hrTime = function() {
let hrtime = process.hrtime();
return Math.floor((hrtime[0] * 1e9 + hrtime[1]));
};
function standardDeviation(values){
var avg = average(values);
var squareDiffs = values.map(function(value){
var diff = value - avg;
var sqrDiff = diff * diff;
return sqrDiff;
});
var avgSquareDiff = average(squareDiffs);
var stdDev = Math.sqrt(avgSquareDiff);
return stdDev;
}
function average(data){
var sum = data.reduce(function(sum, value){
return sum + value;
}, 0);
var avg = sum / data.length;
return avg;
}
let resolution = 1.0;
let tests = {
"0.01": {
"times": [0.01, 0.05, 0.07, 0.1, 0.3, 0.33, 0.5, 0.7, 0.77, 1, 1.5, 3.3, 3.33, 10, 100, 333, 333.3, 333.33, 1000]
},
"0.1": {
"times": [0.1, 0.3, 0.5, 0.7, 1, 1.5, 3.3, 10, 100, 333, 333.3, 1000]
},
"1.0": {
"times": [1, 10, 100, 333, 1000]
}
};
function delay (res, time, i) {
return new Promise(resolve => {
let start = hrTime();
temporal.delay(time, () => {
// Ignore the first few delays
if (i) {
let end = hrTime();
tests[res].results[time].starts.push(start);
tests[res].results[time].ends.push(end);
tests[res].results[time].expects.push(start + time * 1e6);
}
resolve();
});
});
}
async function delayRunner(res, time) {
console.log("Testing - Resolution: " + res + "ms, Delay: " + time +"ms");
return new Promise(async resolve => {
let iterate = [0,0,0,0,1,2,3,4,5,6,7,8,9,10];
for (const i of iterate) {
await delay(res, time, i);
}
for (i=0;i < tests[res].results[time].ends.length;i++) {
tests[res].results[time].diffs.push(tests[res].results[time].ends[i] - tests[res].results[time].expects[i]);
}
tests[res].results[time].standardDeviation = Math.floor(standardDeviation(tests[res].results[time].diffs));
resolve();
});
};
async function testRunner(res) {
for (const res of Object.keys(tests)) {
temporal.resolution(Number(res));
tests[res].results = {};
for (const time of tests[res].times) {
tests[res].results[time] = { starts: [], expects: [], ends: [], diffs: []};
await delayRunner(res, time);
}
};
Object.keys(tests).forEach( resolution => {
console.log("\n\nResolution: " + resolution);
console.log(tests[resolution].times);
tests[resolution].times.forEach(time => {
process.stdout.write(tests[resolution].results[time].standardDeviation + ",");
;
});
});
}
testRunner();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment