c++ - boost::asio::strand post method performance -
i measuring performance thread post event strand, until strand begin process it. presumed different number of receive packets, 500 ,or 1 below example, wouldn't impact boost::asio::strand when receive packets, because i passing pointer packets array packets_ptr strand, believe no copying involved. however,after timed 9999999 iteration individually, results shows aren't similar between different size. size of 1, take between 1~9micro sec, , size of 500, bwetween 50 ~85micro sec.
//socket thread      int packet_count = recvmmsg(mcast_fd, packets, 500, 0, null); //or     int packet_count = recvmmsg(mcast_fd, packets, 1, 0, null);     .....     packets_recv_time = timer.gettime();     strand_.post(boost::bind(&handler_class::process_package_method, this, packets_ptr, packets_recv_time, num_of_packets));     .....  //io_service thread     handler_class::process_package_method(...)     {     prcess_beign_time = timer.gettime();     measure_time = prcess_beign_time - packets_recv_time;     } 
the presumptions correct.  however, analytics wrong.  timestamps not measuring performance of strand::post().  instead, measuring duration of when completion handler posted strand, , when io_service starts execution of completion handler, affected numerous factors (threads, current work load, current strand backlog, etc).
to measure performance of strand::post(), 1 need sample before , after post:
auto t1 = clock::now(); strand_.post(...); auto t2 = clock::now(); when handler posted strand, may copied or moved before executed.  hence, if user provided handler's copy/move construction has uniform cost, performance of strand::post() constant.  variance can introduced when:
- the handler's copy/move construction not have uniform cost.  instance, if vectorbound handler, copy construction affected amount of elements being copied.
- there multiple threads concurrently changing strand's internals.strandprovides thread safety, thread contention may occur withinstrand::post().
in original code, functor returned boost::bind() has same type regardless of amount of messages have been received.  furthermore, , bound arguments have uniform copy/move construction cost.  thus, amount of messages received not affect performance of strand::post().
Comments
Post a Comment