python - Can I improve my server response time? -
i have following sample code running on server. accepts connections, when reads 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) (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 following client code connects server , times response time 10x:
import socket import time def main(): sock = socket.socket() sock.connect(("192.30.35.15", 28889)) _ 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 results 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 pings jumping 1.5 seconds.
here's when ping server:
reply 192.30.35.15: bytes=32 time=83ms
why response time bad , there can improve it?
note: cheap rented server, best can expect? don't know server administration, there should check?
remember, tcp requires 3-way handshake, plus you're sending 10 round trips of data. you've created worst case in terms of latency / byte.
check out icmp ping times .. multiply like.. 25. think you'll see application ping times in line should expect.
edit: while sound advice many programs, guess doesn't pertain situation. connect isn't within timers. still, you're looking @ delays due nagle algorithm, potentially turn off specific code (but in general it's leave on). still want @ icmp ping times see response time possibly given network situation.
Comments
Post a Comment