Advertisement
Guest User

asio benchmark timer

a guest
Sep 3rd, 2015
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <future>
  2. #include <iostream>
  3.  
  4. #include <boost/accumulators/accumulators.hpp>
  5. #include <boost/accumulators/statistics.hpp>
  6. #include <boost/chrono.hpp>
  7.  
  8. #include <boost/asio/basic_waitable_timer.hpp>
  9.  
  10. #include <boost/thread.hpp>
  11.  
  12. typedef boost::accumulators::accumulator_set<
  13.     int, boost::accumulators::features<
  14.              boost::accumulators::tag::min, boost::accumulators::tag::max,
  15.              boost::accumulators::tag::mean, boost::accumulators::tag::median>>
  16.     Accumulator;
  17.  
  18. void DisplayStatistics(const Accumulator& statistics, int expected_wait);
  19.  
  20. int main(int argc, char* argv[]) {
  21.   namespace chrono = boost::chrono;
  22.   typedef chrono::high_resolution_clock Clock;
  23.   typedef boost::asio::basic_waitable_timer<Clock> ClockTimer;
  24.   typedef chrono::time_point<Clock> ClockTimePoint;
  25.   typedef std::function<void(const boost::system::error_code&)> TimerHandler;
  26.  
  27.   if (argc < 3) {
  28.     std::cout << "timer_benchmark [wait_usec] [sample_size]" << std::endl;
  29.     return 0;
  30.   }
  31.  
  32.   boost::asio::io_service io_service;
  33.   int count = 0;
  34.   int wait_usec = atoi(argv[1]);
  35.   int sample_size = atoi(argv[2]);
  36.   std::promise<bool> wait_end;
  37.   Accumulator statistics;
  38.   ClockTimer timer(io_service);
  39.   TimerHandler wait_handler;
  40.   ClockTimePoint last_now;
  41.  
  42.   if (wait_usec < 1) wait_usec = 50;
  43.   if (sample_size < 1) sample_size = 10000;
  44.  
  45.   wait_handler = [&](const boost::system::error_code& ec) {
  46.     statistics(static_cast<int>(
  47.         chrono::duration_cast<chrono::microseconds>(Clock::now() - last_now)
  48.             .count()));
  49.     ++count;
  50.     if (!ec && count < sample_size) {
  51.       last_now = Clock::now();
  52.       timer.expires_from_now(chrono::microseconds(wait_usec));
  53.       timer.async_wait(wait_handler);
  54.     } else {
  55.       wait_end.set_value(ec.value() == 0);
  56.     }
  57.   };
  58.  
  59.   last_now = Clock::now();
  60.   timer.expires_from_now(chrono::microseconds(wait_usec));
  61.   timer.async_wait(wait_handler);
  62.  
  63.   boost::thread_group threads;
  64.   for (uint16_t i = 1; i <= boost::thread::hardware_concurrency(); ++i) {
  65.     threads.create_thread([&io_service]() { io_service.run(); });
  66.   }
  67.  
  68.   if (!wait_end.get_future().get()) {
  69.     std::cout << "Collect statistics failed";
  70.     return 1;
  71.   }
  72.  
  73.   threads.join_all();
  74.  
  75.   DisplayStatistics(statistics, wait_usec);
  76.  
  77.   return 0;
  78. }
  79.  
  80. void DisplayStatistics(const Accumulator& statistics, int expected_wait) {
  81.   std::cout << "Number of samples    : "
  82.             << boost::accumulators::count(statistics) << std::endl;
  83.   std::cout << "Expected wait (usec) : " << expected_wait << std::endl;
  84.   std::cout << "Mean (usec)          : "
  85.             << boost::accumulators::mean(statistics) << std::endl;
  86.   std::cout << "Median (usec)        : "
  87.             << boost::accumulators::median(statistics) << std::endl;
  88.   std::cout << "Min (usec)           : " << boost::accumulators::min(statistics)
  89.             << std::endl;
  90.   std::cout << "Max (usec)           : " << boost::accumulators::max(statistics)
  91.             << std::endl;
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement