diff --git a/lib/registry.js b/lib/registry.js index e658d43..f04b7ae 100644 --- a/lib/registry.js +++ b/lib/registry.js @@ -1,4 +1,5 @@ -var _ = require('underscore'); +const isEmpty = require('lodash.isempty'); +const isFunction = require('lodash.isfunction'); //global collection of metrics. Having this global makes it easy for other parts of application to access var _metrics; @@ -9,13 +10,13 @@ function reset() { } function register(key, metric) { - if (_.isEmpty(metric)) { + if (isEmpty(metric)) { metric = key; key = null; } key = determineKey(key, metric); - if (_.isEmpty(key)) { + if (isEmpty(key)) { throw Error('Cannot register a metric without a key. Key can be specified as a parameter, or supplied in the metric as name, name(), or getName()'); } @@ -24,11 +25,11 @@ function register(key, metric) { function determineKey(key, metric) { - if (_.isEmpty(key)) { - if (_.isFunction(metric.name)) { + if (isEmpty(key)) { + if (isFunction(metric.name)) { key = metric.name(); } - else if (_.isFunction(metric.getName)) { + else if (isFunction(metric.getName)) { key = metric.getName(); } else if (metric.name) { @@ -45,7 +46,7 @@ function findOrCreate(metricname, constructor) { existingMetric = get(metricname); } - if (_.isEmpty(existingMetric)) { + if (isEmpty(existingMetric)) { var newMetric = new constructor(metricname); return register(newMetric); } else { diff --git a/lib/statman.js b/lib/statman.js index 03eee2e..1d3cb42 100644 --- a/lib/statman.js +++ b/lib/statman.js @@ -1,10 +1,8 @@ -var Stopwatch = module.exports.Stopwatch = require('statman-stopwatch'); -var Meter = module.exports.Meter = require('statman-meter'); -var Gauge = module.exports.Gauge = require('statman-gauge'); +const Stopwatch = module.exports.Stopwatch = require('statman-stopwatch'); +const Meter = module.exports.Meter = require('statman-meter'); +const Gauge = module.exports.Gauge = require('statman-gauge'); -var _ = require('underscore'); - -var _registry = require('./registry'); +const _registry = require('./registry'); // var httpFilters = require('./httpFilters'); // exports.httpFilters = httpFilters; diff --git a/package.json b/package.json index 09e096d..fd7e4e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "statman", - "version": "1.3.0", + "version": "1.4.0", "description": "Package to assist with collection of metrics, including stopwatch functionality", "main": "./lib/statman.js", "scripts": { @@ -16,10 +16,12 @@ "url": "https://github.com/jasonray/statman/issues" }, "dependencies": { + "lodash.isempty": "^4.4.0", + "lodash.isfunction": "^3.0.9", + "lodash.isnull": "^3.0.0", "statman-gauge": "^1.0.5", "statman-meter": "^1.0.2", - "statman-stopwatch": "^2.4.0", - "underscore": "^1.8.3" + "statman-stopwatch": "^2.7.0" }, "devDependencies": { "should": "^11.1.2", diff --git a/test/gauge-test.js b/test/gauge-test.js index ece8694..4a6a0bf 100644 --- a/test/gauge-test.js +++ b/test/gauge-test.js @@ -1,52 +1,52 @@ /*jslint node: true */ "use strict"; -var mocha = require('mocha'); -var assert = require('assert'); -var should = require('should'); -var statman = require('../lib/statman'); +const mocha = require('mocha'); +const assert = require('assert'); +const should = require('should'); +const statman = require('../lib/statman'); describe('gauge (smoke test)', function () { it('gauagename', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); assert.equal('metric-name', gauge.name()); }); it('increment', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.increment(); assert.equal(1, gauge.value()); }); it('incrementByValue', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.set(10); gauge.increment(2); assert.equal(12, gauge.value()); }); it('decrement', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.set(10); gauge.decrement(); assert.equal(9, gauge.value()); }); it('decrementByValue', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.set(10); gauge.decrement(2); assert.equal(8, gauge.value()); }); it('set', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.set(5); assert.equal(5, gauge.value()); }); it('toString produces something with the value', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); gauge.set(5); gauge.toString().should.containEql(5); }); diff --git a/test/meter-test.js b/test/meter-test.js index b25bc12..b561733 100644 --- a/test/meter-test.js +++ b/test/meter-test.js @@ -1,28 +1,25 @@ /*jslint node: true */ "use strict"; -var statman = require('../lib/statman'); -var should = require('should'); +const statman = require('../lib/statman'); +const should = require('should'); describe('meter (smoke test)', function () { this.timeout(5000); it('init should return an instance of meter', function () { - var meter; - meter = new statman.Meter(); + const meter = new statman.Meter(); should.exist(meter); }); it('record once and get count should return 1', function () { - var meter; - meter = new statman.Meter(); + const meter = new statman.Meter(); meter.record(); meter.getCount().should.be.equal(1); }); it('record and read', function () { - var meter; - meter = new statman.Meter(); + const meter = new statman.Meter(); meter.record(1000); meter.record(2000); meter.getAverage().should.equal(1500); @@ -30,9 +27,9 @@ describe('meter (smoke test)', function () { }); it('meter.start() provides an event, which can be used to auto record meter', function (done) { - var meter = new statman.Meter(); + const meter = new statman.Meter(); - var meterEvent = meter.startEvent(); + const meterEvent = meter.startEvent(); setTimeout(function () { meterEvent.stop(); @@ -44,8 +41,7 @@ describe('meter (smoke test)', function () { }); it('toString()', function () { - var meter; - meter = new statman.Meter(); + const meter = new statman.Meter(); meter.record(2.2); meter.record(4.4); meter.toString().should.equal('[count:2; average:3.30]'); diff --git a/test/statman-test.js b/test/statman-test.js index 377f369..6922e40 100644 --- a/test/statman-test.js +++ b/test/statman-test.js @@ -1,6 +1,6 @@ -var statman = require('../lib/statman'); -var assert = require('assert'); -var should = require('should'); +const statman = require('../lib/statman'); +const assert = require('assert'); +const should = require('should'); describe('registry', function () { beforeEach(function () { @@ -8,9 +8,9 @@ describe('registry', function () { }); it('registry() => returns full list of metrics', function () { - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -18,9 +18,9 @@ describe('registry', function () { }); it('registry(name) => returns single item', function () { - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -30,11 +30,11 @@ describe('registry', function () { }); it('registry can have key passed explicitly', function () { - var metric = {value: 'abc'}; + const metric = {value: 'abc'}; - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -44,11 +44,11 @@ describe('registry', function () { }); it('registry can have key discovered from name', function () { - var metric = {name: 'x'}; + const metric = {name: 'x'}; - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -58,15 +58,15 @@ describe('registry', function () { }); it('registry can have key discovered from name()', function () { - var metric = { + const metric = { name: function () { return 'x' } }; - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -76,15 +76,15 @@ describe('registry', function () { }); it('registry can have key discovered from getName()', function () { - var metric = { + const metric = { getName: function () { return 'x' } }; - var metricA = {name: 'metricA'}; - var metricB = {name: 'metricB'}; - var metricC = {name: 'metric-C'}; + const metricA = {name: 'metricA'}; + const metricB = {name: 'metricB'}; + const metricC = {name: 'metric-C'}; statman.register(metricA); statman.register(metricB); statman.register(metricC); @@ -94,12 +94,12 @@ describe('registry', function () { }); it('registering metric with same name overwrites the first', function () { - var metricA = { + const metricA = { name: function () { return 'metric-a' }, value: 1 }; - var metricB = { + const metricB = { name: function () { return 'metric-a' }, value: 2 @@ -126,12 +126,12 @@ describe('registry', function () { describe('gauge in registry', function () { it('get new instance of a gauge', function () { - var gauge = statman.gauge('metric-name'); + const gauge = statman.gauge('metric-name'); gauge.name().should.equal('metric-name'); }); it('explicit register gauge', function () { - var gauge = new statman.Gauge('metric-name'); + const gauge = new statman.Gauge('metric-name'); should.not.exist(statman.registry('metric-name')); statman.register(gauge); should.exist(statman.registry('metric-name')); @@ -155,12 +155,12 @@ describe('registry', function () { describe('meter in registry', function () { it('get new instance of a meter', function () { - var meter = statman.meter('metric-name'); + const meter = statman.meter('metric-name'); meter.name().should.equal('metric-name'); }); it('explicit register meter', function () { - var meter = new statman.Meter('metric-name'); + const meter = new statman.Meter('metric-name'); should.not.exist(statman.registry('metric-name')); statman.register(meter); should.exist(statman.registry('metric-name')); diff --git a/test/stopwatch-test.js b/test/stopwatch-test.js index 5618c36..4dee061 100644 --- a/test/stopwatch-test.js +++ b/test/stopwatch-test.js @@ -1,65 +1,64 @@ /*jslint node: true */ "use strict"; -var mocha = require('mocha'); -var assert = require('assert'); -var should = require('should'); -var statman = require('../lib/statman'); -var TestHelper = require('./testhelper'); -var _ = require('underscore'); +const mocha = require('mocha'); +const assert = require('assert'); +const should = require('should'); +const statman = require('../lib/statman'); +const verifyDelta = require('./testhelper').assertCloseEnough; describe('stopwatch (smoke test)', function () { beforeEach(function() { statman.reset(); }); it('explicit constructor', function () { - var stopwatch = new statman.Stopwatch('metric-name'); + const stopwatch = new statman.Stopwatch('metric-name'); should.exists(stopwatch); }); it('explicit constructor (w/no name or autostart)', function () { - var stopwatch = new statman.Stopwatch(); + const stopwatch = new statman.Stopwatch(); should.exist(stopwatch.name()); }); it('explicit constructor (w/autostart)', function () { - var stopwatch = new statman.Stopwatch(true); + const stopwatch = new statman.Stopwatch(true); should.exist(stopwatch.name()); stopwatch.name().should.not.equal(true); }); it('explicit constructor (w/name and autostart)', function () { - var stopwatch = new statman.Stopwatch('metric-name', true); + const stopwatch = new statman.Stopwatch('metric-name', true); should.exist(stopwatch.name()); stopwatch.name().should.equal('metric-name'); }); it('implicit constructor', function () { - var stopwatch = statman.stopwatch('metric-name'); + const stopwatch = statman.stopwatch('metric-name'); should.exists(stopwatch); }); it('implicit constructor w/no name', function () { - var stopwatch = statman.stopwatch(); + const stopwatch = statman.stopwatch(); should.exists(stopwatch); should.exist(stopwatch.name()); }); it('name', function () { - var stopwatch = new statman.Stopwatch('metric-name'); + const stopwatch = new statman.Stopwatch('metric-name'); assert.equal('metric-name', stopwatch.name()); }); it('access via registry', function () { - var stopwatch = statman.stopwatch('metric-name'); + const stopwatch = statman.stopwatch('metric-name'); statman.registry('metric-name').start(); statman.registry('metric-name').stop(); }); it('start and read (10ms)', function (done) { - var testtime = 10; + const testtime = 10; - var stopwatch = new statman.Stopwatch(); + const stopwatch = new statman.Stopwatch(); stopwatch.start(); setTimeout(function () { var delta = stopwatch.read(); @@ -69,9 +68,9 @@ describe('stopwatch (smoke test)', function () { }); it('autostart and read (10ms)', function (done) { - var testtime = 10; + const testtime = 10; - var stopwatch = new statman.Stopwatch(true); + const stopwatch = new statman.Stopwatch(true); setTimeout(function () { var delta = stopwatch.read(); verifyDelta(testtime, delta, 10); @@ -81,20 +80,20 @@ describe('stopwatch (smoke test)', function () { describe('toString()', function () { it('idle', function () { - var stopwatch = statman.stopwatch('sw'); + const stopwatch = statman.stopwatch('sw'); //[sw => state:init; value:NaN] stopwatch.toString().should.containEql('state:init'); stopwatch.toString().should.containEql('value:'); }); it('started', function () { - var stopwatch = statman.stopwatch('sw'); + const stopwatch = statman.stopwatch('sw'); stopwatch.start(); //[sw => state:running; value:0.01] stopwatch.toString().should.containEql('state:running'); stopwatch.toString().should.containEql('value:'); }); it('stopped', function () { - var stopwatch = statman.stopwatch('sw'); + const stopwatch = statman.stopwatch('sw'); stopwatch.start(); stopwatch.stop(); //[sw => state:stopped; value:0.01] @@ -102,13 +101,4 @@ describe('stopwatch (smoke test)', function () { stopwatch.toString().should.containEql('value:'); }); }); -}); - -//TODO: -//replace with: statman.TestHelper.assertCloseEnough(testtime, delta, defaultPrecision); -function verifyDelta(expected, actual, acceptedVariance) { - var lowerThreshold = expected - acceptedVariance; - var upperThreshold = expected + acceptedVariance; - var message = "Expected " + expected + " ± " + acceptedVariance + ", was " + actual + "."; - assert.ok((actual >= lowerThreshold) && (actual <= upperThreshold), message); -} \ No newline at end of file +}); \ No newline at end of file diff --git a/test/testhelper.js b/test/testhelper.js index fce61c4..49ce656 100644 --- a/test/testhelper.js +++ b/test/testhelper.js @@ -1,12 +1,11 @@ -var _ = require('underscore'); -var assert = require('assert'); - +const assert = require('assert'); +const isNull = require('lodash.isnull'); function assertCloseEnough(actual, expected, acceptedVariance) { - if (_.isNull(acceptedVariance)) acceptedVariance = 10; - var lowerThreshold = expected - acceptedVariance; - var upperThreshold = expected + acceptedVariance; - var message = "Expected " + expected + " ± " + acceptedVariance + ", was " + actual + "."; + if (isNull(acceptedVariance)) acceptedVariance = 10; + const lowerThreshold = expected - acceptedVariance; + const upperThreshold = expected + acceptedVariance; + const message = "Expected " + expected + " ± " + acceptedVariance + ", was " + actual + "."; assert.ok((actual >= lowerThreshold) && (actual <= upperThreshold), message); }