ÂI use libevent in my program, but I meet some performance problem.
I create one class named SocketEventBase. SocketEventBase is one thread and use event_base_dispatch() to check socket.
There is one list<socket*> in the SocketEventBase. Use SocketEventBase to listen all the socket in the list.
When the socket is ready to read, libevent framework will remove socket from the list and I will use another thread(named Handler) to deal the request by using
the callback function.
After Handler complete the request, i will add the socket to event base again.
So during the run-time, there are lots of event will be remove from and add to
the event base.
I try to use more event base to get better performance. I use handler-thread-pool-num to control the num of SocketEventBase. Assume handler-thread-pool-num is four, all requests will be dispatch to the four event base.
When i don't use libevent, one thread will service one socket.
After i use libevent, i wish less threads can service same num of socket and will get better performance.
But i test my program as below.
I use 300 client threads to test my program.
(It is a sysbench oltp test, and libevent is used inside a proxy, which is located between the sysbench and backend mysql)
When i don't use libevent, i get 10000~12000 tps.
After i use libevent, different handler-thread-pool-num get different result.
handler-thread-pool-num Â Â Â Â Âtps
1 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 1101.17
2 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 2065.75
4 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 3354.30
8 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â 3995.18
16 Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â4308.92
Then i monitor my program like below:
| Monitor_point Â Â Â Â| Cost_time | % Â Â Â|
| session_other Â Â Â Â| 37462 Â Â | 78.36 Â|
| session_svc Â Â Â Â Â| 47805 Â Â | 100.00 |
| session_switch Â Â Â | 484 Â Â Â | 1.01 Â |
| session_work Â Â Â Â | 9670 Â Â Â| 20.23 Â|
session_svc : all the time of the requests be done
session_switch : the time is when socket is ready to read, move request from SocketEventBase to Handler.
session_work : the time is Handler deal the requests
session_other : the time is from adding socket to SocketEventBase's list to find the socket is ready to read. (The time is also include waiting for sysbench client to send the request, but it still too much, cause theÂ session_work is only 20%, which also
includes the time for backend mysql handle the sql request and send back result )
I don't know why session_other needs so much time. Is libevent need so much time to find the socket is ready?
some other test result as below:
client thread = 30
client thread = 60
client thread = 90
client thread = 120