-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
134 lines (111 loc) · 3.57 KB
/
index.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
'use strict';
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
const net = require('net');
const debug = require('debug')('engine:tcp');
const A = require('async');
const _ = require('lodash');
const helpers = require('artillery-core/lib/engine_util');
function TCPEngine (script, ee) {
this.script = script;
this.ee = ee;
this.helpers = helpers;
this.config = script.config;
return this;
}
TCPEngine.prototype.createScenario = function createScenario (scenarioSpec, ee) {
const tasks = scenarioSpec.flow.map(rs => this.step(rs, ee));
return this.compile(tasks, scenarioSpec.flow, ee);
};
TCPEngine.prototype.step = function step (rs, ee, opts) {
opts = opts || {};
let self = this;
if (rs.loop) {
let steps = _.map(rs.loop, function (rs) {
return self.step(rs, ee, opts);
});
return this.helpers.createLoopWithCount(
rs.count || -1,
steps,
{
loopValue: rs.loopValue || '$loopCount',
overValues: rs.over,
whileTrue: self.config.processor
? self.config.processor[rs.whileTrue] : undefined
});
}
if (rs.log) {
return function log (context, callback) {
return process.nextTick(function () { callback(null, context); });
};
}
if (rs.think) {
return this.helpers.createThink(rs, _.get(self.config, 'defaults.think', {}));
}
if (rs.function) {
return function (context, callback) {
let func = self.config.processor[rs.function];
if (!func) {
return process.nextTick(function () { callback(null, context); });
}
return func(context, ee, function () {
return callback(null, context);
});
};
}
if (rs.send) {
return function send (context, callback) {
const payload = typeof rs.send.payload === 'object'
? JSON.stringify(rs.send.payload) : rs.send.payload;
const buff = Buffer.from(payload, rs.send.encoding);
const onError = err => {
debug('Send error');
ee.emit('error', err);
return callback(err, context);
};
ee.emit('request');
const startedAt = process.hrtime();
context.client.once('error', function (error) {
onError(error)
})
context.client.once('close', function (withError) {
return onError('Socket closed', withError ? ' with error' : '')
})
context.client.once('data', function (data) {
const endedAt = process.hrtime(startedAt);
let delta = (endedAt[0] * 1e9) + endedAt[1];
ee.emit('response', delta, data.toString('hex'), context._uid);
debug('Response');
debug(data);
return callback(null, context);
});
context.client.write(buff, function (err) {
if (err) return onError(err);
});
};
}
return function (context, callback) {
return callback(null, context);
};
};
TCPEngine.prototype.compile = function compile (tasks, scenarioSpec, ee) {
const self = this;
return function scenario (initialContext, callback) {
const init = function init (next) {
initialContext.client = net.createConnection(self.script.config.tcp.port, self.script.config.target)
ee.emit('started');
return next(null, initialContext);
};
let steps = [init].concat(tasks);
A.waterfall(
steps,
function done (err, context) {
if (err) {
debug(err);
}
return callback(err, context);
});
};
};
module.exports = TCPEngine;