iperf-20120304.py
1 |
# Laurent GUERBY 20120304
|
---|---|
2 |
# on server 91.224.148.1
|
3 |
# while true; do date;python iperf.py -s 5555 ; sleep 1; done
|
4 |
# on client source route from 192.168.88.221
|
5 |
# T=t1; for i in 5 8 ; do for j in 400 1200; do N=z-$T-$i-$j.csv; python iperf.py -a 192.168.88.221 -c 91.224.148.1:5555 -d ${i}.0e6 -l $j -n 2000 >& $N; tail -5 $N; sleep 1; done;done
|
6 |
|
7 |
import time |
8 |
import os |
9 |
import sys |
10 |
import socket |
11 |
import select |
12 |
import struct |
13 |
import random |
14 |
|
15 |
N=256*256*256*256-1 |
16 |
S=160000
|
17 |
random.seed(0)
|
18 |
random_s="".join([struct.pack("I",random.randint(0,N)) for i in xrange(S/4)]) |
19 |
|
20 |
host=None
|
21 |
port=None
|
22 |
addr=""
|
23 |
mode=None
|
24 |
RBUFL=2000
|
25 |
BYE="BYE"
|
26 |
PL=1200 # packet of 1200 bytes content |
27 |
TR=1.0e6 # Mbit/s |
28 |
NP=1000 # number of packets |
29 |
|
30 |
opt_l=sys.argv[1:]
|
31 |
while len(opt_l)>0: |
32 |
opt=opt_l.pop(0)
|
33 |
if opt=="-c": |
34 |
host,port=opt_l.pop(0).split(":") |
35 |
port=int(port)
|
36 |
peer=(host,port) |
37 |
mode="client"
|
38 |
elif opt=="-s": |
39 |
host=None
|
40 |
port=int(opt_l.pop(0)) |
41 |
mode="server"
|
42 |
elif opt=="-a": |
43 |
addr=opt_l.pop(0)
|
44 |
elif opt=="-d": |
45 |
TR=float(opt_l.pop(0)) |
46 |
elif opt=="-l": |
47 |
PL=int(opt_l.pop(0)) |
48 |
elif opt=="-n": |
49 |
NP=int(opt_l.pop(0)) |
50 |
|
51 |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
52 |
|
53 |
sleep_delay=PL*8.0/TR
|
54 |
PLOVER=8+20+8 # 8 byte seq and stamp, 20 byte IP header, 8 byte UDP header |
55 |
PL2=PL-PLOVER |
56 |
|
57 |
print mode,host,port,addr,sleep_delay*1.0e6,PL,TR |
58 |
|
59 |
|
60 |
info_l=[] |
61 |
|
62 |
if mode=="server": |
63 |
s.bind((addr, port)) |
64 |
p,peer=s.recvfrom(RBUFL) |
65 |
PL,TR,NP=struct.unpack("IfI",p)
|
66 |
sleep_delay=PL*8.0/TR
|
67 |
PL2=PL-PLOVER |
68 |
print peer,sleep_delay,PL,TR,NP
|
69 |
seq_n=0
|
70 |
rand_i=0
|
71 |
t0=time.time() |
72 |
for i in xrange(NP): |
73 |
tt=time.time() |
74 |
t1=int((tt-t0)*1.0e6) |
75 |
seq_n+=1
|
76 |
buf=struct.pack("II%ds"%PL2,seq_n,t1,random_s[:PL2])
|
77 |
s.sendto(buf,peer) |
78 |
time.sleep(min(max(0.0,t0+(i+1)*sleep_delay-time.time()),sleep_delay)) # to tt+sleep_delay |
79 |
for i in xrange(10): |
80 |
s.sendto(BYE,peer) |
81 |
time.sleep(sleep_delay) |
82 |
|
83 |
elif mode=="client": |
84 |
s.bind((addr, 0))
|
85 |
s.sendto(struct.pack("IfI",PL,TR,NP),peer)
|
86 |
t0=time.time() |
87 |
while True: |
88 |
r = select.select([s],[],[])[0][0] |
89 |
if r==s:
|
90 |
t1=int((time.time()-t0)*1.0e6) |
91 |
buf,peer2=s.recvfrom(RBUFL) |
92 |
if buf==BYE: break |
93 |
seq_n,t1p,sd=struct.unpack("II%ds"%PL2,buf)
|
94 |
info_l.append((t1,seq_n,t1p,t1-t1p)) |
95 |
else:
|
96 |
print "bad select" |
97 |
|
98 |
min_t=min([x[3] for x in info_l]) |
99 |
for t1,seq_n,t1p,td in info_l: |
100 |
print "%d,%d,%d,%d,%d"%(t1,seq_n,t1p,td,td-min_t) |
101 |
dt=(info_l[-1][0]-info_l[0][0])*1.0e-6 |
102 |
np=len(info_l)
|
103 |
print np*PL*8.0/dt,float(np)/dt,TR/PL/8.0,np,PL,TR,NP |