-
Notifications
You must be signed in to change notification settings - Fork 0
/
scenarioProcessor.js
149 lines (126 loc) · 4.12 KB
/
scenarioProcessor.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
// each time you change something inside processor you need to redeploy Lambda
const Influx = require("influx");
const throttle = require("lodash/throttle");
const get = require("lodash/get");
const faker = require("faker");
const DEBUG = true;
const influx = new Influx.InfluxDB({
// here we create an InfluxDB client, it works great with Graphana visualisation tool
host: process.env.INFLUX_HOST,
username: process.env.INFLUX_USER,
password: process.env.INFLUX_PASSWORD,
database: process.env.INFLUX_DB,
schema: [
{
measurement: "RequestSent",
fields: {
url: Influx.FieldType.STRING
},
tags: []
},
{
measurement: "Errors",
fields: {
step: Influx.FieldType.STRING,
errorType: Influx.FieldType.STRING
},
tags: []
}
]
});
let points = []; // this is an array for data points we send to DB later
sendPointsToInflux = () => {
// here we send our data to DB
influx
.writePoints(points, {
precision: "ms"
})
.catch(err => {
console.log(`Error saving data to InfluxDB! ${err.stack}`);
});
points = []; // after sending points to DB we clean the array to avoid sending the same points again
console.log("Sent requests reported to InfluxDB.");
};
const sendDataToDbEveryMs = 10000;
const throttledSendPointsToInflux = throttle(
sendPointsToInflux,
sendDataToDbEveryMs,
{
trailing: true,
leading: false
}
); // we create a throttled DB interface to prevent DB from choaking on connections
module.exports.writeDataToDB = (context, ee, next) => {
sendPointsToInflux();
return next(); // we need to call next to progress in the scenario
};
module.exports.generateNewUserEmailInFunction = (context, ee, next) => {
// here we use faker to generate random user data
const randomFirstName = faker.name.firstName();
const randomLastName = faker.name.lastName();
const randomString = faker.random.uuid().substring(0, 8);
const newUserEmail = `${randomFirstName}.${randomLastName}-${randomString}@testDomain.com`;
console.log("newUserEmail", newUserEmail);
context.vars.firstName = randomFirstName; // context is shared between steps during the whole scenario run
context.vars.lastName = randomLastName;
context.vars.newUserEmail = newUserEmail;
return next();
};
module.exports.logRequestSentToInflux = (requestParams, context, ee, next) => {
// here we log request send timestamp
points.push({
measurement: "RequestSent",
fields: { url: requestParams.url },
tags: {},
timestamp: new Date().getTime()
});
throttledSendPointsToInflux();
return next();
};
// --------- STEP HOOKS ---------
module.exports.logRequestToConsole = (requestParams, context, ee, next) => {
// here we run a custom hook to log data used in request
// requestParams holds data that will be sent over HTTP
// context holds Artillery configuration and variables
// ee is an EventEmitter which can be used to hook additional functions to HTTP engine
if (DEBUG) {
console.log("requestParams", requestParams);
}
return next();
};
module.exports.logResponseToConsole = (
requestParams,
response,
context,
ee,
next
) => {
// this is a hook after we get the response, we have an additional parameter response here
const responseJSON = JSON.parse(response.body);
const errors = get(responseJSON, "data.errors");
if (errors) {
const statusCode = get(responseJSON, "data.errors[0].statusCode");
console.log("ERROR statusCode", statusCode);
}
if (DEBUG) {
console.log("response.body", response.body);
}
return next();
};
module.exports.logErrorsInStep = (requestParams, context, ee, next) => {
// here we define an EventEmitter with on error event handler to log errors to DB
ee.on("error", handleStepErrors);
return next();
};
// --------- END STEP HOOKS ---------
// --------- ERROR HANDLERS ---------
const handleStepErrors = error => {
console.log("error in HTTP request", error);
points.push({
measurement: "Errors",
fields: { step: "getReservation", errorType: error },
tags: {},
timestamp: new Date().getTime()
});
};
// --------- END ERROR HANDLERS ---------