Mercurial > cpython-lru_cache-weakref
annotate Demo/sockets/throughput.py @ 54294:dba767bc3155 legacy-trunk
remove mention of old ctypes version
| author | Benjamin Peterson <benjamin@python.org> |
|---|---|
| date | Sat, 23 May 2009 20:59:09 +0000 |
| parents | 2bc66f1df7cc |
| children | 7d16b42fcdbd |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1566
diff
changeset
|
1 #! /usr/bin/env python |
| 300 | 2 |
| 3 # Test network throughput. | |
| 4 # | |
| 5 # Usage: | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
6 # 1) on host_A: throughput -s [port] # start a server |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
7 # 2) on host_B: throughput -c count host_A [port] # start a client |
| 300 | 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 | |
| 15 import sys, time | |
| 16 from socket import * | |
| 17 | |
| 18 MY_PORT = 50000 + 42 | |
| 19 | |
| 20 BUFSIZE = 1024 | |
| 21 | |
| 22 | |
| 23 def main(): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
24 if len(sys.argv) < 2: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
25 usage() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
26 if sys.argv[1] == '-s': |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
27 server() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
28 elif sys.argv[1] == '-c': |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
29 client() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
30 else: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
31 usage() |
| 300 | 32 |
| 33 | |
| 34 def usage(): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
35 sys.stdout = sys.stderr |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
36 print 'Usage: (on host_A) throughput -s [port]' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
37 print 'and then: (on host_B) throughput -c count host_A [port]' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
38 sys.exit(2) |
| 300 | 39 |
| 40 | |
| 41 def server(): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
42 if len(sys.argv) > 2: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
43 port = eval(sys.argv[2]) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
44 else: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
45 port = MY_PORT |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
46 s = socket(AF_INET, SOCK_STREAM) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
47 s.bind(('', port)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
48 s.listen(1) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
49 print 'Server ready...' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
50 while 1: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
51 conn, (host, remoteport) = s.accept() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
52 while 1: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
53 data = conn.recv(BUFSIZE) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
54 if not data: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
55 break |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
56 del data |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
57 conn.send('OK\n') |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
58 conn.close() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
59 print 'Done with', host, 'port', remoteport |
| 300 | 60 |
| 61 | |
| 62 def client(): | |
|
32735
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
63 if len(sys.argv) < 4: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
64 usage() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
65 count = int(eval(sys.argv[2])) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
66 host = sys.argv[3] |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
67 if len(sys.argv) > 4: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
68 port = eval(sys.argv[4]) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
69 else: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
70 port = MY_PORT |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
71 testdata = 'x' * (BUFSIZE-1) + '\n' |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
72 t1 = time.time() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
73 s = socket(AF_INET, SOCK_STREAM) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
74 t2 = time.time() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
75 s.connect((host, port)) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
76 t3 = time.time() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
77 i = 0 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
78 while i < count: |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
79 i = i+1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
80 s.send(testdata) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
81 s.shutdown(1) # Send EOF |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
82 t4 = time.time() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
83 data = s.recv(BUFSIZE) |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
84 t5 = time.time() |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
85 print data |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
86 print 'Raw timers:', t1, t2, t3, t4, t5 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
87 print 'Intervals:', t2-t1, t3-t2, t4-t3, t5-t4 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
88 print 'Total:', t5-t1 |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
89 print 'Throughput:', round((BUFSIZE*count*0.001) / (t5-t1), 3), |
|
2bc66f1df7cc
Whitespace normalization. Ran reindent.py over the entire source tree.
Tim Peters <tim.peters@gmail.com>
parents:
14183
diff
changeset
|
90 print 'K/sec.' |
| 300 | 91 |
| 92 | |
| 93 main() |
