-
Notifications
You must be signed in to change notification settings - Fork 224
/
k6-test.js
201 lines (172 loc) · 6.33 KB
/
k6-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
193
194
195
196
197
198
199
200
201
/*
* Copyright (c) 2023 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
export { modifyThing } from './modify-thing.js'
export { searchThing, searchThingBatch } from './search-thing.js'
export { readThing } from './read-thing.js'
export { warmup } from './warmup.js'
export { sendDeviceLiveMessage } from './device-live-message.js'
import * as common from './common.js'
import { sendCreateThingsAndPolicies } from './kafka-util.js';
import {
Reader
} from 'k6/x/kafka';
import {
createHttpPushConnection,
createKafkaSourceConnection,
createKafkaTargetConnection,
deleteConnection,
deletePolicy,
deleteThing,
waitForConnectionToOpen,
} from './http-util.js';
export const options = {
setupTimeout: __ENV.SETUP_TIMEOUT,
teardownTimeout: __ENV.TEARDOWN_TIMEOUT,
// will set later
scenarios: {},
batch: common.BATCH_SIZE,
batchPerHost: common.BATCH_SIZE,
};
let WARMUP = 'WARMUP';
let MODIFY_THINGS = 'MODIFY_THINGS';
let SEARCH_THINGS = 'SEARCH_THINGS';
let READ_THINGS = 'READ_THINGS';
let DEVICE_LIVE_MESSAGES = 'DEVICE_LIVE_MESSAGES';
let availableScenarios = {};
availableScenarios[WARMUP] = {
executor: 'per-vu-iterations',
maxDuration: __ENV.THINGS_WARMUP_MAX_DURATION,
exec: 'warmup',
startTime: __ENV.THINGS_WARMUP_START_TIME
};
availableScenarios[MODIFY_THINGS] = {
executor: 'constant-arrival-rate',
duration: __ENV.MODIFY_THINGS_DURATION,
rate: __ENV.MODIFY_THINGS_PER_SECOND,
timeUnit: '1s',
preAllocatedVus: __ENV.MODIFY_THINGS_PRE_ALLOCATED_VUS,
maxVUs: __ENV.MODIFY_THINGS_MAX_VUS,
exec: 'modifyThing',
startTime: __ENV.MODIFY_THINGS_START_TIME
};
availableScenarios[SEARCH_THINGS] = {
executor: 'constant-arrival-rate',
duration: __ENV.SEARCH_THINGS_DURATION,
rate: __ENV.SEARCH_THINGS_PER_SECOND,
timeUnit: '1s',
preAllocatedVus: __ENV.SEARCH_THINGS_PRE_ALLOCATED_VUS,
maxVUs: __ENV.SEARCH_THINGS_MAX_VUS,
exec: 'searchThing',
startTime: __ENV.SEARCH_THINGS_START_TIME
};
availableScenarios[READ_THINGS] = {
executor: 'constant-arrival-rate',
duration: __ENV.READ_THINGS_DURATION,
rate: __ENV.READ_THINGS_PER_SECOND,
timeUnit: '1s',
preAllocatedVus: __ENV.READ_THINGS_PRE_ALLOCATED_VUS,
maxVUs: __ENV.READ_THINGS_MAX_VUS,
exec: 'readThing',
startTime: __ENV.READ_THINGS_START_TIME
};
availableScenarios[DEVICE_LIVE_MESSAGES] = {
executor: 'constant-arrival-rate',
duration: __ENV.DEVICE_LIVE_MESSAGES_DURATION,
rate: __ENV.DEVICE_LIVE_MESSAGES_PER_SECOND,
timeUnit: '1s',
preAllocatedVus: __ENV.DEVICE_LIVE_MESSAGES_PRE_ALLOCATED_VUS,
maxVUs: __ENV.DEVICE_LIVE_MESSAGES_MAX_VUS,
exec: 'sendDeviceLiveMessage',
startTime: __ENV.DEVICE_LIVE_MESSAGES_START_TIME
};
let scenariosToRun = __ENV.SCENARIOS_TO_RUN !== undefined ? __ENV.SCENARIOS_TO_RUN.split(/\s*,\s*/) : undefined;
if (scenariosToRun !== undefined) {
scenariosToRun.forEach(scenario => {
if (availableScenarios[scenario] !== undefined) {
options.scenarios[scenario] = availableScenarios[scenario];
}
});
}
export function setup() {
let httpPushConnection, kafkaTargetConnection, kafkaSourceConnection, consumer;
if (__ENV.CREATE_DITTO_CONNECTIONS == 1) {
let httpPushConnectionId = undefined;
if (shouldCreateHttpPushConnection()) {
httpPushConnection = createHttpPushConnection();
waitForConnectionToOpen(httpPushConnection.id);
}
if (shouldCreateKafkaSourceConnection()) {
common.createThingsTopicsIfNotAvailable();
kafkaSourceConnection = createKafkaSourceConnection(httpPushConnectionId);
waitForConnectionToOpen(kafkaSourceConnection.id);
}
}
if (shouldCreateKafkaTargetConnection()) {
consumer = new Reader({
brokers: common.BOOTSTRAP_SERVERS,
groupID: common.CREATE_UPDATE_THING_CONSUMER_GROUP_ID,
groupTopics: [common.CREATE_UPDATE_THING_REPLY_TOPIC],
connectLogger: __ENV.KAFKA_CONSUMER_LOGGER_ENABLED == 1,
maxWait: parseInt(__ENV.CREATE_UPDATE_THING_CONSUMER_MAX_WAIT_TIME_S) * 1000000000
});
kafkaTargetConnection = createKafkaTargetConnection();
waitForConnectionToOpen(kafkaTargetConnection.id);
let thingIds = []
for (let i = 0; i < common.THINGS_COUNT; i++) {
thingIds.push(common.GET_THING_ID(i))
}
sendCreateThingsAndPolicies(thingIds, common.KAFKA_CREATE_UPDATE_PRODUCER, consumer);
console.log('created things');
}
return {
kafkaSourceConnection: kafkaSourceConnection,
kafkaTargetConnection: kafkaTargetConnection,
httpPushConnection: httpPushConnection
};
}
export default function () {
console.log('no scenarios to run configured');
}
export function teardown(config) {
console.log("TEARDOWN EXECUTING...")
if (__ENV.DELETE_THINGS == 1) {
for (let i = 0; i < common.THINGS_COUNT; i++) {
let thingId = common.GET_THING_ID(i);
deleteThing(thingId);
deletePolicy(thingId);
}
}
if (__ENV.DELETE_DITTO_CONNECTIONS == 1) {
if (config.httpPushConnection != undefined) {
deleteConnection(config.httpPushConnection.id);
}
if (config.kafkaTargetConnection != undefined) {
deleteConnection(config.kafkaTargetConnection.id);
}
if (config.kafkaSourceConnection != undefined) {
deleteConnection(config.kafkaSourceConnection.id);
}
if (__ENV.CREATE_THINGS == 1 || options.scenarios[MODIFY_THINGS] !== undefined) {
common.deleteTopics();
}
}
}
function shouldCreateHttpPushConnection() {
return scenariosToRun.indexOf(DEVICE_LIVE_MESSAGES) !== -1 || scenariosToRun.indexOf(MODIFY_THINGS) !== -1
}
function shouldCreateKafkaSourceConnection() {
return __ENV.CREATE_THINGS == 1 || scenariosToRun.indexOf(MODIFY_THINGS) !== -1
}
function shouldCreateKafkaTargetConnection() {
return __ENV.CREATE_THINGS == 1;
}