Skip to content

Instantly share code, notes, and snippets.

@thawk
Last active April 25, 2017 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thawk/9dbf88a9b3ff55afe754811aba769918 to your computer and use it in GitHub Desktop.
Save thawk/9dbf88a9b3ff55afe754811aba769918 to your computer and use it in GitHub Desktop.
Boost timer test
bin/
GPATH
GRTAGS
GTAGS
lib system : : <name>boost_system <search>/usr/lib ;
exe timer_test : timer_test.cpp system : <include>/usr/include <threading>multi <variant>release ;
#include <iostream>
#include <boost/asio.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/bind.hpp>
#include <thread>
#include <boost/lexical_cast.hpp>
#include <future>
#include <chrono>
#include <vector>
using namespace std;
namespace asio = boost::asio;
using boost::lexical_cast;
class TimerTester
{
public:
struct Result
{
chrono::steady_clock::time_point begin_time;
chrono::steady_clock::time_point end_time;
};
typedef vector<Result> Results;
private:
asio::steady_timer timer_;
asio::steady_timer::duration dur_;
size_t loop_count_;
Results results_;
promise<Results> promise_;
public:
TimerTester(
asio::io_service& ios,
asio::steady_timer::duration dur,
size_t loop_count)
: timer_(ios),
dur_(dur),
loop_count_(loop_count)
{
results_.reserve(loop_count);
}
virtual ~TimerTester()
{
}
void Start()
{
if (loop_count_ == 0)
{
promise_.set_value(results_);
return;
}
loop_count_--;
results_.push_back(Result());
results_.back().begin_time = chrono::steady_clock::now();
SetTimer();
}
virtual void SetTimer()
{
timer_.expires_from_now(dur_);
timer_.async_wait(boost::bind(&TimerTester::OnTimer, this));
}
virtual void OnTimer()
{
results_.back().end_time = chrono::steady_clock::now();
Start();
}
future<Results> GetResults()
{
return promise_.get_future();
}
};
vector<TimerTester::Results> TestTimer(size_t thread_count, size_t dur_us, size_t loop_count, size_t timer_count)
{
asio::io_service ios;
vector<thread> threads;
vector<TimerTester::Results> results;
{
asio::io_service::work work(ios);
for (size_t i=0; i<thread_count; ++i)
{
threads.push_back(thread(boost::bind(&asio::io_service::run, &ios)));
}
chrono::microseconds dur(dur_us);
vector<TimerTester*> testers;
for (size_t i=0; i<timer_count; ++i)
{
testers.push_back(
new TimerTester(ios, chrono::microseconds(dur), loop_count));
}
for (auto tester : testers)
{
tester->Start();
}
for (auto tester : testers)
{
results.push_back(tester->GetResults().get());
delete tester;
}
}
for (auto& t: threads)
{
t.join();
}
return results;
}
int main(int argc, const char* argv[])
{
if (argc < 2)
{
cerr << "Usage: " << argv[0] << " <dur(us)> [<loop>=1] [<timer_count>=1]" << endl;
return 1;
}
size_t thread_count = 1;
size_t dur_us = lexical_cast<size_t>(argv[1]);
size_t loop_count = argc > 2 ? lexical_cast<size_t>(argv[2]) : 1;
size_t timer_count = argc > 3 ? lexical_cast<size_t>(argv[3]) : 1;
vector<TimerTester::Results> results(
TestTimer(thread_count, dur_us, loop_count, timer_count));
for (size_t i=0; i<loop_count; ++i)
{
cout << "#" << (i+1);
for (size_t j=0; j<timer_count; ++j)
{
auto& result = results[j][i];
cout << "\t" << chrono::duration_cast<chrono::microseconds>(
result.end_time - result.begin_time).count();
}
cout << endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment