forked from pmalhaire/xk6-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
192 lines (167 loc) · 4.87 KB
/
test.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
This is a k6 test script that imports the xk6-mqtt and
tests Mqtt with a 100 messages per connection.
*/
import {
check
} from 'k6';
const mqtt = require('k6/x/mqtt');
const rnd_count = 2000;
// create random number to create a new topic at each run
let rnd = Math.random() * rnd_count;
// conection timeout (ms)
let connectTimeout = 2000
// publish timeout (ms)
let publishTimeout = 2000
// subscribe timeout (ms)
let subscribeTimeout = 2000
// connection close timeout (ms)
let closeTimeout = 2000
// Mqtt topic one per VU
const k6Topic = `test-k6-plugin-topic ${rnd} ${__VU}`;
// Connect IDs one connection per VU
const k6SubId = `k6-sub-${rnd}-${__VU}`;
const k6PubId = `k6-pub-${rnd}-${__VU}`;
// number of message pusblished and receives at each iteration
const messageCount = 3;
const host = "localhost";
const port = "1883";
// create publisher client
let publisher = new mqtt.Client(
// The list of URL of MQTT server to connect to
[host + ":" + port],
// A username to authenticate to the MQTT server
"",
// Password to match username
"",
// clean session setting
false,
// Client id for reader
k6PubId,
// timeout in ms
connectTimeout,
)
let err;
try {
publisher.connect()
}
catch (error) {
err = error
}
if (err != undefined) {
console.error("publish connect error:", err)
// you may want to use fail here if you want only to test successfull connection only
// fail("fatal could not connect to broker for publish")
}
// create subscriber client
let subscriber = new mqtt.Client(
// The list of URL of MQTT server to connect to
[host + ":" + port],
// A username to authenticate to the MQTT server
"",
// Password to match username
"",
// clean session setting
false,
// Client id for reader
k6SubId,
// timeout in ms
connectTimeout,
)
try {
subscriber.connect()
}
catch (error) {
err = error
}
if (err != undefined) {
console.error("subscribe connect error:", err)
// you may want to use fail here if you want only to test successfull connection only
// fail("fatal could not connect to broker for subscribe")
}
export default function () {
// Message content one per ITER
const k6Message = `k6-message-content-${rnd} ${__VU}:${__ITER}`;
check(publisher, {
"is publisher connected": publisher => publisher.isConnected()
});
check(subscriber, {
"is subcriber connected": subscriber => subscriber.isConnected()
});
// subscribe first
try {
subscriber.subscribe(
// topic to be used
k6Topic,
// The QoS of messages
1,
// timeout in ms
subscribeTimeout,
)
} catch (error) {
err = error
}
if (err != undefined) {
console.error("subscribe error:", err)
// you may want to use fail here if you want only to test successfull connection only
// fail("fatal could not connect to broker for subscribe")
}
let count = messageCount;
subscriber.addEventListener("message", (obj) => {
// closing as we received one message
let message = obj.message
check(message, {
"message received": msg => msg != undefined
});
check(message, {
"is content correct": msg => msg == k6Message
});
if (--count > 0) {
// tell the subscriber that you want to wait for more than one message
// if you don't call subContinue you'll receive only one message per subscribe
subscriber.subContinue();
}
})
subscriber.addEventListener("error", (err) => {
check(null, {
"message received": false
});
})
for (let i = 0; i < messageCount; i++) {
// publish count messages
let err_publish;
try {
publisher.publish(
// topic to be used
k6Topic,
// The QoS of messages
1,
// Message to be sent
k6Message,
// retain policy on message
false,
// timeout in ms
publishTimeout,
// async publish handlers if needed
// (obj) => { // success
// console.log(obj.type) // publish
// console.log(obj.topic) // published topic
// },
// (err) => { // failure
// console.log(err.type) // error
// console.log(err.message)
// }
);
} catch (error) {
err_publish = error
}
check(err_publish, {
"is sent": err => err == undefined
});
}
}
export function teardown() {
// closing both connections at VU close
publisher.close(closeTimeout);
subscriber.close(closeTimeout);
}