This repository was archived by the owner on Sep 28, 2022. It is now read-only.
forked from orchestrated-io/artillery-engine-lambda
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (139 loc) · 4.44 KB
/
index.js
File metadata and controls
161 lines (139 loc) · 4.44 KB
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
'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 debug = require('debug')('engine:sql');
const A = require('async');
const _ = require('lodash');
const helpers = require('artillery-core/lib/engine_util');
const anyDB = require('any-db');
let config;
function SQLEngine (script, ee) {
this.script = script;
this.ee = ee;
this.helpers = helpers;
this.config = script.config;
return this;
}
SQLEngine.prototype.createScenario = function createScenario (scenarioSpec, ee) {
const tasks = scenarioSpec.flow.map(rs => this.step(rs, ee));
return this.compile(tasks, scenarioSpec.flow, ee);
};
SQLEngine.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.query) {
return function query (context, callback) {
debug('Running Query');
let params = {
query: rs.query,
args: [],
afterResponse: null,
beforeRequest: null,
target: config.target
};
if (typeof rs.query === 'object') {
params = {
query: rs.query.statement,
args: rs.query.values,
afterResponse: rs.query.afterResponse,
beforeRequest: rs.query.beforeRequest,
target: config.target
};
}
debug(params);
params.query = helpers.template(params.query, context);
let before = err => {
let onError = err => {
debug('Query Error');
// debug(err);
ee.emit('error', err.number);
return callback(err, context);
};
if (err) return onError(err);
ee.emit('request');
const startedAt = process.hrtime();
context.sql.query(params.query, params.args, function (err, data) {
if (err) return onError(err);
const endedAt = process.hrtime(startedAt);
let delta = (endedAt[0] * 1e9) + endedAt[1];
let after = err => {
if (err) return onError(err);
ee.emit('response', delta, 0, data.rowCount);
debug('Query Response');
debug(data);
return callback(null, context);
};
if (params.afterResponse && config.processor[params.afterResponse]) {
debug('Executing afterResponse');
config.processor[params.afterResponse](params, data, context, ee, after);
} else after();
});
};
if (params.beforeRequest && config.processor[params.beforeRequest]) {
debug('Executing beforeRequest');
config.processor[params.beforeRequest](params, context, ee, before);
} else before();
};
}
return function (context, callback) {
return callback(null, context);
};
};
SQLEngine.prototype.compile = function compile (tasks, scenarioSpec, ee) {
const self = this;
return function scenario (initialContext, callback) {
const init = function init (next) {
debug('Configuration');
config = self.script.config;
debug(config.target);
initialContext.sql = anyDB.createConnection(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);
}
initialContext.sql.close();
return callback(err, context);
});
};
};
module.exports = SQLEngine;