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 void startTimer ()
87 {
88 m_start = std::chrono::high_resolution_clock::now();
89 }
90
91 void stopTimer ()
92 {
93 m_ticks += std::chrono::high_resolution_clock::now() - m_start;
94 m_count += 1;
95 }
96
97
98
101 private:
102
103 std::string m_name;
104
105 std::chrono::time_point<std::chrono::high_resolution_clock> m_start;
106
108 std::chrono::high_resolution_clock::duration m_ticks {};
109
111 std::uint64_t m_count = 0;
112
114 unsigned m_batchSize = 1;
115
117 bool m_silence = false;
118 };
119 }
120}
121
122#endif
std::chrono::time_point< std::chrono::high_resolution_clock > m_start
Definition Benchmark.h:105
std::uint64_t m_count
the number of times the timer has been started
Definition Benchmark.h:111
std::chrono::high_resolution_clock::duration m_ticks
accumulated time m_ticks
Definition Benchmark.h:108
unsigned m_batchSize
the number of calls per batch
Definition Benchmark.h:114
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:117