ATLAS Offline Software
Loading...
Searching...
No Matches
Benchmark.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3*/
4
6
7
8#ifndef COLUMNAR_TEST_FIXTURES_BENCHMARK_H
9#define COLUMNAR_TEST_FIXTURES_BENCHMARK_H
10
11#include <optional>
12#include <chrono>
13#include <cstdint>
14#include <iostream>
15#include <string>
16
17namespace columnar
18{
19 namespace TestUtils
20 {
49
50 class Benchmark final
51 {
54 public:
55
56 Benchmark (const std::string& val_name = "", unsigned val_batchSize = 1)
57 : m_name (val_name), m_batchSize (val_batchSize)
58 {
59 if (m_name.empty())
60 m_silence = true;
61 }
62
64 {
65 if (m_count > 0 && !m_silence)
66 std::cout << m_name << ": " << std::chrono::duration<std::uint64_t,std::nano> (m_ticks) / (m_count * m_batchSize) << std::endl;
67 }
68
69 void setSilence ()
70 {
71 m_silence = true;
72 }
73
74 std::optional<float> getEntryTime (float emptyTime) const
75 {
76 if (m_count == 0)
77 return std::nullopt;
78 return static_cast<float>((std::chrono::duration<float,std::nano> (m_ticks) / (m_count * m_batchSize)) / std::chrono::duration<float,std::nano> (1))-emptyTime/m_batchSize;
79 }
80
81 auto getTotalTime () const
82 {
83 return m_ticks;
84 }
85
86 static float getTickDuration()
87 {
88 using period = std::chrono::high_resolution_clock::period;
89 return static_cast<float>(period::num) / period::den * 1e9f;
90 }
91
92 void startTimer ()
93 {
94 m_start = std::chrono::high_resolution_clock::now();
95 }
96
97 void stopTimer ()
98 {
99 m_ticks += std::chrono::high_resolution_clock::now() - m_start;
100 m_count += 1;
101 }
102
103
104
107 private:
108
109 std::string m_name;
110
111 std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
112
114 std::chrono::high_resolution_clock::duration m_ticks {};
115
117 std::uint64_t m_count = 0;
118
120 unsigned m_batchSize = 1;
121
123 bool m_silence = false;
124 };
125 }
126}
127
128#endif
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
Definition Benchmark.h:111
std::uint64_t m_count
the number of times the timer has been started
Definition Benchmark.h:117
std::chrono::high_resolution_clock::duration m_ticks
accumulated time m_ticks
Definition Benchmark.h:114
static float getTickDuration()
Definition Benchmark.h:86
unsigned m_batchSize
the number of calls per batch
Definition Benchmark.h:120
std::optional< float > getEntryTime(float emptyTime) const
Definition Benchmark.h:74
Benchmark(const std::string &val_name="", unsigned val_batchSize=1)
Definition Benchmark.h:56
bool m_silence
whether to suppress output
Definition Benchmark.h:123