+++ /dev/null
-#! /usr/bin/env python\r
-\r
-# Test network throughput.\r
-#\r
-# Usage:\r
-# 1) on host_A: throughput -s [port] # start a server\r
-# 2) on host_B: throughput -c count host_A [port] # start a client\r
-#\r
-# The server will service multiple clients until it is killed.\r
-#\r
-# The client performs one transfer of count*BUFSIZE bytes and\r
-# measures the time it takes (roundtrip!).\r
-\r
-\r
-import sys, time\r
-from socket import *\r
-\r
-MY_PORT = 50000 + 42\r
-\r
-BUFSIZE = 1024\r
-\r
-\r
-def main():\r
- if len(sys.argv) < 2:\r
- usage()\r
- if sys.argv[1] == '-s':\r
- server()\r
- elif sys.argv[1] == '-c':\r
- client()\r
- else:\r
- usage()\r
-\r
-\r
-def usage():\r
- sys.stdout = sys.stderr\r
- print 'Usage: (on host_A) throughput -s [port]'\r
- print 'and then: (on host_B) throughput -c count host_A [port]'\r
- sys.exit(2)\r
-\r
-\r
-def server():\r
- if len(sys.argv) > 2:\r
- port = eval(sys.argv[2])\r
- else:\r
- port = MY_PORT\r
- s = socket(AF_INET, SOCK_STREAM)\r
- s.bind(('', port))\r
- s.listen(1)\r
- print 'Server ready...'\r
- while 1:\r
- conn, (host, remoteport) = s.accept()\r
- while 1:\r
- data = conn.recv(BUFSIZE)\r
- if not data:\r
- break\r
- del data\r
- conn.send('OK\n')\r
- conn.close()\r
- print 'Done with', host, 'port', remoteport\r
-\r
-\r
-def client():\r
- if len(sys.argv) < 4:\r
- usage()\r
- count = int(eval(sys.argv[2]))\r
- host = sys.argv[3]\r
- if len(sys.argv) > 4:\r
- port = eval(sys.argv[4])\r
- else:\r
- port = MY_PORT\r
- testdata = 'x' * (BUFSIZE-1) + '\n'\r
- t1 = time.time()\r
- s = socket(AF_INET, SOCK_STREAM)\r
- t2 = time.time()\r
- s.connect((host, port))\r
- t3 = time.time()\r
- i = 0\r
- while i < count:\r
- i = i+1\r
- s.send(testdata)\r
- s.shutdown(1) # Send EOF\r
- t4 = time.time()\r
- data = s.recv(BUFSIZE)\r
- t5 = time.time()\r
- print data\r
- print 'Raw timers:', t1, t2, t3, t4, t5\r
- print 'Intervals:', t2-t1, t3-t2, t4-t3, t5-t4\r
- print 'Total:', t5-t1\r
- print 'Throughput:', round((BUFSIZE*count*0.001) / (t5-t1), 3),\r
- print 'K/sec.'\r
-\r
-\r
-main()\r