Re: Socket Performance

2008-03-13 Thread sleddd
On Mar 13, 9:33 am, Brian Smith [EMAIL PROTECTED] wrote:
 [EMAIL PROTECTED] wrote:
  Sent: Wednesday, March 12, 2008 9:47 PM
  To: [EMAIL PROTECTED]
  Subject: Socket Performance

  Can anyone explain why socket performance (throughput) varies
  depending on the amount of data send and recv are called with?

  For example: try creating a local client/server (running on the same
  computer) where the server sends the client a fixed amount of data.
  Using method A, recv(8192) and sendall( ) with 8192 bytes
  worth of data. Do this 100 times. Using method B, recv(1) and
  sendall( ) with 1 byte worth of data. Do this 819200 times.

  If you time both methods, method A has much greater
  throughput than method B.

 Why is it faster to drink a liter of water a cupful at a time than to
 drink it out of an eyedropper?

 - Brian

Well, lets say you have a situation where you're going to be
alternating between sending large and small chunks of data. Is the
solution to create a NetworkBuffer class and only call send when the
buffer is full, always recv(8192)?
-- 
http://mail.python.org/mailman/listinfo/python-list


Socket Performance

2008-03-12 Thread sleddd
Can anyone explain why socket performance (throughput) varies
depending on the amount of data send and recv are called with?

For example: try creating a local client/server (running on the same
computer) where the server sends the client a fixed amount of data.
Using method A, recv(8192) and sendall( ) with 8192 bytes worth of
data. Do this 100 times. Using method B, recv(1) and sendall( ) with 1
byte worth of data. Do this 819200 times.

If you time both methods, method A has much greater throughput than
method B.

Server:

import socket
import random
import string
import time

HOST = 'localhost'
PORT = 50023
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print 'Connected by', addr

string_1 = 'A'
string_8192 = ''.join([random.choice(string.letters + string.digits)
for i in range(8192)])

conn.sendall('Start')

start = time.clock()
total_data = 0
for i in range(0,100):
   conn.sendall(string_8192)
   total_data += len(string_8192)
print 'Send Speed (Long String): ' + str( total_data / (time.clock() -
start) / 1024 / 1024 ) + ' MB/sec\n\n'

start = time.clock()
total_data = 0
for i in range(0,819200):
   conn.sendall(string_1)
   total_data += len(string_1)
print 'Send Speed (Short String): ' + str( total_data / (time.clock()
- start) / 1024 / 1024 ) + ' MB/sec'

conn.close()


Client:

import socket
import time

HOST = 'localhost'# The remote host
PORT = 50023  # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, 50026))
s.connect((HOST, PORT))

data = s.recv(5)
print 'From Server: ' + data

start = time.clock()
total_data = 0
while total_data  819200:
   data = s.recv(8192)
   total_data += len(data)
print 'Receive Speed (Long String): ' + str( total_data /
(time.clock() - start) / 1024 / 1024 ) + ' MB/sec\n\n'

start = time.clock()
total_data = 0
while total_data  819200:
   data = s.recv(1)
   total_data += len(data)
print 'Receive Speed (Short String): ' + str( total_data /
(time.clock() - start) / 1024 / 1024 ) + ' MB/sec'
s.close()
-- 
http://mail.python.org/mailman/listinfo/python-list