Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71f3b93
commit d3a7a8b
Showing
15 changed files
with
164 additions
and
15 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var Gauge = require('./gauge'), | ||
util = require('util'); | ||
|
||
function now() { | ||
return new Date().getTime(); | ||
} | ||
|
||
function CachedGauge(valueFn, expirationInMs) { | ||
var value, lastRefresh; | ||
Gauge.call(this, function() { | ||
if (!lastRefresh || now() - lastRefresh > expirationInMs) { | ||
value = valueFn(); | ||
lastRefresh = now(); | ||
} | ||
return value; | ||
}); | ||
} | ||
|
||
util.inherits(CachedGauge, Gauge); | ||
|
||
module.exports = CachedGauge |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
var Gauge = module.exports = function Gauge(valueFn) { | ||
this.value = valueFn; | ||
this.type = 'gauge'; | ||
} | ||
|
||
Gauge.prototype.printObj = function() { | ||
return {type: 'gauge', value: this.value()}; | ||
} |
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
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
var expect = require('chai').expect | ||
, describe = require('mocha').describe | ||
, it = require('mocha').it | ||
, CachedGauge = require('../../metrics/cached_gauge'); | ||
|
||
describe('CachedGauge', function() { | ||
function seqFn() { | ||
var i = 0; | ||
return function() { | ||
return i++; | ||
} | ||
} | ||
|
||
it('should call function when asked for a value for the first time.', function() { | ||
var gauge = new CachedGauge(seqFn(), 100); | ||
expect(gauge.printObj()).to.have.property('value', 0); | ||
}); | ||
|
||
it('should not call function before expiration timeout.', function() { | ||
var gauge = new CachedGauge(seqFn(), 1000); | ||
expect(gauge.printObj()).to.have.property('value', 0); | ||
expect(gauge.printObj()).to.have.property('value', 0); | ||
}); | ||
|
||
it('should call function after expiration timeout.', function(done) { | ||
var gauge = new CachedGauge(seqFn(), 50); | ||
expect(gauge.printObj()).to.have.property('value', 0); | ||
setTimeout(function() { | ||
expect(gauge.printObj()).to.have.property('value', 1); | ||
done(); | ||
}, 100); | ||
}); | ||
}); |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
var expect = require('chai').expect | ||
, describe = require('mocha').describe | ||
, it = require('mocha').it | ||
, Gauge = require('../../metrics/gauge'); | ||
|
||
describe('Gauge', function() { | ||
it('should call function every time value is requested.', function() { | ||
var i = 0; | ||
var gauge = new Gauge(function() { return i++; }); | ||
|
||
expect(gauge.printObj()).to.have.property('value', 0); | ||
expect(gauge.printObj()).to.have.property('value', 1); | ||
}); | ||
}); |
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
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