Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

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