Skip to content

Commit 9ca05ba

Browse files
committed
Init commit basic functionality working
1 parent efffbf3 commit 9ca05ba

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
node_modules/
3+
.DS_Store
4+
package-lock.json

hive/hive.js

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#!/usr/bin/env node
2+
const http = require('http');
3+
const fastify = require('fastify')();
4+
const request = require('request');
5+
var wasps = [];
6+
var idCount = 0;
7+
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+
}
18+
}
19+
20+
var wasp = {
21+
ip: req.ip,
22+
port: req.params.port,
23+
id: 'wasp'+idCount++
24+
}
25+
if(found == null)
26+
{
27+
wasps.push(wasp);
28+
}
29+
else
30+
{
31+
wasps[found] = wasp;
32+
}
33+
34+
35+
res.code('200').send({id:wasp.id});
36+
37+
console.log(`Wasp ${idCount-1} checking in at ${wasp.ip}!`);
38+
console.log(`Total Wasps: ${wasps.length}`)
39+
40+
})
41+
42+
fastify.get('/wasp/list', (req, res) => {
43+
res.code('200').send(wasps);
44+
})
45+
46+
fastify.put('/wasp/reportin/:id', (req, res) => {
47+
console.log(req.params.id,req.body)
48+
res.send();
49+
})
50+
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+
})
59+
}
60+
res.code(200).send('Angry wasp noises');
61+
console.log('Sending command to fire!');
62+
})
63+
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');
68+
})
69+
70+
fastify.get('/hive/status/done', (req, res) => {
71+
72+
})
73+
74+
fastify.get('/hive/status/report', (req, res) => {
75+
76+
})
77+
78+
fastify.get('/hive/status', (req, res) => {
79+
80+
})
81+
82+
fastify.listen(process.argv[3] || process.env.WWB_HIVE_PORT || 4269)

index.js

Whitespace-only changes.

package.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "waspswithbazookas",
3+
"version": "1.0.0",
4+
"description": "Its like bees with machine guns but way more power",
5+
"main": "./hive/hive.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/Phara0h/WaspsWithBazookas.git"
12+
},
13+
"keywords": [
14+
"loadtest",
15+
"aws",
16+
"bees",
17+
"with",
18+
"machineguns"
19+
],
20+
"author": "Jt Whissel",
21+
"license": "MIT",
22+
"bugs": {
23+
"url": "https://github.com/Phara0h/WaspsWithBazookas/issues"
24+
},
25+
"homepage": "https://github.com/Phara0h/WaspsWithBazookas#readme",
26+
"bin": {
27+
"wwb-hive": "./hive/hive.js",
28+
"wwb-wasp": "./wasp/wasp.js"
29+
},
30+
"dependencies": {
31+
"fastify": "^2.4.1",
32+
"request": "^2.88.0"
33+
}
34+
}

wasp/wasp.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#!/usr/bin/env node
2+
const fastify = require('fastify')();
3+
const os = require('os');
4+
const proc = require('child_process');
5+
const request = require('request');
6+
7+
var running = false;
8+
var id = 0;
9+
var hive = process.argv[2] || process.env.WWB_HIVE_URL;
10+
var port = process.argv[3] || process.env.WWB_WASP_PORT || 4268;
11+
12+
if(!hive)
13+
{
14+
console.error(`Need to set HIVE URL either through WWB_HIVE_URL env or 'wwb-wasp http://<hiveip>:<hiveport>/'`)
15+
process.exit();
16+
}
17+
else
18+
{
19+
request.get(hive+'wasp/checkin/'+port,(error, response, body) => {
20+
21+
if(error)
22+
{
23+
console.error(`Hive is not responding! ${error}`)
24+
process.exit();
25+
}
26+
else
27+
{
28+
id = JSON.parse(body).id;
29+
fastify.put('/fire', (req, res) => {
30+
if(!running)
31+
{
32+
running = true;
33+
runWRK(req.body.t,req.body.c,req.body.d,req.body.target,cmd=>{
34+
sendStats(cmd);
35+
});
36+
res.code('200').send('Rockets launching!');
37+
}
38+
else
39+
{
40+
res.code('409').send(`I'm already shooting...`);
41+
}
42+
})
43+
44+
var runWRK = function(t,c,d,target,cb)
45+
{
46+
console.log(`Shooting ${target} with bazookas!`);
47+
var cmd = {cmd:`wrk -t${t} -c${c} -d${d} ${target}`}
48+
var bat = os.platform() == 'win32' ? proc.spawn('cmd.exe', ['/c', cmd.cmd]) : proc.spawn('sh', ['-c', cmd.cmd]);
49+
cmd.bat = bat;
50+
cmd.status = 'running';
51+
cmd.response = "";
52+
bat.stdout.on('data', function(data)
53+
{
54+
cmd.response+=data;
55+
});
56+
bat.stderr.on('data', function(data)
57+
{
58+
console.log(data)
59+
cmd.response+=data;
60+
cmd.status = 'failed';
61+
});
62+
63+
bat.on('exit', code =>
64+
{
65+
if(cmd.status != 'failed')
66+
{
67+
cmd.status = 'done';
68+
console.log('Buzzz buz buz ugh, oof, I mean target destroyed!');
69+
}
70+
else
71+
{
72+
console.error('Ahhh buzzzz a rocket jam.');
73+
}
74+
cmd.bat = null;
75+
cb(cmd);
76+
});
77+
}
78+
79+
var sendStats = function(cmd)
80+
{
81+
var tmp = cmd.response.slice(cmd.response.indexOf('Latency')).replace(/\n/g,'').split(' ').filter(Boolean);
82+
83+
var stats = {
84+
latency: {
85+
avg: tmp[1],
86+
stdev: tmp[2],
87+
max: tmp[3],
88+
stdevPercent: tmp[4],
89+
},
90+
rps: {
91+
avg : tmp[6],
92+
stdev: tmp[7],
93+
max: tmp[8],
94+
stdevPercent: tmp[9],
95+
},
96+
totalRequests: tmp[10],
97+
read: tmp[14],
98+
totalRPS: tmp[16].replace('Transfer/sec:',''),
99+
tps: tmp[17]
100+
}
101+
request({
102+
method: 'PUT',
103+
uri: `${hive}wasp/reportin/${id}`,
104+
json: true,
105+
body: stats
106+
},(err,res,body)=>{
107+
if(!err)
108+
{
109+
console.log('Hive transmission complete.');
110+
}
111+
else
112+
{
113+
console.error('Hive do you read me? HIVE? Hive not responding... ');
114+
}
115+
116+
})
117+
console.log('Reporting back to hive.');
118+
}
119+
120+
121+
fastify.listen(port)
122+
console.log(id+' ready and listing for your orders!')
123+
}
124+
})
125+
}

0 commit comments

Comments
 (0)