-
Notifications
You must be signed in to change notification settings - Fork 631
/
bees.py
381 lines (281 loc) · 12.7 KB
/
bees.py
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/bin/env python
"""
The MIT License
Copyright (c) 2010 The Chicago Tribune & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from multiprocessing import Pool
import os
import re
import socket
import time
import urllib2
import boto
import boto.ec2
import paramiko
STATE_FILENAME = os.path.expanduser('~/.bees')
# Utilities
def _read_server_list():
instance_ids = []
if not os.path.isfile(STATE_FILENAME):
return (None, None, None, None)
with open(STATE_FILENAME, 'r') as f:
username = f.readline().strip()
key_name = f.readline().strip()
zone = f.readline().strip()
text = f.read()
instance_ids = text.split('\n')
print 'Read %i bees from the roster.' % len(instance_ids)
return (username, key_name, zone, instance_ids)
def _write_server_list(username, key_name, zone, instances):
with open(STATE_FILENAME, 'w') as f:
f.write('%s\n' % username)
f.write('%s\n' % key_name)
f.write('%s\n' % zone)
f.write('\n'.join([instance.id for instance in instances]))
def _delete_server_list():
os.remove(STATE_FILENAME)
def _get_pem_path(key):
return os.path.expanduser('~/.ssh/%s.pem' % key)
def _get_region(zone):
return zone[:-1] # chop off the "d" in the "us-east-1d" to get the "Region"
# Methods
def up(count, group, zone, image_id, instance_type, username, key_name, subnet):
"""
Startup the load testing server.
"""
existing_username, existing_key_name, existing_zone, instance_ids = _read_server_list()
if instance_ids:
print 'Bees are already assembled and awaiting orders.'
return
count = int(count)
pem_path = _get_pem_path(key_name)
if not os.path.isfile(pem_path):
print 'No key file found at %s' % pem_path
return
print 'Connecting to the hive.'
ec2_connection = boto.ec2.connect_to_region(_get_region(zone))
print 'Attempting to call up %i bees.' % count
if not subnet:
reservation = ec2_connection.run_instances(
image_id=image_id,
min_count=count,
max_count=count,
key_name=key_name,
security_groups=[group],
instance_type=instance_type,
placement=zone)
else:
reservation = ec2_connection.run_instances(
image_id=image_id,
min_count=count,
max_count=count,
key_name=key_name,
security_group_ids=[group],
instance_type=instance_type,
placement=zone,
subnet_id=subnet)
print 'Waiting for bees to load their machine guns...'
instance_ids = []
for instance in reservation.instances:
instance.update()
while instance.state != 'running':
print '.'
time.sleep(5)
instance.update()
instance_ids.append(instance.id)
print 'Bee %s is ready for the attack.' % instance.id
ec2_connection.create_tags(instance_ids, { "Name": "a bee!" })
_write_server_list(username, key_name, zone, reservation.instances)
print 'The swarm has assembled %i bees.' % len(reservation.instances)
def report():
"""
Report the status of the load testing servers.
"""
username, key_name, zone, instance_ids = _read_server_list()
if not instance_ids:
print 'No bees have been mobilized.'
return
ec2_connection = boto.ec2.connect_to_region(_get_region(zone))
reservations = ec2_connection.get_all_instances(instance_ids=instance_ids)
instances = []
for reservation in reservations:
instances.extend(reservation.instances)
for instance in instances:
print 'Bee %s: %s @ %s' % (instance.id, instance.state, instance.ip_address)
def down():
"""
Shutdown the load testing server.
"""
username, key_name, zone, instance_ids = _read_server_list()
if not instance_ids:
print 'No bees have been mobilized.'
return
print 'Connecting to the hive.'
ec2_connection = boto.ec2.connect_to_region(_get_region(zone))
print 'Calling off the swarm.'
terminated_instance_ids = ec2_connection.terminate_instances(
instance_ids=instance_ids)
print 'Stood down %i bees.' % len(terminated_instance_ids)
_delete_server_list()
def _attack(params):
"""
Test the target URL with requests.
Intended for use with multiprocessing.
"""
print 'Bee %i is joining the swarm.' % params['i']
try:
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(
params['instance_name'],
username=params['username'],
key_filename=_get_pem_path(params['key_name']))
print 'Bee %i is firing his machine gun. Bang bang!' % params['i']
options = ''
if params['headers'] is not '':
for h in params['headers'].split(';'):
options += ' -H "%s"' % h
if params['post_file']:
os.system("scp -q -o 'StrictHostKeyChecking=no' %s %s@%s:/tmp/honeycomb" % (params['post_file'], params['username'], params['instance_name']))
options += ' -k -T "%(mime_type)s; charset=UTF-8" -p /tmp/honeycomb' % params
benchmark_command = 'ab -r -n %(num_requests)s -c %(concurrent_requests)s -C "sessionid=NotARealSessionID" %(options) "%(url)s"' % params
stdin, stdout, stderr = client.exec_command(benchmark_command)
response = {}
ab_results = stdout.read()
ms_per_request_search = re.search('Time\ per\ request:\s+([0-9.]+)\ \[ms\]\ \(mean\)', ab_results)
if not ms_per_request_search:
print 'Bee %i lost sight of the target (connection timed out).' % params['i']
return None
requests_per_second_search = re.search('Requests\ per\ second:\s+([0-9.]+)\ \[#\/sec\]\ \(mean\)', ab_results)
failed_requests = re.search('Failed\ requests:\s+([0-9.]+)', ab_results)
fifty_percent_search = re.search('\s+50\%\s+([0-9]+)', ab_results)
ninety_percent_search = re.search('\s+90\%\s+([0-9]+)', ab_results)
complete_requests_search = re.search('Complete\ requests:\s+([0-9]+)', ab_results)
response['ms_per_request'] = float(ms_per_request_search.group(1))
response['requests_per_second'] = float(requests_per_second_search.group(1))
response['failed_requests'] = float(failed_requests.group(1))
response['fifty_percent'] = float(fifty_percent_search.group(1))
response['ninety_percent'] = float(ninety_percent_search.group(1))
response['complete_requests'] = float(complete_requests_search.group(1))
print 'Bee %i is out of ammo.' % params['i']
client.close()
return response
except socket.error, e:
return e
def _print_results(results):
"""
Print summarized load-testing results.
"""
timeout_bees = [r for r in results if r is None]
exception_bees = [r for r in results if type(r) == socket.error]
complete_bees = [r for r in results if r is not None and type(r) != socket.error]
num_timeout_bees = len(timeout_bees)
num_exception_bees = len(exception_bees)
num_complete_bees = len(complete_bees)
if exception_bees:
print ' %i of your bees didn\'t make it to the action. They might be taking a little longer than normal to find their machine guns, or may have been terminated without using "bees down".' % num_exception_bees
if timeout_bees:
print ' Target timed out without fully responding to %i bees.' % num_timeout_bees
if num_complete_bees == 0:
print ' No bees completed the mission. Apparently your bees are peace-loving hippies.'
return
complete_results = [r['complete_requests'] for r in complete_bees]
total_complete_requests = sum(complete_results)
print ' Complete requests:\t\t%i' % total_complete_requests
complete_results = [r['failed_requests'] for r in complete_bees]
total_failed_requests = sum(complete_results)
print ' Failed requests:\t\t%i' % total_failed_requests
complete_results = [r['requests_per_second'] for r in complete_bees]
mean_requests = sum(complete_results)
print ' Requests per second:\t%f [#/sec] (mean)' % mean_requests
complete_results = [r['ms_per_request'] for r in complete_bees]
mean_response = sum(complete_results) / num_complete_bees
print ' Time per request:\t\t%f [ms] (mean)' % mean_response
complete_results = [r['fifty_percent'] for r in complete_bees]
mean_fifty = sum(complete_results) / num_complete_bees
print ' 50%% response time:\t\t%f [ms] (mean)' % mean_fifty
complete_results = [r['ninety_percent'] for r in complete_bees]
mean_ninety = sum(complete_results) / num_complete_bees
print ' 90%% response time:\t\t%f [ms] (mean)' % mean_ninety
if mean_response < 500:
print 'Mission Assessment: Target crushed bee offensive.'
elif mean_response < 1000:
print 'Mission Assessment: Target successfully fended off the swarm.'
elif mean_response < 1500:
print 'Mission Assessment: Target wounded, but operational.'
elif mean_response < 2000:
print 'Mission Assessment: Target severely compromised.'
else:
print 'Mission Assessment: Swarm annihilated target.'
def attack(url, n, c, **options):
"""
Test the root url of this site.
"""
username, key_name, zone, instance_ids = _read_server_list()
if not instance_ids:
print 'No bees are ready to attack.'
return
print 'Connecting to the hive.'
ec2_connection = boto.ec2.connect_to_region(_get_region(zone))
print 'Assembling bees.'
reservations = ec2_connection.get_all_instances(instance_ids=instance_ids)
instances = []
for reservation in reservations:
instances.extend(reservation.instances)
instance_count = len(instances)
if n < instance_count * 2:
print 'bees: error: the total number of requests must be at least %d (2x num. instances)' % (instance_count * 2)
return
if c < instance_count:
print 'bees: error: the number of concurrent requests must be at least %d (num. instances)' % instance_count
return
if n < c:
print 'bees: error: the number of concurrent requests (%d) must be at most the same as number of requests (%d)' % (c, n)
return
requests_per_instance = int(float(n) / instance_count)
connections_per_instance = int(float(c) / instance_count)
print 'Each of %i bees will fire %s rounds, %s at a time.' % (instance_count, requests_per_instance, connections_per_instance)
params = []
for i, instance in enumerate(instances):
params.append({
'i': i,
'instance_id': instance.id,
'instance_name': instance.public_dns_name,
'url': url,
'concurrent_requests': connections_per_instance,
'num_requests': requests_per_instance,
'username': username,
'key_name': key_name,
'headers': options.get('headers', ''),
'post_file': options.get('post_file'),
'mime_type': options.get('mime_type', ''),
})
print 'Stinging URL so it will be cached for the attack.'
# Ping url so it will be cached for testing
dict_headers = {}
if headers is not '':
dict_headers = headers = dict(h.split(':') for h in headers.split(';'))
request = urllib2.Request(url, headers=dict_headers)
urllib2.urlopen(request).read()
print 'Organizing the swarm.'
# Spin up processes for connecting to EC2 instances
pool = Pool(len(params))
results = pool.map(_attack, params)
print 'Offensive complete.'
_print_results(results)
print 'The swarm is awaiting new orders.'