Pages

Banner 468

Thursday 15 August 2013

Can I improve my server response time

0 comments
 
I have the following sample code running on my server. It simply accepts connections, when it reads something it responds immediately:
import socket
import select

def main():
bind = ("0.0.0.0", 28889)
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(bind)
server_socket.listen(50)
server_socket.setblocking(0)
server_socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)

clients = {}

epoll = select.epoll()  # @UndefinedVariable
epoll.register(server_socket.fileno(), select.EPOLLIN)  # @UndefinedVariable

while 1:
    events = epoll.poll(1)
    for (fileno, event) in events:
        if fileno == server_socket.fileno():
            sock, addr = server_socket.accept()
            sock.setblocking(0)
            sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
            fileno = sock.fileno()
            epoll.register(fileno, select.EPOLLIN)  # @UndefinedVariable
            clients[fileno] = sock

        elif event & select.EPOLLIN:  # @UndefinedVariable
            sock = clients[fileno]
            try:
                sock.recv(4096)
                sock.send("~\n")
            except socket.error:
                sock.close()
                del clients[fileno]

        elif event & select.EPOLLHUP:  # @UndefinedVariable
            sock = clients[fileno]
            sock.close()
            del clients[fileno]

if __name__ == "__main__":
main()
I have the following client code which connects to the server and times the response time 10x:
import socket
import time

def main():
sock = socket.socket()
sock.connect(("192.30.35.15", 28889))

for _ in xrange(10):
    start_time = time.time()
    sock.send("~\n")
    sock.recv(2048)
    end_time = time.time()
    print "Ping: %.5f" % (end_time-start_time)


if __name__ == "__main__":
main()
Here are the results I get from running it:
Ping: 0.09100
Ping: 0.11500
Ping: 0.87100
Ping: 0.24400
Ping: 0.49100
Ping: 1.45300
Ping: 0.74800
Ping: 1.59100
Ping: 0.43600
Ping: 0.27100
This seems pretty bad with pings jumping up to 1.5 seconds.
Here's what I get when I ping the server:
Reply from 192.30.35.15: bytes=32 time=83ms
Why is my response time so bad and is there anything I can do to improve it?
Note: This is a cheap rented server, is it the best I can expect? I don't know much about server administration, is there anything I should check?

Answer

Remember, TCP requires a 3-way handshake, plus you're sending 10 round trips of data. What you've created is about the worst case in terms of latency / byte.
Check out your ICMP ping times .. then multiply that by like.. 25. I think you'll see that your application ping times are in line with what you should expect.

Leave a Reply