Files
Library/include/Void/Time/Bench.hpp
faumaray 053ee5b296
CI / GCC 16 / C++26 / sanitizers (push) Failing after 4s
CI / GCC 16 / C++26 / werror (push) Failing after 3s
CI / GCC 16 / C++26 / default (push) Failing after 1m48s
update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-25 23:42:52 +03:00

147 lines
4.6 KiB
C++

#pragma once
#include <chrono>
#include <cstddef>
#include <functional>
#include <string>
#include <vector>
namespace Void::Bench
{
using Clock = std::chrono::steady_clock;
using Duration = std::chrono::nanoseconds;
// ── BenchResult ───────────────────────────────────────────────────────────────
struct BenchResult {
std::string name;
std::size_t iterations;
Duration total;
Duration min_ns;
Duration max_ns;
[[nodiscard]] double mean_ns() const noexcept
{
return iterations > 0
? static_cast<double>(total.count()) / static_cast<double>(iterations)
: 0.0;
}
[[nodiscard]] double ops_per_sec() const noexcept
{
const double total_s =
std::chrono::duration<double>(total).count();
return total_s > 0.0
? static_cast<double>(iterations) / total_s
: 0.0;
}
};
// ── run ───────────────────────────────────────────────────────────────────────
// Run fn for `iterations` iterations, discarding the first `warmup` runs.
// The body is not inlined / optimized away — use DoNotOptimize below.
[[nodiscard]] inline BenchResult
run(std::string name, std::size_t iterations, std::function<void()> fn,
std::size_t warmup = 3)
{
for (std::size_t i = 0; i < warmup; ++i) fn();
Duration total{0};
Duration mn = Duration::max();
Duration mx = Duration::min();
for (std::size_t i = 0; i < iterations; ++i) {
const auto t0 = Clock::now();
fn();
const auto dt = Clock::now() - t0;
const auto ns = std::chrono::duration_cast<Duration>(dt);
total += ns;
if (ns < mn) mn = ns;
if (ns > mx) mx = ns;
}
return {std::move(name), iterations, total, mn, mx};
}
// ── run_for ───────────────────────────────────────────────────────────────────
// Run fn as many times as possible within `budget`, return result.
template <typename Rep, typename Period>
[[nodiscard]] inline BenchResult
run_for(std::string name, std::chrono::duration<Rep, Period> budget,
std::function<void()> fn, std::size_t warmup = 3)
{
for (std::size_t i = 0; i < warmup; ++i) fn();
Duration total{0};
Duration mn = Duration::max();
Duration mx = Duration::min();
std::size_t count = 0;
const auto deadline = Clock::now() + budget;
while (Clock::now() < deadline) {
const auto t0 = Clock::now();
fn();
const auto ns = std::chrono::duration_cast<Duration>(Clock::now() - t0);
total += ns;
if (ns < mn) mn = ns;
if (ns > mx) mx = ns;
++count;
}
if (mn == Duration::max()) mn = Duration{0};
if (mx == Duration::min()) mx = Duration{0};
return {std::move(name), count, total, mn, mx};
}
// ── DoNotOptimize ─────────────────────────────────────────────────────────────
// Prevent the compiler from optimizing away a value.
template <typename T>
inline void do_not_optimize(T& val) noexcept
{
#if defined(__GNUC__) || defined(__clang__)
// A memory operand works for every object type and remains accepted by
// GCC 16, which rejects the old mixed "+r,m" alternative constraint for
// some scalar instantiations.
asm volatile("" : "+m"(val) : : "memory");
#else
volatile T* p = &val;
(void)*p;
#endif
}
template <typename T>
inline void do_not_optimize(const T& val) noexcept
{
#if defined(__GNUC__) || defined(__clang__)
asm volatile("" : : "m"(val) : "memory");
#else
volatile const T* p = &val;
(void)*p;
#endif
}
// ── Suite ─────────────────────────────────────────────────────────────────────
// Collect multiple benchmark results.
class Suite
{
public:
void add(BenchResult r) { results_.push_back(std::move(r)); }
[[nodiscard]] const std::vector<BenchResult>& results() const noexcept
{
return results_;
}
[[nodiscard]] std::size_t size() const noexcept { return results_.size(); }
private:
std::vector<BenchResult> results_;
};
} // namespace Void::Bench