blob: b8df1f36912c5320c25b20558fcfe798172c2f4c [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossum22825e81991-07-01 18:32:32 +00002
3# Test network throughput.
4#
5# Usage:
Tim Peterse6ddc8b2004-07-18 05:56:09 +00006# 1) on host_A: throughput -s [port] # start a server
7# 2) on host_B: throughput -c count host_A [port] # start a client
Guido van Rossum22825e81991-07-01 18:32:32 +00008#
9# The server will service multiple clients until it is killed.
10#
11# The client performs one transfer of count*BUFSIZE bytes and
12# measures the time it takes (roundtrip!).
13
14
15import sys, time
16from socket import *
17
18MY_PORT = 50000 + 42
19
20BUFSIZE = 1024
21
22
23def main():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000024 if len(sys.argv) < 2:
25 usage()
26 if sys.argv[1] == '-s':
27 server()
28 elif sys.argv[1] == '-c':
29 client()
30 else:
31 usage()
Guido van Rossum22825e81991-07-01 18:32:32 +000032
33
34def usage():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000035 sys.stdout = sys.stderr
36 print 'Usage: (on host_A) throughput -s [port]'
37 print 'and then: (on host_B) throughput -c count host_A [port]'
38 sys.exit(2)
Guido van Rossum22825e81991-07-01 18:32:32 +000039
40
41def server():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000042 if len(sys.argv) > 2:
43 port = eval(sys.argv[2])
44 else:
45 port = MY_PORT
46 s = socket(AF_INET, SOCK_STREAM)
47 s.bind(('', port))
48 s.listen(1)
49 print 'Server ready...'
50 while 1:
51 conn, (host, remoteport) = s.accept()
52 while 1:
53 data = conn.recv(BUFSIZE)
54 if not data:
55 break
56 del data
57 conn.send('OK\n')
58 conn.close()
59 print 'Done with', host, 'port', remoteport
Guido van Rossum22825e81991-07-01 18:32:32 +000060
61
62def client():
Tim Peterse6ddc8b2004-07-18 05:56:09 +000063 if len(sys.argv) < 4:
64 usage()
65 count = int(eval(sys.argv[2]))
66 host = sys.argv[3]
67 if len(sys.argv) > 4:
68 port = eval(sys.argv[4])
69 else:
70 port = MY_PORT
71 testdata = 'x' * (BUFSIZE-1) + '\n'
72 t1 = time.time()
73 s = socket(AF_INET, SOCK_STREAM)
74 t2 = time.time()
75 s.connect((host, port))
76 t3 = time.time()
77 i = 0
78 while i < count:
79 i = i+1
80 s.send(testdata)
81 s.shutdown(1) # Send EOF
82 t4 = time.time()
83 data = s.recv(BUFSIZE)
84 t5 = time.time()
85 print data
86 print 'Raw timers:', t1, t2, t3, t4, t5
87 print 'Intervals:', t2-t1, t3-t2, t4-t3, t5-t4
88 print 'Total:', t5-t1
89 print 'Throughput:', round((BUFSIZE*count*0.001) / (t5-t1), 3),
90 print 'K/sec.'
Guido van Rossum22825e81991-07-01 18:32:32 +000091
92
93main()