-
Notifications
You must be signed in to change notification settings - Fork 581
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/benchmark.hpp" | ||
|
||
using namespace icinga; | ||
|
||
/** | ||
* Adds the time elapsed since watch.GetStartTime() to this instance. | ||
* | ||
* May be called multiple times to accumulate time. | ||
* | ||
* @param watch The watch to get the start time from | ||
* | ||
* @return This instance for method chaining | ||
*/ | ||
BenchmarkSummary& BenchmarkSummary::operator+=(const BenchmarkStopWatch& watch) noexcept | ||
{ | ||
m_Sum.fetch_add((BenchmarkStopWatch::Clock::now() - watch.GetStartTime()).count(), std::memory_order_relaxed); | ||
return *this; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#pragma once | ||
|
||
#include "base/atomic.hpp" | ||
#include <chrono> | ||
|
||
namespace icinga | ||
{ | ||
|
||
/** | ||
* Stopwatch for benchmarking. | ||
* | ||
* @ingroup base | ||
*/ | ||
class BenchmarkStopWatch { | ||
public: | ||
using Clock = std::chrono::steady_clock; | ||
|
||
/** | ||
* Captures the current time for later subtraction. | ||
* | ||
* May be called multiple times to re-start the timer. | ||
*/ | ||
void Start() noexcept | ||
{ | ||
m_Start = Clock::now(); | ||
} | ||
|
||
/** | ||
* @return The last time Start() was called | ||
*/ | ||
[[nodiscard]] Clock::time_point GetStartTime() const noexcept | ||
{ | ||
return m_Start; | ||
} | ||
|
||
private: | ||
Clock::time_point m_Start; | ||
}; | ||
|
||
/** | ||
* Benchmark result. | ||
* | ||
* @ingroup base | ||
*/ | ||
class BenchmarkSummary | ||
{ | ||
public: | ||
BenchmarkSummary& operator+=(const BenchmarkStopWatch&) noexcept; | ||
|
||
/** | ||
* @return The total accumulated time in seconds | ||
*/ | ||
template<class T> | ||
explicit operator T() const noexcept | ||
{ | ||
return std::chrono::duration<T>(BenchmarkStopWatch::Clock::duration(m_Sum.load(std::memory_order_relaxed))).count(); | ||
} | ||
|
||
private: | ||
Atomic<BenchmarkStopWatch::Clock::duration::rep> m_Sum {0}; | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/benchmark.hpp" | ||
#include "base/utility.hpp" | ||
#include <BoostTestTargetConfig.h> | ||
#include <cmath> | ||
|
||
using namespace icinga; | ||
|
||
static bool AssertSumSeconds(BenchmarkSummary& sum, long double seconds) | ||
{ | ||
return std::abs(((long double)sum - seconds) / seconds) < 0.05; | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE(base_benchmark) | ||
|
||
BOOST_AUTO_TEST_CASE(one_second) | ||
{ | ||
BenchmarkSummary sum; | ||
BenchmarkStopWatch sw; | ||
|
||
sw.Start(); | ||
Utility::Sleep(1); | ||
sum += sw; | ||
|
||
BOOST_CHECK(AssertSumSeconds(sum, 1)); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(two_seconds) | ||
{ | ||
BenchmarkSummary sum; | ||
BenchmarkStopWatch sw; | ||
|
||
sw.Start(); | ||
Utility::Sleep(1); | ||
sum += sw; | ||
|
||
sw.Start(); | ||
Utility::Sleep(1); | ||
sum += sw; | ||
|
||
BOOST_CHECK(AssertSumSeconds(sum, 2)); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |