Skip to content

Instantly share code, notes, and snippets.

@StephanDollberg
Last active October 27, 2019 11:46
Show Gist options
  • Save StephanDollberg/93a752a04436040495b449954c8b1248 to your computer and use it in GitHub Desktop.
Save StephanDollberg/93a752a04436040495b449954c8b1248 to your computer and use it in GitHub Desktop.
io_context poll latency
#include <iostream>
#include <chrono>
#include <boost/asio/io_context.hpp>
#include <boost/asio/spawn.hpp>
#include <boost/asio/steady_timer.hpp>
int main(int argc, char* argv[])
{
boost::asio::io_context io_context(BOOST_ASIO_CONCURRENCY_HINT_1);
std::uint64_t counter = 0;
std::uint64_t fire_count = 0;
for (int i = 0; i < 1; ++i) {
boost::asio::spawn(io_context, [&] (boost::asio::yield_context yield) {
boost::asio::steady_timer timer(io_context);
while (true) {
timer.expires_after(std::chrono::seconds(1));
timer.async_wait(yield);
// std::cout << "fired " << std::endl;
fire_count++;
}
});
}
auto begin = std::chrono::high_resolution_clock::now();
while(fire_count < 10) {
counter++;
io_context.poll();
}
auto end = std::chrono::high_resolution_clock::now();
auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
std::cout << "Polled count: " << counter << std::endl;
std::cout << "Fired: " << fire_count << std::endl;
std::cout << "ns per poll: " << nanos / double(counter) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment