• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#! /usr/bin/env python
2
3# Test network throughput.
4#
5# Usage:
6# 1) on host_A: throughput -s [port]                    # start a server
7# 2) on host_B: throughput -c  count host_A [port]      # start a client
8#
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():
24    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()
32
33
34def usage():
35    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)
39
40
41def server():
42    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
60
61
62def client():
63    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.'
91
92
93main()
94