forked from moscajs/aedes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingpong.js
executable file
·53 lines (43 loc) · 1.17 KB
/
pingpong.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#! /usr/bin/env node
var mqtt = require('mqtt')
var convertHrtime = require('convert-hrtime')
var mode = require('compute-mode')
var client = mqtt.connect({ port: 1883, host: 'localhost', clean: true, keepalive: 0 })
var sent = 0
var interval = 5000
var latencies = []
function count () {
console.log('sent/s', sent / interval * 1000)
sent = 0
}
setInterval(count, interval)
function publish () {
sent++
client.publish('test', JSON.stringify(process.hrtime()), { qos: 1 })
}
function subscribe () {
client.subscribe('test', { qos: 1 }, publish)
}
client.on('connect', subscribe)
client.on('message', publish)
client.on('message', function (topic, payload) {
var sentAt = JSON.parse(payload)
var diff = process.hrtime(sentAt)
latencies.push(convertHrtime(diff).ms)
})
client.on('offline', function () {
console.log('offline')
})
client.on('error', function () {
console.log('reconnect!')
client.stream.end()
})
process.on('SIGINT', function () {
var total = latencies.reduce(function (acc, num) {
return acc + num
})
console.log('total', total)
console.log('average', total / latencies.length)
console.log('mode', mode(latencies))
process.exit(0)
})