Allow benchmarking of CpuBoundWork#CpuBoundWork() via additional out-param

This commit is contained in:
Alexander A. Klimov 2024-09-20 10:17:33 +02:00
parent a65f2d6b41
commit 76503f6b45
2 changed files with 32 additions and 0 deletions

View file

@ -34,6 +34,31 @@ CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc)
}
}
/**
* Measures how long it takes to acquire a slot.
*
* @param yc Forwarded to the regular constructor.
* @param took Set to the time it took to acquire the slot.
*/
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took)
: CpuBoundWork(std::move(yc), Clock::now(), took)
{
}
/**
* An internal helper layer between the regular constructor and the one that measures how long it takes.
* This is necessary to get the start time before the regular constructor is called.
*
* @param yc Forwarded to the regular constructor.
* @param started The current time.
* @param took Set to the time it took to acquire the slot.
*/
CpuBoundWork::CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took)
: CpuBoundWork(std::move(yc))
{
took = Clock::now() - started;
}
CpuBoundWork::~CpuBoundWork()
{
if (!m_Done) {

View file

@ -10,6 +10,7 @@
#include "base/logger.hpp"
#include "base/shared.hpp"
#include <atomic>
#include <chrono>
#include <exception>
#include <memory>
#include <thread>
@ -36,8 +37,14 @@ namespace icinga
*/
class CpuBoundWork
{
private:
using Clock = std::chrono::steady_clock;
CpuBoundWork(boost::asio::yield_context yc, Clock::time_point started, Clock::duration& took);
public:
CpuBoundWork(boost::asio::yield_context yc);
CpuBoundWork(boost::asio::yield_context yc, Clock::duration& took);
CpuBoundWork(const CpuBoundWork&) = delete;
CpuBoundWork(CpuBoundWork&&) = delete;
CpuBoundWork& operator=(const CpuBoundWork&) = delete;