diff --git a/tests/timer.js b/tests/timer.js index db26768..2bb83dc 100644 --- a/tests/timer.js +++ b/tests/timer.js @@ -2,26 +2,39 @@ var Timer = require('../metrics/timer'); var test = function(callback){ console.log("\nCreating a new timer, updating 10 times / sec for 30 secs"); - var timer = new Timer(); + var timer = new Timer() + , n = 0; + + var updateInterval = setInterval(function(){ + //simulates the duration that a task takes between 0 and 500 milliseconds + //And then update the timer for the duration of that time + var duration = Math.round(Math.random() * (500)); + timer.update(duration); + n += 1; + if (n % 10 == 0) { + process.stdout.write(".") + } + }, 100); + + setTimeout(function(){ + clearInterval(updateInterval); + console.log('Expected rate: 10/sec'); + console.log('count: ' + timer.count()); + console.log('min: ' + timer.min()); + console.log('max: ' + timer.max()); + console.log('mean: ' + timer.mean()); + console.log('stdDev: ' + timer.stdDev()); + console.log('percentiles: ' + JSON.stringify(timer.percentiles(), undefined, 2)); + console.log('oneMinuteRate: ' + timer.oneMinuteRate()); + console.log('fiveMinuteRate: ' + timer.fiveMinuteRate()); + console.log('fifteeenMinuteRate: ' + timer.fifteenMinuteRate()); + console.log('meanRate: ' + timer.meanRate()); + }, 30000); - for(var i = 0; i < 10000; i++) { - timer.update(i); - } - timer.tick(); - console.log(timer.count()); - console.log(timer.min()); - console.log(timer.max()); - console.log(timer.mean()); - console.log(timer.stdDev()); - console.log(timer.percentiles()); - console.log(timer.oneMinuteRate()); - console.log(timer.fiveMinuteRate()); - console.log(timer.fifteenMinuteRate()); - console.log(timer.meanRate()); if (typeof callback == 'function') { callback(); } -} +}; if (module.parent) { module.exports = test;