Botan 3.0.0
Crypto and TLS for C&
timer.h
Go to the documentation of this file.
1/*
2* (C) 2018 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#ifndef BOTAN_TIMER_H_
8#define BOTAN_TIMER_H_
9
10#include <botan/types.h>
11#include <string>
12#include <chrono>
13
14namespace Botan {
15
17 {
18 public:
19 Timer(std::string_view name,
20 std::string_view provider,
21 std::string_view doing,
22 uint64_t event_mult,
23 size_t buf_size,
24 double clock_cycle_ratio,
25 uint64_t clock_speed);
26
27 Timer(std::string_view name) :
28 Timer(name, "", "", 1, 0, 0.0, 0)
29 {}
30
31 Timer(std::string_view name, size_t buf_size) :
32 Timer(name, "", "", buf_size, buf_size, 0.0, 0)
33 {}
34
35 Timer(const Timer& other) = default;
36 Timer& operator=(const Timer& other) = default;
37
38 void start();
39
40 void stop();
41
42 bool under(std::chrono::milliseconds msec)
43 {
44
45 return (milliseconds() < msec.count());
46 }
47
49 {
50 public:
51 explicit Timer_Scope(Timer& timer)
52 : m_timer(timer)
53 {
54 m_timer.start();
55 }
57 {
58 try
59 {
60 m_timer.stop();
61 }
62 catch(...) {}
63 }
64 private:
65 Timer& m_timer;
66 };
67
68 template<typename F>
69 auto run(F f) -> decltype(f())
70 {
71 Timer_Scope timer(*this);
72 return f();
73 }
74
75 template<typename F>
76 void run_until_elapsed(std::chrono::milliseconds msec, F f)
77 {
78 while(this->under(msec))
79 {
80 run(f);
81 }
82 }
83
84 uint64_t value() const
85 {
86 return m_time_used;
87 }
88
89 double seconds() const
90 {
91 return milliseconds() / 1000.0;
92 }
93
94 double milliseconds() const
95 {
96 return value() / 1000000.0;
97 }
98
99 double ms_per_event() const
100 {
101 return milliseconds() / events();
102 }
103
104 uint64_t cycles_consumed() const
105 {
106 if(m_clock_speed != 0)
107 {
108 return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
109 }
110 return m_cpu_cycles_used;
111 }
112
113 uint64_t events() const
114 {
115 return m_event_count * m_event_mult;
116 }
117
118 const std::string get_name() const
119 {
120 return m_name;
121 }
122
123 const std::string doing() const
124 {
125 return m_doing;
126 }
127
128 size_t buf_size() const
129 {
130 return m_buf_size;
131 }
132
133 double bytes_per_second() const
134 {
135 return seconds() > 0.0 ? events() / seconds() : 0.0;
136 }
137
138 double events_per_second() const
139 {
140 return seconds() > 0.0 ? events() / seconds() : 0.0;
141 }
142
143 double seconds_per_event() const
144 {
145 return events() > 0 ? seconds() / events() : 0.0;
146 }
147
148 void set_custom_msg(std::string_view s)
149 {
150 m_custom_msg = s;
151 }
152
153 bool operator<(const Timer& other) const;
154
155 std::string to_string() const;
156
157 private:
158 std::string result_string_bps() const;
159 std::string result_string_ops() const;
160
161 // const data
162 std::string m_name, m_doing;
163 size_t m_buf_size;
164 uint64_t m_event_mult;
165 double m_clock_cycle_ratio;
166 uint64_t m_clock_speed;
167
168 // set at runtime
169 std::string m_custom_msg;
170 uint64_t m_time_used = 0, m_timer_start = 0;
171 uint64_t m_event_count = 0;
172
173 uint64_t m_max_time = 0, m_min_time = 0;
174 uint64_t m_cpu_cycles_start = 0, m_cpu_cycles_used = 0;
175 };
176
177}
178
179#endif
Timer_Scope(Timer &timer)
Definition: timer.h:51
double milliseconds() const
Definition: timer.h:94
Timer(std::string_view name, size_t buf_size)
Definition: timer.h:31
double events_per_second() const
Definition: timer.h:138
uint64_t value() const
Definition: timer.h:84
bool under(std::chrono::milliseconds msec)
Definition: timer.h:42
uint64_t cycles_consumed() const
Definition: timer.h:104
Timer & operator=(const Timer &other)=default
Timer(std::string_view name)
Definition: timer.h:27
double ms_per_event() const
Definition: timer.h:99
double seconds_per_event() const
Definition: timer.h:143
const std::string doing() const
Definition: timer.h:123
auto run(F f) -> decltype(f())
Definition: timer.h:69
Timer(const Timer &other)=default
void set_custom_msg(std::string_view s)
Definition: timer.h:148
size_t buf_size() const
Definition: timer.h:128
uint64_t events() const
Definition: timer.h:113
void run_until_elapsed(std::chrono::milliseconds msec, F f)
Definition: timer.h:76
double seconds() const
Definition: timer.h:89
const std::string get_name() const
Definition: timer.h:118
double bytes_per_second() const
Definition: timer.h:133
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition: compiler.h:51
Definition: alg_id.cpp:12
bool operator<(const OID &a, const OID &b)
Definition: asn1_oid.cpp:141