Skip to content
This repository was archived by the owner on Jan 30, 2026. It is now read-only.

Commit 0bf34e1

Browse files
committed
Added option to silence timer
Added a third attribute to set timer to silent, so that it doesn’t write to the stdout. Also, added a demo.js.
1 parent 51e3756 commit 0bf34e1

3 files changed

Lines changed: 30 additions & 9 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,18 @@ First, you need to spin up a timer. When doing so, you can assign the timer a `n
3535

3636
**name**: Okay, so the `name` will also be the name of the metric on CloudWatch. This means, this should be unique for every loop you want to measure. Whereas the `namespace` when requiring the module could be the same throughout the project.
3737

38-
**pulse**: The `pulse` is the number of seconds how often your timer pushed data to CloudWatch. E.g. if you set it to `5`, loop times will be averaged over five seconds and this average will be uploaded only. If you don't set a pulse, i.e. set it to `0`, each loop time will be uploaded immediately.
38+
**pulse** (optional): The `pulse` is the number of seconds how often your timer pushed data to CloudWatch. E.g. if you set it to `5`, loop times will be averaged over five seconds and this average will be uploaded only. If you don't set a pulse, i.e. set it to `0`, each loop time will be uploaded immediately.
3939

4040
**You probably should set a pulse, because CloudWatch only allows 150 uploads per second!** For more info on that topic, read below.
4141

42+
**silent** (optional): As per default, the timer writes its results also to the default output. If you want to override this behavior, you can set it to `silent` using any truthy value, e.g. `true` or `'silent'`, latter being a bit more explicit.
43+
4244
```
4345
var name = 'name-of-the-timer'; // this will be your metric's name
4446
var pulse = 5; // this means data will be pushed to CloudWatch every five seconds
4547
46-
var timer = Timer.getTimer(name, pulse);
48+
var timer = Timer.getTimer(name, pulse); // this will write results to the console
49+
var silentTimer = Timer.getTimer('i-am-silent', 0, 'silent'); // this one will not
4750
```
4851

4952
## Measure

demo.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
var Timer = require('./index.js')('eu-central-1', 'AWS-KEY', 'AWS-SECRET', 'namespace');
2+
3+
var timer = Timer.getTimer('my-timer', 10);
4+
var silentTimer = Timer.getTimer('i-am-silent', 0, 'silent');
5+
6+
timer.start();
7+
silentTimer.start();
8+
9+
setTimeout(function () {
10+
11+
timer.end();
12+
silentTimer.end();
13+
14+
}, 1000);

index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ var namespace = 'empty';
1111
// Prototype //
1212
///////////////
1313

14-
function Timer(name, pulse) {
14+
function Timer(name, pulse, isSilent) {
1515

1616
// validate
17-
if (!(this instanceof Timer)) return new Timer('unnamed', 0);
17+
if (!(this instanceof Timer)) return new Timer('unnamed');
1818
if (!_.isString(name)) return console.error('Name must be a string');
1919
if (_.isUndefined(pulse)) pulse = 0;
2020
if (!_.isNumber(pulse) || _.isNaN(pulse) || pulse < 0)
@@ -23,6 +23,7 @@ function Timer(name, pulse) {
2323
// set
2424
this.name = name;
2525
this.pulse = pulse;
26+
this.isSilent = isSilent;
2627

2728
// init
2829
this.startDate = null;
@@ -104,7 +105,7 @@ Timer.prototype.pushPulseToCloudWatch = function () {
104105

105106
// inform
106107
if (!_.isNaN(averageCycletime)) {
107-
print(_this.name, averageCycletime, true);
108+
if (!this.isSilent) print(_this.name, averageCycletime, true);
108109
uploadMetricToCloudWatch(_this.name, averageCycletime);
109110
}
110111

@@ -125,13 +126,16 @@ Timer.prototype.pushPulseToCloudWatch = function () {
125126
* returns a timer object
126127
* @param {String} name the name of the timer
127128
* @param {Number} pulse the pulse in seconds, is optional
128-
* @return {[type]} [description]
129+
* @param {Boolean} silent whether the timer should write to the log
130+
* (default false, any truthy value works)
131+
* @return {Object} a timer
129132
*/
130-
function getTimer(name, pulse) {
133+
function getTimer(name, pulse, silent) {
131134

132135
if (_.isUndefined(pulse)) pulse = 0;
136+
var writeResultsToLog = _.isUndefined(silent) || silent === false;
133137

134-
var timer = new Timer(name, pulse);
138+
var timer = new Timer(name, pulse, !writeResultsToLog);
135139
if (timer.pulse > 0) timer.pushPulseToCloudWatch();
136140
return timer;
137141

@@ -164,7 +168,7 @@ Timer.prototype.end = function () {
164168
}
165169

166170
// inform
167-
print(this.name, cycletime, false);
171+
if (!this.isSilent) print(this.name, cycletime, false);
168172
return cycletime;
169173
};
170174

0 commit comments

Comments
 (0)