-
Notifications
You must be signed in to change notification settings - Fork 0
/
artillery-delegate.js
286 lines (247 loc) · 10 KB
/
artillery-delegate.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
const fs = require('fs');
const http = require('http');
const exec = require('child_process').exec;
const ARTILLERY_PROVIDER_HOST = process.env.ARTILLERY_PROVIDER_HOST || "localhost";
const ARTILLERY_PROVIDER_PORT = process.env.ARTILLERY_PROVIDER_PORT || 8081;
const PING_PATH = '/dialogflow';
const ARTILLERY_YAML_DOWNLOAD_PATH = '/dialogflow/artillery/yaml/';
const ARTILLERY_OUTPUT_PATH = '/dialogflow/artillery/output/';
const SCRIPTS_PATH = process.env.SCRIPTS_PATH || './scripts/';
const OUTPUT_PATH = process.env.OUTPUT_PATH || './output/';
const TEST_PREFIX = process.env.TEST_PREFIX || 'TEST-';;
const CONVERSATION_IDS_FILE = process.env.CONVERSATION_IDS_FILE || "/conversation-ids.csv";
class ArtilleryDelegate {
deepPing() {
return new Promise((resolve, reject) => {
const host = ARTILLERY_PROVIDER_HOST;
const port = ARTILLERY_PROVIDER_PORT;
const path = PING_PATH;
const post_req = http.request({
host: host,
port: port,
path: path,
method: 'GET'
}, function (res) {
var responseString = "";
res.setEncoding('utf8');
res.on("data", function (data) {
responseString += data;
});
res.on('error', function (err) {
reject({
error: "Unable to contact Artillery provider at " + host + ":" + port
});
});
res.on("end", function () {
try {
const response = JSON.parse(responseString);
resolve({ message: "OK", dialogflow: response });
} catch (err) {
resolve({ message: "OK", dialogflow: { message: responseString } });
}
});
});
post_req.on('error', (error) => {
const message = "Unable ping artillery provider at at " + host + ":" + port;
console.log("ERROR " + message, error)
resolve({ message: "OK", dialogflow: { message: message } });
})
post_req.end();
})
}
downloadYaml(projectId, scriptId) {
return new Promise((resolve, reject) => {
const host = ARTILLERY_PROVIDER_HOST;
const port = ARTILLERY_PROVIDER_PORT;
const path = ARTILLERY_YAML_DOWNLOAD_PATH + projectId + '/' + scriptId;
const post_req = http.request({
host: host,
port: port,
path: path,
method: 'GET'
}, function (res) {
var responseString = "";
res.setEncoding('utf8');
res.on("data", function (data) {
responseString += data;
});
res.on('error', function (err) {
reject({ error: "Unable to contact Artillery provider at " + host + ":" + port })
});
res.on("end", function () {
const yaml = responseString;
this.writeYaml(yaml, projectId, scriptId);
this.writeConversationIds(projectId, 100);
resolve({ message: "OK-1" })
});
});
post_req.on('error', (error) => {
const message = "Error contacting Artillery Provider at " + host + ":" + port;
console.log("ERROR " + message, error);
reject({ error: message })
})
post_req.end();
})
}
writeYaml(yaml, projectId, scriptId) {
const dir = SCRIPTS_PATH + projectId;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const filepath = dir + "/" + scriptId + ".yml";
fs.writeFileSync(filepath, yaml)
}
writeConversationIds(projectId, number) {
const ids = [];
for (let i = 0; i < number; i++) {
ids.push(TEST_PREFIX + this.getRandomString());
}
const output = ids.join("\n");
const dir = SCRIPTS_PATH + projectId;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const filepath = dir + CONVERSATION_IDS_FILE;
fs.writeFileSync(filepath, output)
}
getRandomString() {
return Math.random().toString(36).substring(7);
}
executeYaml(yamlRequest) {
return new Promise((resolve, reject) => {
const projectId = yamlRequest.projectId; //sanitize this!!
const scriptId = yamlRequest.scriptId; //sanitize this!!
const time = yamlRequest.time; //sanitize this!!
const target = yamlRequest.target; //sanitize this!!
const ipAddress = yamlRequest.ipAddress;
const filepath = this.getYamlPath(projectId, scriptId);
const dir = OUTPUT_PATH + projectId;
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
const fileprefix = dir + '/' + scriptId + '-' + time;
const outputPath = fileprefix + '.out';
const metaPath = fileprefix + '.json';
const output = {
delegateId: process.env.DELEGATE_ID,
ipAddress,
projectId,
scriptId,
time,
target,
outputPath,
metaPath
}
let scripttext = 'artillery run ' + filepath + ' --output ' + outputPath;
if (target) {
scripttext = scripttext + ' --target ' + target;
}
exec(scripttext,
(error, stdout, stderr) => {
const now = (new Date()).getTime();
output.completed = now;
console.log(`${stdout}`);
console.log(`${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
fs.writeFileSync(metaPath, JSON.stringify(output), 'utf-8');
//uploadResults(projectId,scriptId,output,outputPath);
});
resolve({ message: "OK-EXECUTE", output });
})
}
getYamlPath(projectId, scriptId) {
const dir = SCRIPTS_PATH + projectId;
const filepath = dir + "/" + scriptId + ".yml";
return filepath;
}
listOutput(projectId) {
return new Promise((resolve, reject) => {
const path = OUTPUT_PATH + projectId;
if (!fs.existsSync(path)) {
resolve({ output: [] });
} else {
let output = [];
fs.readdir(path, function (err, items) {
if (err) {
console.log(err);
reject({ error: err });
}
if (items) {
items.filter(item => {
return item.endsWith('.json');
})
.map((filename) => {
const object = require(path + "/" + filename);
output.push(object);
});
resolve({ output: output });
} else {
resolve({ output: [] });
}
});
}
})
}
uploadOutput(projectId, scriptId, time) {
return new Promise((resolve, reject) => {
const host = ARTILLERY_PROVIDER_HOS;
const port = ARTILLERY_PROVIDER_PORT;
const path = ARTILLERY_OUTPUT_PATH + projectId + '/' + scriptId;
const fileprefix = OUTPUT_PATH + projectId + '/' + scriptId + '-' + time;
const outputPath = fileprefix + '.out';
const metaPath = fileprefix + ".json";
const exec = fs.readFileSync(outputPath, 'utf-8');
const meta = require(metaPath);
const post_obj = {
meta: meta,
exec: exec
}
const post_data = JSON.stringify(post_obj)
const post_req = http.request({
host: host,
port: port,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(post_data)
}
}, function (res) {
res.setEncoding('utf8');
let responseString = "";
res.on("data", function (data) {
responseString += data;
});
res.on('error', function (err) {
reject({
error: "Unable to contact Artillery provider at " + host + ":" + port
});
});
res.on("end", function () {
meta.uploaded = (new Date()).getTime();
console.log("UPLOADED", meta);
fs.writeFile(metaPath, JSON.stringify(meta), 'utf-8', (err) => {
if (err) {
reject({
error: "Unable to contact Artillery provider at " + host + ":" + port
});
} else {
console.log("WROTE UPDATED", metaPath);
resolve({ uploaded: meta.uploaded })
}
});
});
});
post_req.on('error', (error) => {
const message = "Error uploading to Artillery provider " + host + ":" + port
console.log("ERROR " + message, error)
reject({ error: error });
})
post_req.write(post_data);
post_req.end();
})
}
}
module.exports = { artilleryDelegate: new ArtilleryDelegate() }