This repository has been archived by the owner on Jun 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
166 lines (138 loc) · 4.79 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
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
/* 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/. */
'use strict';
const debug = require('debug')('plugin:ensure');
const filtrex = require('filtrex').compileExpression;
class EnsurePlugin {
constructor(script, events) {
// If running in Artillery v1, do nothing
// If running in Artillery v2, we only want to run on the main thread
if (!global.artillery) {
debug('Running in an unsupported Artillery version, nothing to do');
return;
}
if (global.artillery &&
Number(global.artillery.version.slice(0, 1)) === 1) {
debug('Running in Artillery v1, nothing to do')
return;
}
if (global.artillery &&
Number(global.artillery.version.slice(0, 1)) > 1 &&
typeof process.env.LOCAL_WORKER_ID !== 'undefined') {
debug('Running in a worker, nothing to do')
return;
}
debug('plugin loaded');
this.script = script;
this.events = events;
global.artillery.ext(
{
ext: 'beforeExit',
method: async (data) => {
if (typeof this.script?.config?.ensure === 'undefined' || typeof process.env.ARTILLERY_DISABLE_ENSURE !== 'undefined') {
return;
}
debug(JSON.stringify(data));
const vars = EnsurePlugin.statsToVars(data);
debug({vars});
const checks = this.script.config.ensure;
const checkTests = EnsurePlugin.runChecks(checks, vars);
checkTests.forEach(check => {
if(check.result !== 1) {
global.artillery.log(`fail: ${check.original}${check.strict ? '': ' (optional)'}`);
if(check.strict) {
global.artillery.suggestedExitCode = 1;
}
} else {
global.artillery.log(`ok: ${check.original}`);
}
});
}
}
);
}
// Combine counters/rates/summaries into a flat key->value object for filtrex
static statsToVars(data) {
const vars = Object.assign({}, data.report.counters, data.report.rates);
for(const [name, values] of Object.entries(data.report.summaries || {})) {
for(const [aggregation, value] of Object.entries(values)) {
vars[`${name}.${aggregation}`] = value;
}
}
return vars;
}
static runChecks(checks, vars) {
const LEGACY_CONDITIONS = ['min', 'max', 'median', 'p95', 'p99'];
const checkTests = [];
if (Array.isArray(checks.thresholds)) {
checks.thresholds.forEach((o) => {
if (typeof o === 'object') {
const metricName = Object.keys(o)[0]; // only one metric check per array entry
const maxValue = o[metricName];
const expr = `${metricName} < ${maxValue}`;
let f = () => {};
try {
f = filtrex(expr);
} catch (err) {
global.artillery.log(err);
}
// all threshold checks are strict:
checkTests.push({ f, strict: true, original: expr });
}
});
}
if (Array.isArray(checks.conditions)) {
checks.conditions.forEach((o) => {
if (typeof o === 'object') {
const expression = o.expression;
const strict = typeof o.strict === 'boolean' ? o.strict : true;
let f = () => {};
try {
f = filtrex(expression);
} catch (err) {
global.artillery.log(err);
}
checkTests.push({ f, strict, original: expression });
}
});
}
Object.keys(checks)
.filter(k => LEGACY_CONDITIONS.indexOf(k) > -1)
.forEach(k => {
const metricName = `http.response_time.${k}`;
const maxValue = parseInt(checks[k]);
let f = () => {};
try {
f = filtrex(`${metricName} < ${maxValue}`);
} catch (err) {
global.artillery.log(err);
}
// all legacy threshold checks are strict:
checkTests.push({ f, strict: true, original: `${k} < ${maxValue}` });
});
if(typeof checks.maxErrorRate !== 'undefined') {
const maxValue = Number(checks.maxErrorRate);
const expression = `((vusers.created - vusers.completed)/vusers.created * 100) <= ${maxValue}`;
let f = () => {};
try {
f = filtrex(expression);
} catch (err) {
global.artillery.log(err);
}
checkTests.push({ f, strict: true, original: `maxErrorRate < ${maxValue}` });
}
if(checkTests.length > 0) {
global.artillery.log('\nChecks:');
}
checkTests.forEach(check => {
const result = check.f(vars);
check.result = result;
debug(`check ${check.original} -> ${result}`);
});
return checkTests;
}
}
module.exports = {
Plugin: EnsurePlugin,
};