forked from pmalhaire/xk6-mqtt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
182 lines (163 loc) · 4.45 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
/*
This is a k6 test script that imports the xk6-mqtt and
tests Mqtt with a 100 messages per connection.
*/
import {
check
} from 'k6';
import {
// connect to mqtt
connect,
// close connection
close,
// subscribe to topic
subscribe,
// consume one message
consume,
// publish one message
publish,
} from 'k6/x/mqtt'; // import mqtt plugin
import { Trend } from 'k6/metrics';
const rnd_count = 2000;
// create random number to create a new topic at each run
let rnd = Math.random() * rnd_count;
// keep connection made by VU
let vus_connections = {}
// default timeout (ms)
let timeout = 2000
let publish_trend = new Trend('publish_time', true);
let subscribe_trend = new Trend('subscribe_time', true);
export default function () {
// Mqtt topic one per VU
const k6Topic = `test-k6-plugin-topic ${rnd} ${__VU}`;
// Message content one per ITER and per VU
const k6Message = `k6-message-content-${rnd} ${__VU}:${__ITER}`;
const k6SubId = `k6-sub-${__VU}`;
const k6PubId = `k6-pub-${__VU}`;
let err_pub_client, pub_client;
const host = "localhost";
const port = "1883";
// use one connection per vu
if (k6PubId in vus_connections) {
pub_client = vus_connections[k6PubId];
} else {
try {
pub_client = connect(
// 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
timeout,
)
vus_connections[k6PubId] = pub_client;
} catch (error) {
err_pub_client = error;
}
}
check(err_pub_client, {
"is pub connected": err => err == undefined
});
let err_sub_client, sub_client;
// use one connection per vu
if (k6SubId in vus_connections) {
sub_client = vus_connections[k6SubId];
} else {
try {
sub_client = connect(
// 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
timeout,
)
vus_connections[k6SubId] = sub_client;
} catch (error) {
err_sub_client = error;
}
}
check(err_sub_client, {
"is sub connected": err => err == undefined
});
// subscribe first
let err_subscribe, consume_token;
try {
consume_token = subscribe(
// consume object
sub_client,
// topic to be used
k6Topic,
// The QoS of messages
1,
// timeout in ms
timeout,
)
} catch (error) {
err_subscribe = error
}
check(err_subscribe, {
"is subscribed": err => err == undefined
});
// publish message
let err_publish;
let startTime = new Date().getTime();
try {
publish(
// producer object
pub_client,
// topic to be used
k6Topic,
// The QoS of messages
1,
// Message to be sent
k6Message,
// retain policy on message
false,
// timeout in ms
timeout,
);
publish_trend.add(new Date().getTime() - startTime);
} catch (error) {
err_publish = error
}
check(err_publish, {
"is sent": err => err == undefined
});
let err_consume, message
try {
// Read one message
message = consume(
// token to recieve message
consume_token,
// timeout in ms
timeout,
);
subscribe_trend.add(new Date().getTime() - startTime);
} catch (error) {
err_consume = error
}
check(err_consume, {
"is received": err => err == undefined
});
check(message, {
"is content correct": msg => msg == k6Message
});
}
export function teardown() {
for (client in vus_connections) {
close(client, timeout);
}
}