Created
December 28, 2016 12:21
-
-
Save raed667/a881c1e3d585e987d9ee5b387594fd73 to your computer and use it in GitHub Desktop.
Display error rate when inserting MQTT data into ES
This file contains hidden or 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 ES = require('esta'); | |
const INDEX = 'mqtt', | |
TYPE = 'eclipse'; | |
let counter = 0, | |
errors = 0; | |
ES.CONNECT(INDEX, () => { | |
const client = mqtt.connect('mqtt://iot.eclipse.org'); | |
client.on('connect', () => { | |
client.subscribe('#'); | |
}); | |
client.on('message', (topic, message) => { | |
if (counter > 5000) { | |
client.end(true); | |
console.log(`Error rate : ${(errors / counter) * 100}%`); | |
console.log(`Error count : ${errors}`); | |
process.exit(); | |
} | |
addARecord(message.toString(), topic); | |
}); | |
}); | |
function addARecord(message, topic) { | |
const record = { | |
index: INDEX, | |
type: TYPE, | |
date: new Date().toISOString(), | |
message: message, | |
topic: topic | |
}; | |
ES.CREATE(record, function (response) { | |
counter++; | |
// Check if data has been inserted properly | |
if (response.created === undefined) { | |
errors++; | |
} | |
}); | |
} | |
process.on('uncaughtException', function (err) { | |
console.log('ERROR: ' + err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
We try to insert 5000 messages and check the error rate of the insertion, most of the time it is NOT zero.