SocketServer on NT slow for large transactions

eric jones ej at ee.duke.edu
Thu Sep 9 02:09:49 EDT 1999


Hello,

I wrote a very simple program using based on the
SocketServer.StreamRequestHandler, and found that it crawled along at a
snails pace.  I've traced the problem to the socket._fileobject.  Turns out
that SocketServer.StreamRequestHandler sets it to read 1 byte at a time from
the socket.  Overriding setup() in StreamRequestHandler to create larger
buffers solves the problem (80x speed up).  Either
SocketServer.StreamRequestHandler.setup() or socket._fileobject.__init__()
can be patched to fix the problem.  This is a Windows only issue.   The
problem didn't show up Linux.

I've attached my little test program.  To run the "fast" server, run the
code with out a command line argument.

For the "slow" server, use:

c:\> sock_test slow

Then run a client the client:

c:\> sock_test client

On my machine, the fast client sends and receives 30K in .05 sec, and the
slow client sends and receives 30K in 4.6 sec.

regards,
eric

################# sock_test #########################

import socket
import SocketServer
import time

bufsize = 1<<19 #.5 MB buffer

def client(host,port):
 t1 = time.clock()
 sock = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
 sock.connect(host, port)
 sent = sock.send('aaa'*10000)
 print 'data sent:', sent
 sock.shutdown(1)
 rfile = sock.makefile('r',bufsize)
 msg = rfile.read()
 t2 = time.clock()
 print 'data received: %d, roundtrip time of %f sec' % (len(msg),t2-t1)

class standard_handler(SocketServer.StreamRequestHandler):
    def handle(self):
  t1 = time.clock()
  data=self.rfile.read()
  t2 = time.clock()
  print 'received %d bytes in %f sec' % (len(data),t2-t1)
  self.wfile.write(data)
  t3 = time.clock()
  print 'sent %d bytes in %f sec' % (len(data),t3-t2)

class fast_handler(standard_handler):
    def setup(self):
        self.connection = self.request
        self.rfile = self.connection.makefile('rb', bufsize)
        self.wfile = self.connection.makefile('wb', bufsize)

if __name__ == '__main__':
 import sys
 import string
 host = socket.gethostname()
 port = 8000
 if len(sys.argv) == 2 and sys.argv[1] == 'client':
  client(host,port)
 else:
  if len(sys.argv) == 2 and sys.argv[1] == 'slow':
   h = standard_handler
  else:
   h = fast_handler
  server=SocketServer.TCPServer( (host, port), h)
  server.serve_forever()








More information about the Python-list mailing list