mirror of
https://github.com/Icinga/icinga2.git
synced 2025-09-24 10:17:59 +02:00
Introduce Benchmark
This commit is contained in:
parent
f308bb154a
commit
3c201fa605
@ -19,6 +19,7 @@ set(base_SOURCES
|
||||
atomic.hpp
|
||||
atomic-file.cpp atomic-file.hpp
|
||||
base64.cpp base64.hpp
|
||||
benchmark.cpp benchmark.hpp
|
||||
boolean.cpp boolean.hpp boolean-script.cpp
|
||||
bulker.hpp
|
||||
configobject.cpp configobject.hpp configobject-ti.hpp configobject-script.cpp
|
||||
|
34
lib/base/benchmark.cpp
Normal file
34
lib/base/benchmark.cpp
Normal file
@ -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;
|
||||
}
|
37
lib/base/benchmark.hpp
Normal file
37
lib/base/benchmark.hpp
Normal file
@ -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};
|
||||
};
|
||||
|
||||
}
|
@ -59,6 +59,7 @@ set(base_test_SOURCES
|
||||
icingaapplication-fixture.cpp
|
||||
base-array.cpp
|
||||
base-base64.cpp
|
||||
base-benchmark.cpp
|
||||
base-convert.cpp
|
||||
base-dictionary.cpp
|
||||
base-fifo.cpp
|
||||
@ -113,6 +114,9 @@ add_boost_test(base
|
||||
base_array/clone
|
||||
base_array/json
|
||||
base_base64/base64
|
||||
base_benchmark/zero
|
||||
base_benchmark/one
|
||||
base_benchmark/two
|
||||
base_convert/tolong
|
||||
base_convert/todouble
|
||||
base_convert/tostring
|
||||
|
51
test/base-benchmark.cpp
Normal file
51
test/base-benchmark.cpp
Normal file
@ -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, double seconds)
|
||||
{
|
||||
return std::abs(((double)sum - seconds) / seconds) < 0.05;
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE(base_benchmark)
|
||||
|
||||
BOOST_AUTO_TEST_CASE(zero)
|
||||
{
|
||||
BOOST_CHECK_EQUAL((double)Benchmark(), 0);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(one)
|
||||
{
|
||||
Benchmark sum;
|
||||
|
||||
auto start (Benchmark::Clock::now());
|
||||
Utility::Sleep(0.25);
|
||||
sum += start;
|
||||
|
||||
BOOST_CHECK(AssertSumSeconds(sum, 0.25));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_CASE(two)
|
||||
{
|
||||
Benchmark sum;
|
||||
|
||||
{
|
||||
auto start (Benchmark::Clock::now());
|
||||
Utility::Sleep(0.25);
|
||||
sum += start;
|
||||
}
|
||||
|
||||
auto start (Benchmark::Clock::now());
|
||||
Utility::Sleep(0.5);
|
||||
sum += start;
|
||||
|
||||
BOOST_CHECK(AssertSumSeconds(sum, 0.75));
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
Loading…
x
Reference in New Issue
Block a user