Created
August 3, 2015 04:22
-
-
Save voltrevo/90df48721b7d07099c91 to your computer and use it in GitHub Desktop.
Simulate stress on javascript execution by spin locking intermittently
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'; | |
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