Search code examples
pythonsocketsubuntu-14.04recv

23% performance degradation on ubuntu 14.04 with socket.recv


I am running python 2.7.8 and observing 23% performance degradation on ubuntu 14.04 as compared to 10.04 with simple socket.recv API. I can share my code, exact performance numbers etc. for anyone to look at.

Here is my server:

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module
import sys

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port
f = open ("file", "r")
fdata = f.read(100000000)
s.listen(5)                 # Now wait for client connection.
while True:
    c, addr = s.accept()     # Establish connection with client.
    print 'Got connection from', addr
    if sys.argv[1] == '1':
        c.send('Thank you for connecting')
    elif sys.argv[1] == '2':
        c.send(fdata)
    c.close()   

Here is my client:

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module
import sys
import time
s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.connect((host, port))

i = 0
start = time.time()
while i < int(sys.argv[1]):
    s.recv(100000000)
    i = i + 1
end = time.time()
print "Time taken is %s" % (end - start)
s.close 

When I run 10 million recv's, on ubuntu 14.04 it takes 375 seconds whereas on ubuntu 10.04 it takes only 305 seconds.


Solution

  • Assuming that you use the same hardware you still have a lots of changes between 10.04 and 14.04, i.e. different kernel, different libraries, different python, maybe different system tuning etc. So don't expect anybody to point out the cause for this slow down of your very high level benchmark.

    Just to give an idea where such performance issues might lurk: changes to the scheduler or to processes running on the same system might cause a different load to the CPU. If the CPU gets too hot it will be forced to cool down by running slower which can cause a large performance difference. On the other hand if the CPU gets too idle it will go into various power save modes and waking up from these takes some time which also affects performance. Other things which heavily affect performance are CPU caches, i.e. if the relevant python code fit fully into the processor cache in 10.04 and is now just a bit too large you can get a serious performance difference.