-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathanalysis-free-mqtt-broker.js
58 lines (52 loc) · 1.46 KB
/
analysis-free-mqtt-broker.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
54
55
56
57
const mqtt = require('mqtt')
// 获取命令行的第一个参数
const args = process.argv.slice(2)
const broker = {
hivemq: 'broker.hivemq.com',
emqx: 'broker.emqx.io',
mosquitto: 'test.mosquitto.org',
local: 'localhost',
}
if (!broker[args[0]]) {
console.log('Error: Please specify a valid broker:' + Object.keys(broker).join(', '))
return
}
// 连接 MQTT 服务器
const host = 'mqtt://' + broker[args[0]] + ':1883'
console.log(host)
const client = mqtt.connect(host)
client.on('connect', () => {
console.log('Connected to ' + broker[args[0]])
client.subscribe('+/#', (error) => {
if (!error) {
console.log('Subscribe successfully!')
setInterval(() => {
const m = metric
// 使用 table 输出,并格式化为 KB/MB/GB
console.table({
'msgNumber/s': m.msgNumber / 10,
'msgNumberAll': m.msgNumberAll,
'msgSize/s': (m.msgSize / 1024 / 10).toFixed(2) + 'KB',
'msgSizeAll': (m.msgSizeAll / 1024).toFixed(2) + 'KB',
})
metric.msgNumber = 0
metric.msgSize = 0
}, 10 * 1000)
} else {
console.log('Subscribe failed!')
}
})
})
// 统计每秒钟消息条数以及消息流量,累计消息流量
let metric = {
msgNumber: 0,
msgNumberAll: 0,
msgSize: 0,
msgSizeAll: 0,
}
client.on('message', function (topic, message) {
metric.msgNumber++
metric.msgNumberAll++
metric.msgSize += message.length
metric.msgSizeAll += message.length
})