Skip to content

Instantly share code, notes, and snippets.

@voltrevo
Created August 3, 2015 04:22
Show Gist options
  • Save voltrevo/90df48721b7d07099c91 to your computer and use it in GitHub Desktop.
Save voltrevo/90df48721b7d07099c91 to your computer and use it in GitHub Desktop.
Simulate stress on javascript execution by spin locking intermittently
'use strict';
module.exports = function stress(interDelay, spinDelay, duration) {
var spinLock = function(spinDuration) {
var start = Date.now();
while (Date.now() - start < spinDuration) {
// Do nothing
}
};
var approximate = function(x) {
return (1.0 + 0.75 * (2 * Math.random() - 1)) * x;
};
var active = true;
setTimeout(function() {
active = false;
}, duration);
var loop = function() {
if (!active) {
return;
}
setTimeout(function() {
spinLock(approximate(spinDelay));
loop();
}, approximate(interDelay));
}
loop();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment