Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

measured

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

measured - npm Package Compare versions

Comparing version 0.1.3 to 0.1.4

37

lib/metrics/Meter.js
var units = require('../util/units');
var EWMA = require('../util/ExponentiallyMovingWeightedAverage');
var Stopwatch = require('../util/Stopwatch');

@@ -10,2 +11,3 @@ module.exports = Meter;

this._tickInterval = properties.tickInterval || Meter.TICK_INTERVAL;
if (properties.getTime) this._getTime = properties.getTime;

@@ -17,5 +19,5 @@ this._m1Rate = new EWMA(1 * units.MINUTES, this._tickInterval);

this._currentSum = 0;
this._lastToJSON = null
this._interval = null;
this._startTime = null;
this._startTime = this._getTime();
this._lastToJSON = this._getTime();
this._interval = setInterval(this._tick.bind(this), Meter.TICK_INTERVAL);
}

@@ -39,5 +41,2 @@

Meter.prototype.start = function() {
this._interval = setInterval(this._tick.bind(this), Meter.TICK_INTERVAL);
this._startTime = Date.now();
this._lastToJSON = Date.now();
};

@@ -47,4 +46,17 @@

clearInterval(this._interval);
this._interval = null;
};
Meter.prototype.ref = function() {
if (this._interval && this._interval.ref) {
this._interval.ref();
}
};
Meter.prototype.unref = function() {
if (this._interval && this._interval.unref) {
this._interval.unref();
}
};
Meter.prototype._tick = function() {

@@ -64,3 +76,3 @@ this._m1Rate.tick();

var elapsed = Date.now() - this._startTime;
var elapsed = this._getTime() - this._startTime;
return this._count / elapsed * this._rateUnit;

@@ -71,7 +83,7 @@ };

var currentSum = this._currentSum;
var duration = Date.now() - this._lastToJSON;
var duration = this._getTime() - this._lastToJSON;
var currentRate = currentSum / duration * this._rateUnit;
this._currentSum = 0;
this._lastToJSON = Date.now();
this._lastToJSON = this._getTime();

@@ -92,1 +104,8 @@ // currentRate could be NaN if duration was 0, so fix that

};
Meter.prototype._getTime = function() {
if (!process.hrtime) return Date.now();
var hrtime = process.hrtime();
return hrtime[0] / 1000 + hrtime[1] / (1000 * 1000);
};

@@ -11,2 +11,3 @@ var Histogram = require('./Histogram');

this._histogram = properties.histogram || new Histogram;
this._getTime = properties.getTime;
}

@@ -16,3 +17,3 @@

var self = this;
var watch = new Stopwatch();
var watch = new Stopwatch({getTime: this._getTime});

@@ -19,0 +20,0 @@ watch.once('end', function(elapsed) {

@@ -6,6 +6,8 @@ var util = require('util');

util.inherits(Stopwatch, EventEmitter);
function Stopwatch() {
function Stopwatch(options) {
options = options || {};
EventEmitter.call(this);
this._start = Date.now();
if (options.getTime) this._getTime = options.getTime;
this._start = this._getTime();
this._ended = false;

@@ -18,3 +20,3 @@ }

this._ended = true;
var elapsed = Date.now() - this._start;
var elapsed = this._getTime() - this._start;

@@ -24,1 +26,8 @@ this.emit('end', elapsed);

};
Stopwatch.prototype._getTime = function() {
if (!process.hrtime) return Date.now();
var hrtime = process.hrtime();
return hrtime[0] / 1000 + hrtime[1] / (1000 * 1000);
};

@@ -5,3 +5,3 @@ {

"description": "This is an alternative port of Coda Hale's metrics library.",
"version": "0.1.3",
"version": "0.1.4",
"homepage": "https://github.com/felixge/node-measured",

@@ -8,0 +8,0 @@ "repository": {

@@ -140,2 +140,5 @@ # measured

be reset to the default settings (patch welcome).
* `unref()` Unrefs the backing timer. The meter will not keep the event loop
alive. Idempotent.
* `ref()` Refs the backing timer again. Idempotent.

@@ -142,0 +145,0 @@ **toJSON Output:**

@@ -8,8 +8,13 @@ var common = require('../../common');

var meter;
var clock;
test('Meter', {
before: function() {
meter = new common.measured.Meter();
meter.start = function() {};
clock = sinon.useFakeTimers();
meter = new common.measured.Meter({getTime: Date.now});
},
after: function() {
clock.restore();
},
'all values are correctly initialized': function() {

@@ -47,4 +52,2 @@ assert.deepEqual(meter.toJSON(), {

'mean rate': function() {
var clock = sinon.useFakeTimers();
meter.mark(5);

@@ -60,8 +63,5 @@ clock.tick(5000);

assert.equal(json['mean'], 0.5);
clock.restore();
},
'currentRate is the observed rate since the last toJSON call': function() {
var clock = sinon.useFakeTimers();
meter.mark(1);

@@ -74,4 +74,2 @@ meter.mark(2);

assert.equal(meter.toJSON()['currentRate'], 2);
clock.restore();
},

@@ -89,3 +87,2 @@

'currentRate also resets internal duration timer by reading it': function() {
var clock = sinon.useFakeTimers();
meter.mark(1);

@@ -103,4 +100,2 @@ meter.mark(2);

assert.strictEqual(meter.toJSON()['currentRate'], 1);
clock.restore();
},

@@ -107,0 +102,0 @@

@@ -12,4 +12,6 @@ var common = require('../../common');

var histogram;
var clock;
test('Timer', {
before: function() {
clock = sinon.useFakeTimers();
meter = sinon.stub(new Meter);

@@ -21,5 +23,10 @@ histogram = sinon.stub(new Histogram);

histogram : histogram,
getTime: Date.now
});
},
after: function() {
clock.restore();
},
'can be initialized without options': function() {

@@ -56,3 +63,2 @@ timer = new Timer();

'#start returns a Stopwatch which updates the timer': function() {
var clock = sinon.useFakeTimers();
clock.tick(10);

@@ -66,4 +72,2 @@

assert.equal(histogram.update.args[0][0], 50);
clock.restore();
},

@@ -70,0 +74,0 @@

@@ -12,3 +12,3 @@ var common = require('../../common');

clock = sinon.useFakeTimers();
watch = new Stopwatch();
watch = new Stopwatch({getTime: Date.now});
},

@@ -21,5 +21,2 @@

'returns time on end': function() {
clock.tick(10);
var watch = new Stopwatch();
clock.tick(100);

@@ -32,3 +29,2 @@

'emits time on end': function() {
var watch = new Stopwatch();
clock.tick(20);

@@ -47,3 +43,2 @@

'becomes useless after being ended once': function() {
var watch = new Stopwatch();
clock.tick(20);

@@ -50,0 +45,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc