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.
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