aedes-stats
Advanced tools
Comparing version 3.0.0 to 4.0.0
{ | ||
"name": "aedes-stats", | ||
"version": "3.0.0", | ||
"version": "4.0.0", | ||
"description": "stats for aedes", | ||
@@ -8,7 +8,26 @@ "main": "stats.js", | ||
"lint": "standard --verbose | snazzy", | ||
"test": "tape test.js | faucet", | ||
"coverage": "nyc --reporter=lcov tape test.js" | ||
"unit": "tape test.js | faucet", | ||
"test": "npm run lint && npm run unit", | ||
"coverage": "nyc --reporter=lcov tape test.js", | ||
"test:ci": "npm run lint && npm run coverage", | ||
"license-checker": "license-checker --production --onlyAllow=\"MIT;ISC;BSD-3-Clause;BSD-2-Clause\"", | ||
"release": "read -p 'GITHUB_TOKEN: ' GITHUB_TOKEN && export GITHUB_TOKEN=$GITHUB_TOKEN && release-it --disable-metrics" | ||
}, | ||
"release-it": { | ||
"github": { | ||
"release": true | ||
}, | ||
"git": { | ||
"tagName": "v${version}" | ||
}, | ||
"hooks": { | ||
"before:init": [ | ||
"npm run test" | ||
] | ||
}, | ||
"npm": { | ||
"publish": true | ||
} | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"test" | ||
@@ -18,3 +37,3 @@ ], | ||
"type": "git", | ||
"url": "git+https://github.com/mcollina/aedes-stats.git" | ||
"url": "git+https://github.com/moscajs/aedes-stats.git" | ||
}, | ||
@@ -30,16 +49,19 @@ "keywords": [ | ||
"bugs": { | ||
"url": "https://github.com/mcollina/aedes-stats/issues" | ||
"url": "https://github.com/moscajs/aedes-stats/issues" | ||
}, | ||
"homepage": "https://github.com/mcollina/aedes-stats#readme", | ||
"devDependencies": { | ||
"aedes": "^0.39.0", | ||
"aedes": "^0.42.5", | ||
"faucet": "0.0.1", | ||
"mqtt": "^3.0.0", | ||
"nyc": "^14.1.1", | ||
"license-checker": "^25.0.1", | ||
"mqtt": "^4.1.0", | ||
"nyc": "^15.1.0", | ||
"pre-commit": "^1.2.2", | ||
"qlobber": "^5.0.0", | ||
"release-it": "^13.6.4", | ||
"snazzy": "^8.0.0", | ||
"standard": "^13.1.0", | ||
"tape": "^4.11.0" | ||
"standard": "^14.3.4", | ||
"tape": "^4.13.0" | ||
}, | ||
"dependencies": {} | ||
} |
# aedes-stats | ||
[![Build Status](https://travis-ci.org/mcollina/aedes-stats.svg?branch=master)](https://travis-ci.org/mcollina/aedes-stats) | ||
[![Dependencies Status](https://david-dm.org/mcollina/aedes-stats/status.svg)](https://david-dm.org/mcollina/aedes-stats) | ||
[![devDependencies Status](https://david-dm.org/mcollina/aedes-stats/dev-status.svg)](https://david-dm.org/mcollina/aedes-stats?type=dev) | ||
![](https://github.com/moscajs/aedes-stats/workflows/ci/badge.svg) | ||
[![Dependencies Status](https://david-dm.org/moscajs/aedes-stats/status.svg)](https://david-dm.org/moscajs/aedes-stats) | ||
[![devDependencies Status](https://david-dm.org/moscajs/aedes-stats/dev-status.svg)](https://david-dm.org/moscajs/aedes-stats?type=dev) | ||
<br/> | ||
[![Known Vulnerabilities](https://snyk.io/test/github/mcollina/aedes-stats/badge.svg)](https://snyk.io/test/github/mcollina/aedes-stats) | ||
[![Coverage Status](https://coveralls.io/repos/mcollina/aedes-stats/badge.svg?branch=master&service=github)](https://coveralls.io/github/mcollina/aedes-stats?branch=master) | ||
[![Known Vulnerabilities](https://snyk.io/test/github/moscajs/aedes-stats/badge.svg)](https://snyk.io/test/github/moscajs/aedes-stats) | ||
[![Coverage Status](https://coveralls.io/repos/moscajs/aedes-stats/badge.svg?branch=master&service=github)](https://coveralls.io/github/moscajs/aedes-stats?branch=master) | ||
[![NPM version](https://img.shields.io/npm/v/aedes-stats.svg?style=flat)](https://www.npmjs.com/package/aedes-stats) | ||
@@ -9,0 +9,0 @@ [![NPM downloads](https://img.shields.io/npm/dm/aedes-stats.svg?style=flat)](https://www.npmjs.com/package/aedes-stats) |
42
stats.js
'use strict' | ||
var os = require('os') | ||
var startMeasure = cpuAverage() | ||
function cpuAverage () { | ||
var totalIdle = 0 | ||
var totalTick = 0 | ||
var cpus = os.cpus() | ||
for (var i = 0, len = cpus.length; i < len; i++) { | ||
var cpu = cpus[i] | ||
for (var type in cpu.times) { | ||
totalTick += cpu.times[type] | ||
} | ||
totalIdle += cpu.times.idle | ||
} | ||
return { | ||
idle: totalIdle / cpus.length, | ||
total: totalTick / cpus.length | ||
} | ||
} | ||
function cpuCalculation () { | ||
var endMeasure = cpuAverage() | ||
var idleDifference = endMeasure.idle - startMeasure.idle | ||
var totalDifference = endMeasure.total - startMeasure.total | ||
var cpuPercentage = 100 - ~~(100 * idleDifference / totalDifference) | ||
return cpuPercentage | ||
} | ||
function client () { | ||
@@ -38,3 +67,4 @@ this.stats.connectedClients++ | ||
started: new Date(), | ||
time: new Date() | ||
time: new Date(), | ||
cpuUsage: 0 | ||
} | ||
@@ -52,2 +82,3 @@ | ||
var timer = setInterval(iterate, options.interval || (1 * 1000)) | ||
var cpuUsageTimer = setInterval(function () { aedesInstance.stats.cpuUsage = cpuCalculation() }, 1000) | ||
@@ -59,2 +90,3 @@ function iterate () { | ||
var mem = process.memoryUsage() | ||
var cpu = os.loadavg() | ||
doPub('uptime', uptime) | ||
@@ -67,2 +99,9 @@ doPub('time', aedesInstance.stats.time.toISOString()) | ||
doPub('memory/heap/maximum', mem.heapTotal) | ||
doPub('cpu/usage', stats.cpuUsage) | ||
if (cpu && cpu.length >= 3) { | ||
// ref: http://nodejs.org/api/os.html#os_os_loadavg | ||
doPub('cpu/avg/last/1', cpu[0]) | ||
doPub('cpu/avg/last/5', cpu[1]) | ||
doPub('cpu/avg/last/15', cpu[2]) | ||
} | ||
} | ||
@@ -76,2 +115,3 @@ | ||
clearInterval(timer) | ||
clearInterval(cpuUsageTimer) | ||
aedesEvents.forEach(function (event) { | ||
@@ -78,0 +118,0 @@ aedesInstance.removeListener(event.name, event) |
286
test.js
@@ -6,30 +6,64 @@ 'use strict' | ||
var aedes = require('aedes') | ||
var stats = require('./stats') | ||
var net = require('net') | ||
var QlobberTrue = require('qlobber').QlobberTrue | ||
var matcher = new QlobberTrue({ wildcard_one: '+', wildcard_some: '#' }) | ||
var port = 1889 | ||
var clients = 0 | ||
var server | ||
test('Connect a client and subscribe to get total number of clients', function (t) { | ||
t.plan(1) | ||
function setup () { | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
stats(instance) | ||
if (server && server.listening) { | ||
server.close() | ||
} | ||
server = net.createServer(instance.handle) | ||
server.listen(port) | ||
var subscriber | ||
return { | ||
instance, | ||
server | ||
} | ||
} | ||
subscriber = mqtt.connect({ | ||
function connect (s, opts = {}) { | ||
s = Object.create(s) | ||
var client = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
clientId: opts.clientId || 'my-client-' + clients++, | ||
keepalive: opts.keepAlive || 200 | ||
}) | ||
client.on('end', function () { | ||
if (s.instance.connectedClients > 1) { | ||
return | ||
} | ||
s.instance.close() | ||
s.server.close() | ||
}) | ||
return client | ||
} | ||
subscriber.subscribe('$SYS/+/clients/total') | ||
function checkTopic (actual, expected) { | ||
matcher.clear() | ||
matcher.add(expected) | ||
var bool = matcher.match(expected, actual) | ||
matcher.clear() | ||
return bool | ||
} | ||
test('Connect a client and subscribe to get total number of clients', function (t) { | ||
t.plan(2) | ||
var sysTopic = '$SYS/+/clients/total' | ||
var subscriber = connect(setup()) | ||
subscriber.subscribe(sysTopic) | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.equal('1', message.toString(), 'clients connected') | ||
subscriber.end() | ||
instance.close() | ||
server.close() | ||
t.end() | ||
}) | ||
@@ -39,35 +73,17 @@ }) | ||
test('Connect a client and subscribe to get maximum number of clients', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/clients/maximum' | ||
var s = setup() | ||
var subscriber = connect(s, { clientId: 'subscriber' }) | ||
var additionalClient = connect(s, { clientId: 'client' }) | ||
server.listen(port) | ||
var subscriber, additionalClient | ||
subscriber.subscribe(sysTopic) | ||
additionalClient = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'client', | ||
keepalive: 200 | ||
}) | ||
subscriber = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
}) | ||
subscriber.subscribe('$SYS/+/clients/maximum') | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.equal('2', message.toString(), 'clients connected') | ||
subscriber.end() | ||
additionalClient.end() | ||
instance.close() | ||
server.close() | ||
t.end() | ||
}) | ||
@@ -77,35 +93,17 @@ }) | ||
test('Connect a client and subscribe to get current broker time', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/time' | ||
var s = setup() | ||
var subscriber = connect(s, { clientId: 'subscriber' }) | ||
var additionalClient = connect(s, { clientId: 'client' }) | ||
server.listen(port) | ||
var subscriber, additionalClient | ||
subscriber.subscribe(sysTopic) | ||
additionalClient = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'client', | ||
keepalive: 200 | ||
}) | ||
subscriber = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
}) | ||
subscriber.subscribe('$SYS/+/time') | ||
subscriber.on('message', function (topic, message) { | ||
t.equal(instance.stats.time.toISOString(), message.toString(), 'current broker time') | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.equal(s.instance.stats.time.toISOString(), message.toString(), 'current broker time') | ||
subscriber.end() | ||
additionalClient.end() | ||
instance.close() | ||
server.close() | ||
t.end() | ||
}) | ||
@@ -115,27 +113,16 @@ }) | ||
test('Connect a client and subscribe to get broker up-time', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/uptime' | ||
var s = setup() | ||
var subscriber = connect(s) | ||
server.listen(port) | ||
var subscriber | ||
subscriber.subscribe(sysTopic) | ||
subscriber = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
}) | ||
subscriber.subscribe('$SYS/+/uptime') | ||
subscriber.on('message', function (topic, message) { | ||
var seconds = Math.round((instance.stats.time - instance.stats.started) / 1000) | ||
t.ok(checkTopic(topic, sysTopic)) | ||
var seconds = Math.round((s.instance.stats.time - s.instance.stats.started) / 1000) | ||
t.equal(seconds.toString(), message.toString(), 'Broker uptime') | ||
subscriber.end() | ||
instance.close() | ||
server.close() | ||
t.end() | ||
}) | ||
@@ -145,21 +132,9 @@ }) | ||
test('Connect a client and subscribe to get the number of published messages', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/messages/publish/sent' | ||
var publisher = connect(setup()) | ||
server.listen(port) | ||
var publisher | ||
publisher.subscribe(sysTopic, onSub) | ||
publisher = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'client', | ||
keepalive: 200 | ||
}) | ||
publisher.subscribe('$SYS/+/messages/publish/sent', onSub) | ||
function onSub () { | ||
@@ -171,6 +146,5 @@ publisher.publish('publishing', 'hey there') | ||
publisher.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.equal('2', message.toString(), 'number of published messages') | ||
publisher.end() | ||
instance.close() | ||
server.close() | ||
}) | ||
@@ -180,59 +154,95 @@ }) | ||
test('Connect a client and and subscribe to get current heap usage', function (t) { | ||
t.plan(1) | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/memory/heap/current' | ||
var subscriber = connect(setup()) | ||
server.listen(port) | ||
var subscriber | ||
subscriber.subscribe(sysTopic) | ||
subscriber = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'bytes of heap used currently') | ||
subscriber.end() | ||
t.end() | ||
}) | ||
}) | ||
subscriber.subscribe('$SYS/+/memory/heap/current') | ||
test('Connect a client and subscribe to get maximum heap usage', function (t) { | ||
t.plan(2) | ||
var sysTopic = '$SYS/+/memory/heap/maximum' | ||
var subscriber = connect(setup()) | ||
subscriber.subscribe(sysTopic) | ||
subscriber.on('message', function (topic, message) { | ||
t.pass(message.toString(), 'bytes of heap used currently') | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'max bytes of heap used till now') | ||
subscriber.end() | ||
t.end() | ||
}) | ||
}) | ||
test('Connect a client and subscribe to get cpu usage', function (t) { | ||
t.plan(2) | ||
var sysTopic = '$SYS/+/cpu/usage' | ||
var subscriber = connect(setup()) | ||
subscriber.subscribe(sysTopic) | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'cpu usage') | ||
subscriber.end() | ||
instance.close() | ||
server.close() | ||
t.end() | ||
}) | ||
}) | ||
test('Connect a client and and subscribe to get maximum heap usage', function (t) { | ||
t.plan(1) | ||
test('Connect a client and subscribe to get cpu avg of last 1 min', function (t) { | ||
t.plan(2) | ||
var instance = aedes() | ||
var server = net.createServer(instance.handle) | ||
require('./stats')(instance) | ||
var sysTopic = '$SYS/+/cpu/avg/last/1' | ||
var subscriber = connect(setup()) | ||
server.listen(port) | ||
var subscriber | ||
subscriber.subscribe(sysTopic) | ||
subscriber = mqtt.connect({ | ||
port: port, | ||
host: '127.0.0.1', | ||
clean: true, | ||
clientId: 'subscriber', | ||
keepalive: 200 | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'cpu avg of last 1 min') | ||
subscriber.end() | ||
t.end() | ||
}) | ||
}) | ||
subscriber.subscribe('$SYS/+/memory/heap/maximum') | ||
test('Connect a client and subscribe to get cpu avg of last 5 min', function (t) { | ||
t.plan(2) | ||
var sysTopic = '$SYS/+/cpu/avg/last/5' | ||
var subscriber = connect(setup()) | ||
subscriber.subscribe(sysTopic) | ||
subscriber.on('message', function (topic, message) { | ||
t.pass(message.toString(), 'max bytes of heap used till now') | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'cpu avg of last 5 min') | ||
subscriber.end() | ||
t.end() | ||
}) | ||
}) | ||
test('Connect a client and subscribe to get cpu avg of last 15 min', function (t) { | ||
t.plan(2) | ||
var sysTopic = '$SYS/+/cpu/avg/last/15' | ||
var subscriber = connect(setup()) | ||
subscriber.subscribe(sysTopic) | ||
subscriber.on('message', function (topic, message) { | ||
t.ok(checkTopic(topic, sysTopic)) | ||
t.pass(message.toString(), 'cpu avg of last 15 min') | ||
subscriber.end() | ||
instance.close() | ||
server.close(function () { | ||
process.exit(0) | ||
}) | ||
t.end() | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14319
293
11
3