-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththroughput.py
70 lines (55 loc) · 2.15 KB
/
throughput.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
import subprocess
import threading
def get_rx_tx_files(iface):
rx = ''.join(['/sys/class/net/',str(iface),'/statistics/rx_bytes'])
tx = ''.join(['/sys/class/net/',str(iface),'/statistics/tx_bytes'])
return rx, tx
def get_rx_tx_stats((rx_file, tx_file)):
try:
with open(rx_file, 'r') as rf, open(tx_file, 'r') as tf:
rx = ''.join(rf.readline().split())
tx = ''.join(tf.readline().split())
return (rx, tx)
except Exception as e:
print ''.join(['Stats Err: ', str(e)])
return (0, 0)
def launch_process(cmd, success, failure, out=lambda x: x.stdout.read(1)):
print ' '.join(cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
if p == None:
return None
poutput = ''
try:
while True:
o = out(p)
if o == '' and p.poll() != None:
return None
poutput = ''.join([poutput, o])
if any(connect in poutput for connect in success):
return p
elif any(fail in poutput for fail in failure):
return None
except Exception as e:
print e
return None
def launch_axel(url, threads=1):
cmd = ['axel', '-a', '-n', str(threads), url]
return launch_process(cmd, ('Starting', ), ('Unable', ),
out=lambda x: x.stdout.read(1))
def launch_aria(url, threads=1):
cmd = ['aria2c', '-x', str(threads), url]
return launch_process(cmd, ('connected', ), ('refused', 'failed'),
out=lambda x: x.stdout.read(1))
def launch_iperf(server_ip, time, bind=None, threads=1, wnd=64, wlen=8):
if threads <= 0:
threads = 1
cmd = ['iperf', '-c', server_ip, '-t', str(time), '-P', str(threads)]
if bind is not None:
cmd += ['-P', str(threads)]
return launch_process(cmd, ('connected', ), ('refused', 'failed'))
def launch_wget(url, bind):
cmd = ['wget', url]
if bind is not None:
cmd += ['--bind-address=', str(bind)]
return launch_process(cmd, ('connected', ), ('refused', 'failed'),
out=lambda x: x.stderr.read(1))