Skip to content

Commit

Permalink
Introduce Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
Al2Klimov committed Dec 10, 2024
1 parent e50eb52 commit 5d1b07f
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions lib/base/benchmark.cpp
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;
}
37 changes: 37 additions & 0 deletions lib/base/benchmark.hpp
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};
};

}
4 changes: 4 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -112,6 +113,9 @@ add_boost_test(base
base_array/clone
base_array/json
base_base64/base64
base_benchmark/zero_seconds
base_benchmark/one_second
base_benchmark/two_seconds
base_convert/tolong
base_convert/todouble
base_convert/tostring
Expand Down
51 changes: 51 additions & 0 deletions test/base-benchmark.cpp
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()

0 comments on commit 5d1b07f

Please sign in to comment.