Botan 3.5.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 <chrono>
12#include <string>
13
14namespace Botan {
15
17 public:
18 Timer(std::string_view name,
19 std::string_view provider,
20 std::string_view doing,
21 uint64_t event_mult,
22 size_t buf_size,
23 double clock_cycle_ratio,
24 uint64_t clock_speed);
25
26 Timer(std::string_view name) : Timer(name, "", "", 1, 0, 0.0, 0) {}
27
28 Timer(std::string_view name, size_t buf_size) : Timer(name, "", "", buf_size, buf_size, 0.0, 0) {}
29
30 Timer(const Timer& other) = default;
31 Timer& operator=(const Timer& other) = default;
32
33 void start();
34
35 void stop();
36
37 bool under(std::chrono::milliseconds msec) const { return (milliseconds() < msec.count()); }
38
40 public:
41 explicit Timer_Scope(Timer& timer) : m_timer(timer) { m_timer.start(); }
42
44 try {
45 m_timer.stop();
46 } catch(...) {}
47 }
48
49 private:
50 Timer& m_timer;
51 };
52
53 template <typename F>
54 auto run(F f) -> decltype(f()) {
55 Timer_Scope timer(*this);
56 return f();
57 }
58
59 template <typename F>
60 void run_until_elapsed(std::chrono::milliseconds msec, F f) {
61 while(this->under(msec)) {
62 run(f);
63 }
64 }
65
66 uint64_t value() const { return m_time_used; }
67
68 double seconds() const { return milliseconds() / 1000.0; }
69
70 double milliseconds() const { return value() / 1000000.0; }
71
72 double ms_per_event() const { return milliseconds() / events(); }
73
74 uint64_t cycles_consumed() const {
75 if(m_clock_speed != 0) {
76 return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
77 }
78 return m_cpu_cycles_used;
79 }
80
81 uint64_t events() const { return m_event_count * m_event_mult; }
82
83 const std::string& get_name() const { return m_name; }
84
85 const std::string& doing() const { return m_doing; }
86
87 size_t buf_size() const { return m_buf_size; }
88
89 double bytes_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
90
91 double events_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
92
93 double seconds_per_event() const { return events() > 0 ? seconds() / events() : 0.0; }
94
95 void set_custom_msg(std::string_view s) { m_custom_msg = s; }
96
97 bool operator<(const Timer& other) const;
98
99 std::string to_string() const;
100
101 private:
102 std::string result_string_bps() const;
103 std::string result_string_ops() const;
104
105 // const data
106 std::string m_name, m_doing;
107 size_t m_buf_size;
108 uint64_t m_event_mult;
109 double m_clock_cycle_ratio;
110 uint64_t m_clock_speed;
111
112 // set at runtime
113 std::string m_custom_msg;
114 uint64_t m_time_used = 0, m_timer_start = 0;
115 uint64_t m_event_count = 0;
116
117 uint64_t m_max_time = 0, m_min_time = 0;
118 uint64_t m_cpu_cycles_start = 0, m_cpu_cycles_used = 0;
119};
120
121} // namespace Botan
122
123#endif
Timer_Scope(Timer &timer)
Definition timer.h:41
double milliseconds() const
Definition timer.h:70
Timer(std::string_view name, size_t buf_size)
Definition timer.h:28
bool under(std::chrono::milliseconds msec) const
Definition timer.h:37
double events_per_second() const
Definition timer.h:91
uint64_t value() const
Definition timer.h:66
uint64_t cycles_consumed() const
Definition timer.h:74
Timer & operator=(const Timer &other)=default
Timer(std::string_view name)
Definition timer.h:26
double ms_per_event() const
Definition timer.h:72
double seconds_per_event() const
Definition timer.h:93
auto run(F f) -> decltype(f())
Definition timer.h:54
Timer(const Timer &other)=default
void set_custom_msg(std::string_view s)
Definition timer.h:95
const std::string & get_name() const
Definition timer.h:83
size_t buf_size() const
Definition timer.h:87
uint64_t events() const
Definition timer.h:81
const std::string & doing() const
Definition timer.h:85
void run_until_elapsed(std::chrono::milliseconds msec, F f)
Definition timer.h:60
double seconds() const
Definition timer.h:68
double bytes_per_second() const
Definition timer.h:89
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition compiler.h:51
bool operator<(const OID &a, const OID &b)
Definition asn1_oid.cpp:158