-
Notifications
You must be signed in to change notification settings - Fork 0
/
net_stresser.py
84 lines (67 loc) · 2.92 KB
/
net_stresser.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
#! /usr/bin/env python2
import multiprocessing as mp
import argparse
import subprocess
import time
import os
from random import randint
parser = argparse.ArgumentParser(description="Stress the VM network stack")
parser.add_argument('--host', dest="host")
parser.add_argument('--duration', type=int, dest="duration", default="20")
parser.add_argument('--ports', type=int, dest="ports", default="20")
parser.add_argument('--server', dest="server", action="store_true")
parser.add_argument('--bandwidth', type=int, dest="bandwidth", default="62500")
args = parser.parse_args()
DEVNULL = open(os.devnull, 'w')
def run_iperf_client(bandwidth, host, duration, port):
iperf_cmd = '/usr/bin/trickle -u {0} -d {0} iperf -c {1} -t {2} -p {3} --reportstyle=c'.format(bandwidth, host, duration, port)
subprocess.check_call(iperf_cmd.split(), stdout=DEVNULL, stderr=DEVNULL)
def run_iperf_server(port):
iperf_cmd = '/usr/bin/iperf -s -p {0} --reportstyle=c'.format(port)
subprocess.check_call(iperf_cmd.split(), stdout=DEVNULL, stderr=DEVNULL)
def run_ping(host, count):
ping_cmd = '/bin/ping -c {0} {1}'.format(count, host)
subprocess.check_call(ping_cmd.split())
def run_ping_server(host):
ping_cmd = '/bin/ping {0}'.format(host)
subprocess.check_call(ping_cmd.split())
def calculate_mb_max_bandwidth(processes, bandwidth):
return (8 * processes * bandwidth) / 1000
def main():
processes = []
if args.server is False:
for i in range(args.ports):
p = mp.Process(target=run_iperf_client, args=(args.bandwidth, args.host, randint(1, 9), 5001 + i))
p.start()
processes.append(p)
timer=0
p = mp.Process(target=run_ping, args=(args.host, args.duration))
p.start()
while(timer < args.duration):
alive_processes = 0
started_processes = 0
for i in range(len(processes)):
if processes[i].is_alive():
alive_processes += 1
elif timer % 5 == 0:
del processes[i]
length = randint(1, 9)
p = mp.Process(target=run_iperf_client, args=(args.bandwidth, args.host, length, 5001 + i))
p.start()
processes.insert(i, p)
started_processes += 1
print "Alive processes:", alive_processes
print "Started", started_processes, "processes."
print "Max bandwidth:", calculate_mb_max_bandwidth(alive_processes, args.bandwidth), "Mb/s"
timer += 1
time.sleep(1)
else:
for i in range(args.ports):
p = mp.Process(target=run_iperf_server, args=(5001 + i,))
p.start()
processes.append(p)
p = mp.Process(target=run_ping_server, args=(args.host,))
p.start()
processes.append(p)
if __name__ == '__main__':
main()