UNPKG

993 BJavaScriptView Raw
1
2const mqtt = require('../')
3const max = 1000000
4let i = 0
5const start = Date.now()
6let time
7const buf = Buffer.allocUnsafe(10)
8const net = require('net')
9const server = net.createServer(handle)
10let dest
11
12buf.fill('test')
13
14function handle (sock) {
15 sock.resume()
16}
17
18server.listen(0, () => {
19 dest = net.connect(server.address())
20
21 dest.on('connect', tickWait)
22 dest.on('drain', tickWait)
23 dest.on('finish', () => {
24 time = Date.now() - start
25 console.log('Total time', time)
26 console.log('Total packets', max)
27 console.log('Packet/s', max / time * 1000)
28 server.close()
29 })
30})
31
32function tickWait () {
33 // console.log('tickWait', i)
34 let res = true
35 // var toSend = new Buffer(5 + buf.length)
36
37 for (; i < max && res; i++) {
38 res = dest.write(mqtt.generate({
39 cmd: 'publish',
40 topic: 'test',
41 payload: buf
42 }))
43 // buf.copy(toSend, 5)
44 // res = dest.write(toSend, 'buffer')
45 // console.log(res)
46 }
47
48 if (i >= max) {
49 dest.end()
50 }
51}