@artilleryio/int-core 2.27.0 → 2.28.0-670d2fa
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +4 -0
- package/dist/index.js +13 -0
- package/dist/lib/engine_http.js +817 -0
- package/dist/lib/engine_socketio.js +347 -0
- package/dist/lib/engine_ws.js +278 -0
- package/dist/lib/is-idle-phase.js +9 -0
- package/dist/lib/phases.js +213 -0
- package/dist/lib/readers.js +58 -0
- package/dist/lib/runner.js +463 -0
- package/dist/lib/ssms.js +555 -0
- package/dist/lib/update-global-object.js +75 -0
- package/dist/lib/weighted-pick.js +54 -0
- package/index.ts +14 -0
- package/lib/{engine_http.js → engine_http.ts} +28 -28
- package/lib/{engine_socketio.js → engine_socketio.ts} +13 -11
- package/lib/{engine_ws.js → engine_ws.ts} +16 -10
- package/lib/{is-idle-phase.js → is-idle-phase.ts} +1 -3
- package/lib/{phases.js → phases.ts} +12 -10
- package/lib/{readers.js → readers.ts} +2 -4
- package/lib/{runner.js → runner.ts} +84 -69
- package/lib/{ssms.js → ssms.ts} +18 -18
- package/{index.js → lib/update-global-object.ts} +9 -14
- package/lib/{weighted-pick.js → weighted-pick.ts} +7 -3
- package/package.json +10 -17
- package/tsconfig.build.json +9 -0
- package/types.d.ts +12 -0
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
4
|
+
import { engine_util as engineUtil } from '@artilleryio/int-commons';
|
|
5
|
+
import async from 'async';
|
|
6
|
+
import createDebug from 'debug';
|
|
7
|
+
import deepEqual from 'fast-deep-equal';
|
|
8
|
+
import _ from 'lodash';
|
|
9
|
+
import io from 'socket.io-client';
|
|
10
|
+
import sioWildcard from 'socketio-wildcard';
|
|
11
|
+
import EngineHttp from "./engine_http.js";
|
|
12
|
+
const wildcardPatch = sioWildcard(io.Manager);
|
|
13
|
+
const debug = createDebug('socketio');
|
|
14
|
+
const template = engineUtil.template;
|
|
15
|
+
export default SocketIoEngine;
|
|
16
|
+
function SocketIoEngine(script) {
|
|
17
|
+
this.config = script.config;
|
|
18
|
+
this.socketioOpts = this.config.socketio || {};
|
|
19
|
+
this.httpDelegate = new EngineHttp(script);
|
|
20
|
+
}
|
|
21
|
+
SocketIoEngine.prototype.init = async function () {
|
|
22
|
+
await this.httpDelegate.init();
|
|
23
|
+
};
|
|
24
|
+
SocketIoEngine.prototype.createScenario = function (scenarioSpec, ee) {
|
|
25
|
+
// Adds scenario overridden configuration into the static config
|
|
26
|
+
this.socketioOpts = { ...this.socketioOpts, ...scenarioSpec.socketio };
|
|
27
|
+
const tasks = _.map(scenarioSpec.flow, (rs) => {
|
|
28
|
+
if (typeof rs.think !== 'undefined') {
|
|
29
|
+
return engineUtil.createThink(rs, _.get(this.config, 'defaults.think', {}));
|
|
30
|
+
}
|
|
31
|
+
return this.step(rs, ee);
|
|
32
|
+
});
|
|
33
|
+
return this.compile(tasks, scenarioSpec.flow, ee);
|
|
34
|
+
};
|
|
35
|
+
function markEndTime(ee, _, startedAt) {
|
|
36
|
+
const endedAt = process.hrtime(startedAt);
|
|
37
|
+
const delta = endedAt[0] * 1e9 + endedAt[1];
|
|
38
|
+
ee.emit('histogram', 'socketio.response_time', delta / 1e6);
|
|
39
|
+
}
|
|
40
|
+
function isResponseRequired(spec) {
|
|
41
|
+
return (spec.emit && spec.response && (spec.response.channel || spec.response.on));
|
|
42
|
+
}
|
|
43
|
+
function isAcknowledgeRequired(spec) {
|
|
44
|
+
return spec.emit && spec.acknowledge;
|
|
45
|
+
}
|
|
46
|
+
function isValid(data, response) {
|
|
47
|
+
if (_.isArray(response.data)) {
|
|
48
|
+
//we check if it's an array first (as arrays are objects), and if it's an array, do a deep equality check between both arrays
|
|
49
|
+
return deepEqual(data, response.data);
|
|
50
|
+
}
|
|
51
|
+
if (_.isObject(response.data)) {
|
|
52
|
+
//`json` key is added at some point to the response.data object, to use with `captureOrMatch` function
|
|
53
|
+
//we should omit it when comparing the response to the data
|
|
54
|
+
const expectedResponse = _.omit(response.data, 'json');
|
|
55
|
+
const actualResponse = data[data.length - 1]; // if response.data is not an array, we compare it to the last element of the actual response
|
|
56
|
+
return deepEqual(actualResponse, expectedResponse);
|
|
57
|
+
}
|
|
58
|
+
if (_.isString(response.data)) {
|
|
59
|
+
const expectedResponse = response.data;
|
|
60
|
+
let actualResponse = data[data.length - 1]; // if response.data is not an array, we compare it to the last element of the actual response
|
|
61
|
+
// unless the user wants to test against the entire response
|
|
62
|
+
if (response.concat) {
|
|
63
|
+
actualResponse = data.join('');
|
|
64
|
+
}
|
|
65
|
+
debug(`checking if string ${expectedResponse} is a partial match for string ${actualResponse}`);
|
|
66
|
+
return actualResponse.includes(expectedResponse); //we accept a partial match if it's a string
|
|
67
|
+
}
|
|
68
|
+
debug(`unexpected data type for response.data: ${typeof response.data}`);
|
|
69
|
+
return false;
|
|
70
|
+
}
|
|
71
|
+
function processResponse(ee, data, response, context, callback) {
|
|
72
|
+
// Do we have supplied data to validate?
|
|
73
|
+
if (response.data && !isValid(data, response)) {
|
|
74
|
+
debug('data is not valid:');
|
|
75
|
+
debug(data);
|
|
76
|
+
debug(response);
|
|
77
|
+
const err = 'data is not valid';
|
|
78
|
+
ee.emit('error', err);
|
|
79
|
+
return callback(err, context);
|
|
80
|
+
}
|
|
81
|
+
// If no capture or match specified, then we consider it a success at this point...
|
|
82
|
+
if (!response.capture && !response.match) {
|
|
83
|
+
return callback(null, context);
|
|
84
|
+
}
|
|
85
|
+
// Construct the (HTTP) response...
|
|
86
|
+
const fauxResponse = { body: JSON.stringify(data) };
|
|
87
|
+
// Handle the capture or match clauses...
|
|
88
|
+
engineUtil.captureOrMatch(response, fauxResponse, context, (err, result) => {
|
|
89
|
+
// Were we unable to invoke captureOrMatch?
|
|
90
|
+
if (err) {
|
|
91
|
+
debug(data);
|
|
92
|
+
ee.emit('error', err);
|
|
93
|
+
return callback(err, context);
|
|
94
|
+
}
|
|
95
|
+
if (result !== null) {
|
|
96
|
+
// Do we have any failed matches?
|
|
97
|
+
const failedMatches = _.filter(result.matches, (v) => {
|
|
98
|
+
return !v.success;
|
|
99
|
+
});
|
|
100
|
+
// How to handle failed matches?
|
|
101
|
+
if (failedMatches.length > 0) {
|
|
102
|
+
debug(failedMatches);
|
|
103
|
+
// TODO: Should log the details of the match somewhere
|
|
104
|
+
ee.emit('error', 'Failed match');
|
|
105
|
+
return callback(new Error('Failed match'), context);
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
// Populate the context with captured values
|
|
109
|
+
_.each(result.captures, (v, k) => {
|
|
110
|
+
context.vars[k] = v.value;
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
// Replace the base object context
|
|
114
|
+
// Question: Should this be JSON object or String?
|
|
115
|
+
context.vars.$ = fauxResponse.body;
|
|
116
|
+
// Increment the success count...
|
|
117
|
+
context._successCount++;
|
|
118
|
+
return callback(null, context);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
SocketIoEngine.prototype.step = function (requestSpec, ee) {
|
|
123
|
+
const self = this;
|
|
124
|
+
if (requestSpec.loop) {
|
|
125
|
+
const steps = _.map(requestSpec.loop, (rs) => {
|
|
126
|
+
if (!rs.emit && !rs.loop) {
|
|
127
|
+
return self.httpDelegate.step(rs, ee);
|
|
128
|
+
}
|
|
129
|
+
return self.step(rs, ee);
|
|
130
|
+
});
|
|
131
|
+
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps, {
|
|
132
|
+
loopValue: requestSpec.loopValue,
|
|
133
|
+
loopElement: requestSpec.loopElement || '$loopElement',
|
|
134
|
+
overValues: requestSpec.over,
|
|
135
|
+
whileTrue: self.config.processor
|
|
136
|
+
? self.config.processor[requestSpec.whileTrue]
|
|
137
|
+
: undefined
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
const f = (context, callback) => {
|
|
141
|
+
// Only process emit requests; delegate the rest to the HTTP engine (or think utility)
|
|
142
|
+
if (requestSpec.think) {
|
|
143
|
+
return engineUtil.createThink(requestSpec, _.get(self.config, 'defaults.think', {}));
|
|
144
|
+
}
|
|
145
|
+
if (!requestSpec.emit) {
|
|
146
|
+
const delegateFunc = self.httpDelegate.step(requestSpec, ee);
|
|
147
|
+
return delegateFunc(context, callback);
|
|
148
|
+
}
|
|
149
|
+
ee.emit('counter', 'socketio.emit', 1);
|
|
150
|
+
ee.emit('rate', 'socketio.emit_rate');
|
|
151
|
+
const startedAt = process.hrtime();
|
|
152
|
+
const socketio = context.sockets[requestSpec.namespace] || null;
|
|
153
|
+
if (!(requestSpec.emit && socketio)) {
|
|
154
|
+
debug('invalid arguments');
|
|
155
|
+
ee.emit('error', 'invalid arguments');
|
|
156
|
+
// TODO: Provide a more helpful message
|
|
157
|
+
callback(new Error('socketio: invalid arguments'));
|
|
158
|
+
}
|
|
159
|
+
const outgoing = requestSpec.emit.channel
|
|
160
|
+
? [
|
|
161
|
+
template(requestSpec.emit.channel, context),
|
|
162
|
+
template(requestSpec.emit.data, context)
|
|
163
|
+
]
|
|
164
|
+
: Array.from(requestSpec.emit).map((arg) => template(arg, context));
|
|
165
|
+
const endCallback = (err, context, needEmit) => {
|
|
166
|
+
if (err) {
|
|
167
|
+
debug(err);
|
|
168
|
+
}
|
|
169
|
+
if (isAcknowledgeRequired(requestSpec)) {
|
|
170
|
+
const ackCallback = (...args) => {
|
|
171
|
+
const response = {
|
|
172
|
+
data: template(requestSpec.acknowledge.data || requestSpec.acknowledge.args, context),
|
|
173
|
+
capture: template(requestSpec.acknowledge.capture, context),
|
|
174
|
+
match: template(requestSpec.acknowledge.match, context)
|
|
175
|
+
};
|
|
176
|
+
// Make sure data, capture or match has a default json spec for parsing socketio responses
|
|
177
|
+
_.each(response, (r) => {
|
|
178
|
+
if (_.isPlainObject(r) && !('json' in r)) {
|
|
179
|
+
r.json = '$.0'; // Default to the first callback argument
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
// Acknowledge data can take up multiple arguments of the emit callback
|
|
183
|
+
processResponse(ee, args, response, context, (err) => {
|
|
184
|
+
if (!err) {
|
|
185
|
+
markEndTime(ee, context, startedAt);
|
|
186
|
+
}
|
|
187
|
+
return callback(err, context);
|
|
188
|
+
});
|
|
189
|
+
};
|
|
190
|
+
// Acknowledge required so add callback to emit
|
|
191
|
+
if (needEmit) {
|
|
192
|
+
socketio.emit(...outgoing, ackCallback);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
ackCallback();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
else {
|
|
199
|
+
// No acknowledge data is expected, so emit without a listener
|
|
200
|
+
if (needEmit) {
|
|
201
|
+
socketio.emit(...outgoing);
|
|
202
|
+
}
|
|
203
|
+
markEndTime(ee, context, startedAt);
|
|
204
|
+
return callback(err, context);
|
|
205
|
+
}
|
|
206
|
+
}; // endCallback
|
|
207
|
+
if (isResponseRequired(requestSpec)) {
|
|
208
|
+
const response = {
|
|
209
|
+
channel: template(requestSpec.response.channel || requestSpec.response.on, context),
|
|
210
|
+
concat: template(requestSpec.response.concat, context),
|
|
211
|
+
data: template(requestSpec.response.data || requestSpec.response.args, context),
|
|
212
|
+
capture: template(requestSpec.response.capture, context),
|
|
213
|
+
match: template(requestSpec.response.match, context)
|
|
214
|
+
};
|
|
215
|
+
// Listen for the socket.io response on the specified channel
|
|
216
|
+
let done = false;
|
|
217
|
+
const responseData = [];
|
|
218
|
+
socketio.on(response.channel, function receive(...args) {
|
|
219
|
+
responseData.push(...args);
|
|
220
|
+
if (isValid(responseData, response)) {
|
|
221
|
+
done = true;
|
|
222
|
+
processResponse(ee, responseData, response, context, (err) => {
|
|
223
|
+
if (!err) {
|
|
224
|
+
markEndTime(ee, context, startedAt);
|
|
225
|
+
}
|
|
226
|
+
// Stop listening on the response channel
|
|
227
|
+
socketio.off(response.channel);
|
|
228
|
+
return endCallback(err, context, false);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
// Send the data on the specified socket.io channel
|
|
233
|
+
socketio.emit(...outgoing);
|
|
234
|
+
// If we don't get a response within the timeout, fire an error
|
|
235
|
+
const waitTime = (self.config.timeout || 10) * 1000;
|
|
236
|
+
setTimeout(function responseTimeout() {
|
|
237
|
+
if (!done) {
|
|
238
|
+
if (responseData.length) {
|
|
239
|
+
processResponse(ee, responseData, response, context, (err) => {
|
|
240
|
+
if (!err) {
|
|
241
|
+
markEndTime(ee, context, startedAt);
|
|
242
|
+
}
|
|
243
|
+
// Stop listening on the response channel
|
|
244
|
+
socketio.off(response.channel);
|
|
245
|
+
// called
|
|
246
|
+
return endCallback(err, context, false);
|
|
247
|
+
});
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
const err = 'response timeout';
|
|
251
|
+
ee.emit('error', err);
|
|
252
|
+
return callback(err, context);
|
|
253
|
+
}
|
|
254
|
+
}, waitTime);
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
endCallback(null, context, true);
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
function preStep(context, callback) {
|
|
261
|
+
// Set default namespace in emit action
|
|
262
|
+
requestSpec.namespace = template(requestSpec.namespace, context) || '';
|
|
263
|
+
self.loadContextSocket(requestSpec.namespace, context, (err) => {
|
|
264
|
+
if (err) {
|
|
265
|
+
debug(err);
|
|
266
|
+
ee.emit('error', err.message);
|
|
267
|
+
return callback(err, context);
|
|
268
|
+
}
|
|
269
|
+
return f(context, callback);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
if (requestSpec.emit) {
|
|
273
|
+
return preStep;
|
|
274
|
+
}
|
|
275
|
+
else {
|
|
276
|
+
return f;
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
SocketIoEngine.prototype.loadContextSocket = function (namespace, context, cb) {
|
|
280
|
+
context.sockets = context.sockets || {};
|
|
281
|
+
if (!context.sockets[namespace]) {
|
|
282
|
+
const target = this.config.target + namespace;
|
|
283
|
+
const tls = this.config.tls || {};
|
|
284
|
+
const socketioOpts = template(this.socketioOpts, context);
|
|
285
|
+
const options = _.extend({}, socketioOpts, // templated
|
|
286
|
+
tls);
|
|
287
|
+
const socket = io(target, options);
|
|
288
|
+
context.sockets[namespace] = socket;
|
|
289
|
+
wildcardPatch(socket);
|
|
290
|
+
socket.on('*', () => {
|
|
291
|
+
context.__receivedMessageCount++;
|
|
292
|
+
});
|
|
293
|
+
socket.once('connect', () => {
|
|
294
|
+
cb(null, socket);
|
|
295
|
+
});
|
|
296
|
+
socket.once('connect_error', (err) => {
|
|
297
|
+
cb(err, null);
|
|
298
|
+
});
|
|
299
|
+
socket.once('error', (err) => {
|
|
300
|
+
cb(err, socket);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
else {
|
|
304
|
+
return cb(null, context.sockets[namespace]);
|
|
305
|
+
}
|
|
306
|
+
};
|
|
307
|
+
SocketIoEngine.prototype.closeContextSockets = (context) => {
|
|
308
|
+
if (context.sockets && Object.keys(context.sockets).length > 0) {
|
|
309
|
+
const namespaces = Object.keys(context.sockets);
|
|
310
|
+
namespaces.forEach((namespace) => {
|
|
311
|
+
context.sockets[namespace].disconnect();
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
};
|
|
315
|
+
SocketIoEngine.prototype.compile = function (tasks, scenarioSpec, ee) {
|
|
316
|
+
const self = this;
|
|
317
|
+
function zero(callback, context) {
|
|
318
|
+
context.__receivedMessageCount = 0;
|
|
319
|
+
ee.emit('started');
|
|
320
|
+
self.loadContextSocket('', context, function done(err) {
|
|
321
|
+
if (err) {
|
|
322
|
+
ee.emit('error', err);
|
|
323
|
+
return callback(err, context);
|
|
324
|
+
}
|
|
325
|
+
return callback(null, context);
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
return function scenario(initialContext, callback) {
|
|
329
|
+
initialContext = self.httpDelegate.setInitialContext(initialContext);
|
|
330
|
+
initialContext._pendingRequests = _.size(_.reject(scenarioSpec, (rs) => typeof rs.think === 'number'));
|
|
331
|
+
const steps = _.flatten([
|
|
332
|
+
function z(cb) {
|
|
333
|
+
return zero(cb, initialContext);
|
|
334
|
+
},
|
|
335
|
+
tasks
|
|
336
|
+
]);
|
|
337
|
+
async.waterfall(steps, function scenarioWaterfallCb(err, context) {
|
|
338
|
+
if (err) {
|
|
339
|
+
debug(err);
|
|
340
|
+
}
|
|
341
|
+
if (context) {
|
|
342
|
+
self.closeContextSockets(context);
|
|
343
|
+
}
|
|
344
|
+
return callback(err, context);
|
|
345
|
+
});
|
|
346
|
+
};
|
|
347
|
+
};
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
4
|
+
import url from 'node:url';
|
|
5
|
+
import { engine_util as engineUtil } from '@artilleryio/int-commons';
|
|
6
|
+
import async from 'async';
|
|
7
|
+
import createDebug from 'debug';
|
|
8
|
+
import HttpsProxyAgent from 'https-proxy-agent';
|
|
9
|
+
import _ from 'lodash';
|
|
10
|
+
import WebSocket from 'ws';
|
|
11
|
+
const debug = createDebug('ws');
|
|
12
|
+
const template = engineUtil.template;
|
|
13
|
+
// Injectable dependencies - module namespaces are frozen, so tests
|
|
14
|
+
// replace the WebSocket implementation through this mutable object
|
|
15
|
+
export const _deps = { WebSocket };
|
|
16
|
+
export default WSEngine;
|
|
17
|
+
function WSEngine(script) {
|
|
18
|
+
this.config = script.config;
|
|
19
|
+
}
|
|
20
|
+
WSEngine.prototype.createScenario = function (scenarioSpec, ee) {
|
|
21
|
+
const tasks = _.map(scenarioSpec.flow, (rs) => {
|
|
22
|
+
if (typeof rs.think !== 'undefined') {
|
|
23
|
+
return engineUtil.createThink(rs, _.get(this.config, 'defaults.think', {}));
|
|
24
|
+
}
|
|
25
|
+
return this.step(rs, ee);
|
|
26
|
+
});
|
|
27
|
+
return this.compile(tasks, scenarioSpec.flow, ee);
|
|
28
|
+
};
|
|
29
|
+
function getMessageHandler(context, params, ee, timeout, callback) {
|
|
30
|
+
let done = false;
|
|
31
|
+
setTimeout(() => {
|
|
32
|
+
if (!done) {
|
|
33
|
+
const err = 'response timeout';
|
|
34
|
+
ee.emit('error', err);
|
|
35
|
+
return callback(err, context);
|
|
36
|
+
}
|
|
37
|
+
}, timeout * 1000);
|
|
38
|
+
return function messageHandler(event) {
|
|
39
|
+
ee.emit('counter', 'websocket.messages_received', 1);
|
|
40
|
+
ee.emit('rate', 'websocket.receive_rate');
|
|
41
|
+
done = true;
|
|
42
|
+
const { data } = event;
|
|
43
|
+
debug('WS receive: %s', data);
|
|
44
|
+
if (!data) {
|
|
45
|
+
return callback(new Error('Empty response from WS server'), context);
|
|
46
|
+
}
|
|
47
|
+
let fauxResponse;
|
|
48
|
+
try {
|
|
49
|
+
fauxResponse = { body: JSON.parse(data) };
|
|
50
|
+
}
|
|
51
|
+
catch (_err) {
|
|
52
|
+
fauxResponse = { body: event.data };
|
|
53
|
+
}
|
|
54
|
+
engineUtil.captureOrMatch(params, fauxResponse, context, function captured(err, result) {
|
|
55
|
+
if (err) {
|
|
56
|
+
ee.emit('error', err.message || err.code);
|
|
57
|
+
return callback(err, context);
|
|
58
|
+
}
|
|
59
|
+
const { captures = {}, matches = {} } = result;
|
|
60
|
+
debug('matches: ', matches);
|
|
61
|
+
debug('captures: ', captures);
|
|
62
|
+
// match and capture are strict by default:
|
|
63
|
+
const haveFailedMatches = _.some(result.matches, (v) => !v.success && v.strict !== false);
|
|
64
|
+
const haveFailedCaptures = _.some(result.captures, (v) => v.failed);
|
|
65
|
+
if (haveFailedMatches || haveFailedCaptures) {
|
|
66
|
+
// TODO: Emit the details of each failed capture/match
|
|
67
|
+
return callback(new Error('Failed capture or match'), context);
|
|
68
|
+
}
|
|
69
|
+
_.each(result.matches, (v) => {
|
|
70
|
+
ee.emit('match', v.success, {
|
|
71
|
+
expected: v.expected,
|
|
72
|
+
got: v.got,
|
|
73
|
+
expression: v.expression,
|
|
74
|
+
strict: v.strict
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
_.each(result.captures, (v, k) => {
|
|
78
|
+
_.set(context.vars, k, v.value);
|
|
79
|
+
});
|
|
80
|
+
return callback(null, context);
|
|
81
|
+
});
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
WSEngine.prototype.step = function (requestSpec, ee) {
|
|
85
|
+
if (requestSpec.loop) {
|
|
86
|
+
const steps = _.map(requestSpec.loop, (rs) => this.step(rs, ee));
|
|
87
|
+
return engineUtil.createLoopWithCount(requestSpec.count || -1, steps, {
|
|
88
|
+
loopValue: requestSpec.loopValue || '$loopCount',
|
|
89
|
+
overValues: requestSpec.over,
|
|
90
|
+
whileTrue: this.config.processor
|
|
91
|
+
? this.config.processor[requestSpec.whileTrue]
|
|
92
|
+
: undefined
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
if (requestSpec.think) {
|
|
96
|
+
return engineUtil.createThink(requestSpec, _.get(this.config, 'defaults.think', {}));
|
|
97
|
+
}
|
|
98
|
+
if (requestSpec.function) {
|
|
99
|
+
return (context, callback) => {
|
|
100
|
+
const processFunc = this.config.processor[requestSpec.function];
|
|
101
|
+
if (processFunc) {
|
|
102
|
+
if (processFunc.constructor.name === 'Function') {
|
|
103
|
+
processFunc(context, ee, () => callback(null, context));
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
return processFunc(context, ee)
|
|
107
|
+
.then(() => {
|
|
108
|
+
callback(null, context);
|
|
109
|
+
})
|
|
110
|
+
.catch((err) => {
|
|
111
|
+
callback(err, context);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
if (requestSpec.log) {
|
|
118
|
+
return (context, callback) => {
|
|
119
|
+
console.log(template(requestSpec.log, context));
|
|
120
|
+
return process.nextTick(() => {
|
|
121
|
+
callback(null, context);
|
|
122
|
+
});
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
if (requestSpec.connect) {
|
|
126
|
+
return (context, callback) => process.nextTick(() => {
|
|
127
|
+
callback(null, context);
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
const f = (context, callback) => {
|
|
131
|
+
const params = requestSpec.wait || requestSpec.send;
|
|
132
|
+
// match exists on a string, so check match is not a prototype
|
|
133
|
+
const captureOrMatch = _.has(params, 'capture') || _.has(params, 'match');
|
|
134
|
+
if (captureOrMatch) {
|
|
135
|
+
// only process response if we're capturing
|
|
136
|
+
const timeout = this.config.timeout || _.get(this.config, 'ws.timeout') || 10;
|
|
137
|
+
context.ws.onmessage = getMessageHandler(context, params, ee, timeout, callback);
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
// Reset onmessage to stop steps interfering with each other
|
|
141
|
+
context.ws.onmessage = undefined;
|
|
142
|
+
}
|
|
143
|
+
// Backwards compatible with previous version of `send` api
|
|
144
|
+
let payload = captureOrMatch ? params.payload : params;
|
|
145
|
+
if (payload !== undefined) {
|
|
146
|
+
payload = template(payload, context);
|
|
147
|
+
if (typeof payload === 'object') {
|
|
148
|
+
payload = JSON.stringify(payload);
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
payload = _.toString(payload);
|
|
152
|
+
}
|
|
153
|
+
ee.emit('counter', 'websocket.messages_sent', 1);
|
|
154
|
+
ee.emit('rate', 'websocket.send_rate');
|
|
155
|
+
debug('WS send: %s', payload);
|
|
156
|
+
context.ws.send(payload, (err) => {
|
|
157
|
+
if (err) {
|
|
158
|
+
debug(err);
|
|
159
|
+
ee.emit('error', err);
|
|
160
|
+
return callback(err, null);
|
|
161
|
+
}
|
|
162
|
+
// End step if we're not capturing
|
|
163
|
+
if (!captureOrMatch) {
|
|
164
|
+
return callback(null, context);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
else if (captureOrMatch) {
|
|
169
|
+
debug('WS wait: %j', params);
|
|
170
|
+
}
|
|
171
|
+
else {
|
|
172
|
+
// in the end, we could not send anything, so report it and stop
|
|
173
|
+
const err = 'invalid_step';
|
|
174
|
+
debug(err, requestSpec);
|
|
175
|
+
ee.emit('error', err);
|
|
176
|
+
return callback(err, context);
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
return f;
|
|
180
|
+
};
|
|
181
|
+
function getWsOptions(config) {
|
|
182
|
+
const options = getWsConfig(config);
|
|
183
|
+
const subprotocols = _.get(config, 'ws.subprotocols', []);
|
|
184
|
+
const headers = _.get(config, 'ws.headers', {});
|
|
185
|
+
const subprotocolHeader = _.find(headers, (_value, headerName) => {
|
|
186
|
+
return headerName.toLowerCase() === 'sec-websocket-protocol';
|
|
187
|
+
});
|
|
188
|
+
if (typeof subprotocolHeader !== 'undefined') {
|
|
189
|
+
// NOTE: subprotocols defined via config.ws.subprotocols take precedence:
|
|
190
|
+
subprotocols.push(...subprotocolHeader.split(',').map((s) => s.trim()));
|
|
191
|
+
}
|
|
192
|
+
return { options, subprotocols };
|
|
193
|
+
}
|
|
194
|
+
function getWsInstance(config, scenarioSpec, context, cb) {
|
|
195
|
+
let wsArgs = {
|
|
196
|
+
...getWsOptions(config),
|
|
197
|
+
target: config.target
|
|
198
|
+
};
|
|
199
|
+
const [{ connect }] = scenarioSpec;
|
|
200
|
+
if (connect) {
|
|
201
|
+
if (connect.function && config.processor[connect.function]) {
|
|
202
|
+
const processFn = config.processor[connect.function];
|
|
203
|
+
return processFn(wsArgs, context, (err) => {
|
|
204
|
+
if (err) {
|
|
205
|
+
debug('connect.function', err);
|
|
206
|
+
return cb(err, null);
|
|
207
|
+
}
|
|
208
|
+
context.wsArgs = wsArgs;
|
|
209
|
+
return cb(null, context);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
else if (_.isPlainObject(connect)) {
|
|
213
|
+
const { target = config.target, headers = _.get(config, 'ws.headers', {}), subprotocols = _.get(config, 'ws.subprotocols', []), ...instanceConfig } = connect;
|
|
214
|
+
const opt = getWsOptions({
|
|
215
|
+
tls: config.tls,
|
|
216
|
+
ws: { subprotocols, headers, ...instanceConfig }
|
|
217
|
+
});
|
|
218
|
+
wsArgs = {
|
|
219
|
+
target: template(target, context),
|
|
220
|
+
...opt
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
else {
|
|
224
|
+
wsArgs.target = template(connect, context);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
debug('new WebSocket instance:', wsArgs);
|
|
228
|
+
context.wsArgs = wsArgs;
|
|
229
|
+
return cb(null, context);
|
|
230
|
+
}
|
|
231
|
+
WSEngine.prototype.compile = function (tasks, scenarioSpec, ee) {
|
|
232
|
+
const config = this.config;
|
|
233
|
+
return function scenario(initialContext, callback) {
|
|
234
|
+
function zero(cb) {
|
|
235
|
+
ee.emit('started');
|
|
236
|
+
getWsInstance(config, scenarioSpec, initialContext, cb);
|
|
237
|
+
}
|
|
238
|
+
function one(context, cb) {
|
|
239
|
+
const { wsArgs, ...contextWithoutWsArgs } = context;
|
|
240
|
+
const ws = new _deps.WebSocket(wsArgs.target, wsArgs.subprotocols, wsArgs.options);
|
|
241
|
+
ws.on('open', () => {
|
|
242
|
+
contextWithoutWsArgs.ws = ws;
|
|
243
|
+
return cb(null, contextWithoutWsArgs);
|
|
244
|
+
});
|
|
245
|
+
ws.once('error', (err) => {
|
|
246
|
+
debug(err);
|
|
247
|
+
ee.emit('error', err.message || err.code);
|
|
248
|
+
return cb(err, {});
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
initialContext._successCount = 0;
|
|
252
|
+
const steps = _.flatten([zero, one, tasks]);
|
|
253
|
+
async.waterfall(steps, function scenarioWaterfallCb(err, context) {
|
|
254
|
+
if (err) {
|
|
255
|
+
ee.emit('error', err.code || err.message);
|
|
256
|
+
debug(err);
|
|
257
|
+
}
|
|
258
|
+
if (context?.ws) {
|
|
259
|
+
context.ws.close();
|
|
260
|
+
}
|
|
261
|
+
return callback(err, context);
|
|
262
|
+
});
|
|
263
|
+
};
|
|
264
|
+
};
|
|
265
|
+
function getWsConfig(config) {
|
|
266
|
+
const tls = config.tls || {};
|
|
267
|
+
const { proxy, ...options } = config.ws || {};
|
|
268
|
+
if (proxy) {
|
|
269
|
+
const { url: proxyUrl, ...proxyOptions } = proxy;
|
|
270
|
+
debug('Set proxy: %s, options: %s', proxyUrl, proxyOptions);
|
|
271
|
+
const agent = new HttpsProxyAgent({
|
|
272
|
+
...url.parse(proxyUrl),
|
|
273
|
+
...proxyOptions
|
|
274
|
+
});
|
|
275
|
+
options.agent = agent;
|
|
276
|
+
}
|
|
277
|
+
return _.extend(tls, options);
|
|
278
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
2
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
3
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
4
|
+
export default function isIdlePhase(phase) {
|
|
5
|
+
return ((phase.arrivalRate === 0 && !phase.rampTo) ||
|
|
6
|
+
phase.arrivalCount === 0 ||
|
|
7
|
+
phase.maxVusers === 0 ||
|
|
8
|
+
phase.pause > 0);
|
|
9
|
+
}
|