I'd like to ask some questions about optimizing linux socket. I try to make a multithreaded loadbalancer by using boost and simple linux socket. The loadbalancer works just as simple as these steps:
- A request comes in and tcp listener will accept a socket, just say it clientSocket and create a new thread
- When the thread start, it will create a back-end socket, just say it serverSocket to the back end server (service)
- After serverSocket established, I spawn a new thread to read from serverSocket and send the data/response to clientSocket
- And for the main thread, I call a function that will read from clientSocket and send to serverSocket
- When one of those two sockets become invalid, the worker will close both sockets and dies
I also use Waitset from ting library, which is using epoll, to make the recv method in blocking mode, so that it will wait until there's an event occured and then read the data from the socket.
The problem is when I tested the loadbalancer with AB, -n 10000 -c 100 -k, the result is very disappointing. I only got ~1600 tps. I tried to log request time taken for each request, but the result was good. Each round-trip got < 1000 microsecs/1 milisecs.
But when I log for incoming request intervals, next request processed about > 5000 microsecs/5 milisecs from current request received. Maybe anyone can suggest a better solution to optimize the socket operation here? Thank you.