Skip to content

Commit dace65c

Browse files
committed
WRK scripts now work, reporting and the rest of the functionality
1 parent 9ca05ba commit dace65c

File tree

5 files changed

+486
-136
lines changed

5 files changed

+486
-136
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
# WaspsWithBazookas
22
Its like bees with machine guns but way more power
3+
4+
Postman REST Docs
5+
[Postman REST Docs](https://documenter.getpostman.com/view/7072151/S1TR4zsf)

hive/hive.js

Lines changed: 307 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,342 @@
11
#!/usr/bin/env node
2+
23
const http = require('http');
34
const fastify = require('fastify')();
45
const request = require('request');
6+
var convert = require('convert-units');
7+
8+
var running = false;
59
var wasps = [];
10+
var waspDoneCount = 0;
11+
var waspsRunningCount = 0;
12+
var runTimeStamp = 0;
613
var idCount = 0;
14+
var report = null;
715

8-
fastify.get('/wasp/checkin/:port', (req, res) => {
9-
10-
var found = null;
11-
for (var i = 0; i < wasps.length; i++) {
12-
if(wasps[i].ip == req.ip && wasps[i].port == req.params.port)
13-
{
14-
found = i;
15-
console.log(found)
16-
break;
17-
}
16+
fastify.get('/wasp/checkin/:port', (req, res) =>
17+
{
18+
var found = null;
19+
for(var i = 0; i < wasps.length; i++)
20+
{
21+
if(wasps[i].ip == req.ip && wasps[i].port == req.params.port)
22+
{
23+
found = i;
24+
break;
1825
}
26+
}
1927

20-
var wasp = {
21-
ip: req.ip,
22-
port: req.params.port,
23-
id: 'wasp'+idCount++
24-
}
25-
if(found == null)
28+
var wasp = {
29+
ip: req.ip,
30+
port: req.params.port,
31+
id: 'wasp' + idCount++
32+
}
33+
if(found == null)
34+
{
35+
wasps.push(wasp);
36+
}
37+
else
38+
{
39+
wasps[found] = wasp;
40+
}
41+
42+
43+
res.code('200').send(
44+
{
45+
id: wasp.id
46+
});
47+
48+
console.log(`Wasp ${idCount-1} checking in at ${wasp.ip}!`);
49+
console.log(`Total Wasps: ${wasps.length}`)
50+
51+
})
52+
53+
fastify.get('/wasp/list', (req, res) =>
54+
{
55+
res.code('200').send(wasps);
56+
})
57+
58+
fastify.put('/wasp/reportin/:id', (req, res) =>
59+
{
60+
var wasp = wasps.find(w =>
61+
{
62+
return w.id == req.params.id;
63+
});
64+
65+
if(wasp)
66+
{
67+
waspDoneCount++;
68+
report.wasp.reports.push(
69+
{
70+
wasp: wasp,
71+
status: 'complete',
72+
stats: req.body
73+
})
74+
report.status.completed += 1;
75+
report.totalRPS += req.body.totalRPS;
76+
report.read += req.body.read;
77+
report.totalRequests += req.body.totalRequests;
78+
report.tps += req.body.tps;
79+
report.errors.connect += req.body.errors.connect || 0;
80+
report.errors.read += req.body.errors.read || 0;
81+
report.errors.write += req.body.errors.write || 0;
82+
report.errors.timeout += req.body.errors.timeout || 0;
83+
84+
report.nonSuccessRequest += req.body.nonSuccessRequests;
85+
86+
res.send();
87+
}
88+
else
89+
{
90+
gError('/wasp/reportin/:id', res);
91+
}
92+
if(waspDoneCount == waspsRunningCount)
93+
{
94+
genReport();
95+
}
96+
})
97+
98+
fastify.put('/wasp/reportin/:id/failed', (req, res) =>
99+
{
100+
101+
var wasp = wasps.find(w =>
102+
{
103+
return w.id == req.params.id;
104+
});
105+
106+
if(wasp)
107+
{
108+
waspDoneCount++;
109+
report.wasp.reports.push(
26110
{
27-
wasps.push(wasp);
111+
wasp: wasp,
112+
status: 'failed',
113+
error: req.body
114+
});
115+
report.status.failed += 1;
116+
117+
res.send();
118+
}
119+
else
120+
{
121+
gError('/wasp/reportin/:id/failed', res);
122+
}
123+
if(waspDoneCount == waspsRunningCount)
124+
{
125+
genReport();
126+
}
127+
128+
})
129+
130+
fastify.put('/hive/poke', (req, res) =>
131+
{
132+
if(!isRunningRes(res))
133+
{
134+
if(!req.body.target)
135+
{
136+
res.code(400).send('need a target, cant shoot into the darkness...')
28137
}
29138
else
30139
{
31-
wasps[found] = wasp;
32-
}
33-
140+
req.body.t = req.body.t || 10;
141+
req.body.c = req.body.c || 50;
142+
req.body.d = req.body.d || 30;
34143

35-
res.code('200').send({id:wasp.id});
144+
for(var i = 0; i < wasps.length; i++)
145+
{
146+
request(
147+
{
148+
method: 'PUT',
149+
uri: `http://${wasps[i].ip}:${wasps[i].port}/fire`,
150+
json: true,
151+
body: req.body
152+
})
153+
}
36154

37-
console.log(`Wasp ${idCount-1} checking in at ${wasp.ip}!`);
38-
console.log(`Total Wasps: ${wasps.length}`)
155+
res.code(200).send('Angry wasp noises');
156+
console.log('Sending command to fire!');
157+
setRunning(true);
39158

159+
//shit went down if they don't all respond in duration + 5 seconds
160+
setTimeout(() =>
161+
{
162+
if(running)
163+
{
164+
genReport();
165+
}
166+
}, (req.body.d + 5) * 1000);
167+
}
168+
}
40169
})
41170

42-
fastify.get('/wasp/list', (req, res) => {
43-
res.code('200').send(wasps);
171+
fastify.delete('/hive/torch', (req, res) =>
172+
{
173+
res.code(200).send(`R.I.P All ${wasps.length} wasps. :'(`);
174+
wasps = [];
175+
console.log('f');
44176
})
45177

46-
fastify.put('/wasp/reportin/:id', (req, res) => {
47-
console.log(req.params.id,req.body)
48-
res.send();
178+
fastify.get('/hive/status/done', (req, res) =>
179+
{
180+
if(!isRunningRes(res))
181+
{
182+
res.code(200).send('done');
183+
}
49184
})
50185

51-
fastify.put('/hive/poke', (req, res) => {
52-
for (var i = 0; i < wasps.length; i++) {
53-
request({
54-
method: 'PUT',
55-
uri: `http://${wasps[i].ip}:${wasps[i].port}/fire`,
56-
json: true,
57-
body: req.body
58-
})
186+
fastify.get('/hive/status/report', (req, res) =>
187+
{
188+
if(!isRunningRes(res))
189+
{
190+
if(report)
191+
{
192+
res.code(200).send(report);
193+
}
194+
else
195+
{
196+
res.code(400).send('No report yet.');
197+
}
198+
59199
}
60-
res.code(200).send('Angry wasp noises');
61-
console.log('Sending command to fire!');
62200
})
63201

64-
fastify.delete('/hive/torch', (req, res) => {
65-
res.code(200).send(`R.I.P All ${wasps.length} wasps. :'('`);
66-
wasps = [];
67-
console.log('f');
202+
fastify.get('/hive/status/report/:val', (req, res) =>
203+
{
204+
if(!isRunningRes(res))
205+
{
206+
if(report && report[req.params.val])
207+
{
208+
res.code(200).send(report[req.params.val]);
209+
}
210+
else
211+
{
212+
res.code(400).send('No hive information on that.');
213+
}
214+
}
68215
})
69216

70-
fastify.get('/hive/status/done', (req, res) => {
71217

218+
219+
fastify.get('/hive/status', (req, res) =>
220+
{
221+
if(!isRunningRes(res, 200))
222+
{
223+
res.code(200).send(`Hive is oprational with ${wasps.length} wasps ready and waiting orders.`);
224+
}
72225
})
73226

74-
fastify.get('/hive/status/report', (req, res) => {
75227

76-
})
228+
var isRunningRes = function(res, code)
229+
{
230+
if(running)
231+
{
232+
res.code(code || 425).send(((waspDoneCount / waspsRunningCount) * 100) + "% complete, eta " + ((Number(process.hrtime.bigint()) / 1000000) - runTimeStamp) + "ms to go.");
233+
return true;
234+
}
235+
return false;
236+
}
77237

78-
fastify.get('/hive/status', (req, res) => {
238+
var setRunning = function(run)
239+
{
240+
if(run)
241+
{
242+
running = true;
243+
runTimeStamp = Number(process.hrtime.bigint()) / 1000000;
244+
report = {
245+
wasp:
246+
{
247+
reports: []
248+
},
249+
status:
250+
{
251+
completed: 0,
252+
failed: 0
253+
},
254+
latency:
255+
{
256+
avg: 0,
257+
max: 0,
258+
},
259+
rps:
260+
{
261+
avg: 0,
262+
max: 0,
263+
},
79264

80-
})
265+
totalRPS: 0,
266+
totalRequests: 0,
267+
read: 0,
268+
tps: 0,
269+
nonSuccessRequests: 0,
270+
errors:
271+
{
272+
connect: 0,
273+
read: 0,
274+
write: 0,
275+
timeout: 0
276+
},
277+
};
278+
279+
waspsRunningCount = wasps.length;
280+
}
281+
else
282+
{
283+
runTimeStamp = 0;
284+
waspDoneCount = 0;
285+
waspsRunningCount = 0;
286+
running = false;
287+
}
288+
}
289+
290+
var genReport = function()
291+
{
292+
console.log(`Reports are in lets see how they are.`);
293+
for(var i = 0; i < report.wasp.reports.length; i++)
294+
{
295+
var wasp = report.wasp.reports[i];
296+
if(wasp.stats)
297+
{
298+
report.latency.avg += wasp.stats.latency.avg;
299+
report.rps.avg += wasp.stats.rps.avg;
300+
301+
if(wasp.stats.latency.max > report.latency.max)
302+
{
303+
report.latency.max = wasp.stats.latency.max;
304+
}
305+
if(wasp.stats.rps.max > report.rps.max)
306+
{
307+
report.rps.max = wasp.stats.rps.max;
308+
}
309+
}
310+
}
311+
report.latency.avg = wasp.stats.latency.avg / report.status.completed;
312+
report.rps.avg = wasp.stats.rps.avg / report.status.completed;
313+
report.read = (
314+
{
315+
val,
316+
unit
317+
} = convert(report.read).from('B').toBest(),
318+
{
319+
val,
320+
unit
321+
});
322+
report.tps = (
323+
{
324+
val,
325+
unit
326+
} = convert(report.tps).from('B').toBest(),
327+
{
328+
val,
329+
unit
330+
});
331+
332+
setRunning(false);
333+
}
334+
335+
var gError = function(route, res)
336+
{
337+
res.code(412).send(`I'm a little wasp`);
338+
console.log(`Bad thingz happened in the ${route} sectorz.`);
339+
}
81340

341+
console.log('Hive ready to release the wasps!')
82342
fastify.listen(process.argv[3] || process.env.WWB_HIVE_PORT || 4269)

0 commit comments

Comments
 (0)