-
Notifications
You must be signed in to change notification settings - Fork 579
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
127 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,34 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/benchmark.hpp" | ||
|
||
using namespace icinga; | ||
|
||
/** | ||
* Adds the elapsedTime to this instance. | ||
* | ||
* May be called multiple times to accumulate time. | ||
* | ||
* @param elapsedTime The distance between two time points | ||
* | ||
* @return This instance for method chaining | ||
*/ | ||
Benchmark& Benchmark::operator+=(const Clock::duration& elapsedTime) noexcept | ||
{ | ||
m_Sum.fetch_add(elapsedTime.count(), std::memory_order_relaxed); | ||
return *this; | ||
} | ||
|
||
/** | ||
* Adds the time elapsed since startTime to this instance. | ||
* | ||
* May be called multiple times to accumulate time. | ||
* | ||
* @param startTime The start time to subtract from the current time | ||
* | ||
* @return This instance for method chaining | ||
*/ | ||
Benchmark& Benchmark::operator+=(const Clock::time_point& startTime) noexcept | ||
{ | ||
return *this += Clock::now() - startTime; | ||
} |
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,37 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#pragma once | ||
|
||
#include "base/atomic.hpp" | ||
#include <chrono> | ||
|
||
namespace icinga | ||
{ | ||
|
||
/** | ||
* Benchmark result. | ||
* | ||
* @ingroup base | ||
*/ | ||
class Benchmark | ||
{ | ||
public: | ||
using Clock = std::chrono::steady_clock; | ||
|
||
Benchmark& operator+=(const Clock::duration&) noexcept; | ||
Benchmark& operator+=(const Clock::time_point&) noexcept; | ||
|
||
/** | ||
* @return The total accumulated time in seconds | ||
*/ | ||
template<class T> | ||
explicit operator T() const noexcept | ||
{ | ||
return std::chrono::duration<T>(Clock::duration(m_Sum.load(std::memory_order_relaxed))).count(); | ||
} | ||
|
||
private: | ||
Atomic<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,51 @@ | ||
/* Icinga 2 | (c) 2024 Icinga GmbH | GPLv2+ */ | ||
|
||
#include "base/benchmark.hpp" | ||
#include "base/utility.hpp" | ||
#include <BoostTestTargetConfig.h> | ||
#include <chrono> | ||
#include <cmath> | ||
|
||
using namespace icinga; | ||
|
||
static bool AssertSumSeconds(Benchmark& sum, long double seconds) | ||
{ | ||
return std::abs(((long double)sum - seconds) / seconds) < 0.05; | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE(base_benchmark) | ||
|
||
BOOST_AUTO_TEST_CASE(zero_seconds) | ||
{ | ||
BOOST_CHECK_EQUAL((long double)Benchmark(), 0); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(one_second) | ||
{ | ||
Benchmark sum; | ||
|
||
auto start (Benchmark::Clock::now()); | ||
Utility::Sleep(1); | ||
sum += start; | ||
|
||
BOOST_CHECK(AssertSumSeconds(sum, 1)); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(two_seconds) | ||
{ | ||
Benchmark sum; | ||
|
||
{ | ||
auto start (Benchmark::Clock::now()); | ||
Utility::Sleep(1); | ||
sum += start; | ||
} | ||
|
||
auto start (Benchmark::Clock::now()); | ||
Utility::Sleep(1); | ||
sum += start; | ||
|
||
BOOST_CHECK(AssertSumSeconds(sum, 2)); | ||
} | ||
|
||
BOOST_AUTO_TEST_SUITE_END() |