Last active
December 21, 2016 23:04
-
-
Save raed667/53ac882c45435238b09dbc62ec7ac760 to your computer and use it in GitHub Desktop.
MQTT messages/second
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
const mqtt = require('mqtt'); | |
const client = mqtt.connect('mqtt://iot.eclipse.org'); | |
let start = null, | |
count = 0, | |
topicAverage = 0, | |
payloadAverage = 0; | |
const topics = [], | |
payloads = []; | |
client.on('connect', () => { | |
client.subscribe('#'); | |
}); | |
client.on('message', (topic, message) => { | |
if (start === null) { | |
start = new Date(); | |
} | |
count++; | |
topics.push(Buffer.byteLength(topic.toString(), 'utf8')); | |
payloads.push(Buffer.byteLength(message.toString(), 'utf8')); | |
const end = new Date(); | |
if (end.getTime() - start.getTime() >= 1000) { | |
client.unsubscribe('#'); | |
topics.forEach(topic => { | |
topicAverage += topic; | |
}); | |
topicAverage = topicAverage / topics.length; | |
payloads.forEach(payload => { | |
payloadAverage += payload; | |
}); | |
payloadAverage = payloadAverage / payloads.length; | |
console.log(`Messages: ${count}`); | |
console.log(`Average topic size: ${topicAverage} bytes`); | |
console.log(`Average payload size: ${payloadAverage} bytes`); | |
console.log(`Date size: ${Buffer.byteLength(start.toJSON(), 'utf8')} bytes`); | |
console.log(`Total time ${(end.getTime() - start.getTime())/1000} seconds`); | |
process.exit() | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment