Botan 3.0.0
Crypto and TLS for C&
Classes | Public Member Functions | List of all members
Botan::Timer Class Referencefinal

#include <timer.h>

Classes

class  Timer_Scope
 

Public Member Functions

size_t buf_size () const
 
double bytes_per_second () const
 
uint64_t cycles_consumed () const
 
const std::string doing () const
 
uint64_t events () const
 
double events_per_second () const
 
const std::string get_name () const
 
double milliseconds () const
 
double ms_per_event () const
 
bool operator< (const Timer &other) const
 
Timeroperator= (const Timer &other)=default
 
template<typename F >
auto run (F f) -> decltype(f())
 
template<typename F >
void run_until_elapsed (std::chrono::milliseconds msec, F f)
 
double seconds () const
 
double seconds_per_event () const
 
void set_custom_msg (std::string_view s)
 
void start ()
 
void stop ()
 
 Timer (const Timer &other)=default
 
 Timer (std::string_view name)
 
 Timer (std::string_view name, size_t buf_size)
 
 Timer (std::string_view name, std::string_view provider, std::string_view doing, uint64_t event_mult, size_t buf_size, double clock_cycle_ratio, uint64_t clock_speed)
 
std::string to_string () const
 
bool under (std::chrono::milliseconds msec)
 
uint64_t value () const
 

Detailed Description

Definition at line 16 of file timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/4]

Botan::Timer::Timer ( std::string_view  name,
std::string_view  provider,
std::string_view  doing,
uint64_t  event_mult,
size_t  buf_size,
double  clock_cycle_ratio,
uint64_t  clock_speed 
)

Definition at line 30 of file timer.cpp.

37 : m_name(format_timer_name(name, provider))
38 , m_doing(doing)
39 , m_buf_size(buf_size)
40 , m_event_mult(event_mult)
41 , m_clock_cycle_ratio(clock_cycle_ratio)
42 , m_clock_speed(clock_speed)
43 {}
const std::string doing() const
Definition: timer.h:123
size_t buf_size() const
Definition: timer.h:128
std::string name

◆ Timer() [2/4]

Botan::Timer::Timer ( std::string_view  name)
inline

Definition at line 27 of file timer.h.

27 :
28 Timer(name, "", "", 1, 0, 0.0, 0)
29 {}
Timer(std::string_view name, std::string_view provider, std::string_view doing, uint64_t event_mult, size_t buf_size, double clock_cycle_ratio, uint64_t clock_speed)
Definition: timer.cpp:30

◆ Timer() [3/4]

Botan::Timer::Timer ( std::string_view  name,
size_t  buf_size 
)
inline

Definition at line 31 of file timer.h.

31 :
32 Timer(name, "", "", buf_size, buf_size, 0.0, 0)
33 {}

◆ Timer() [4/4]

Botan::Timer::Timer ( const Timer other)
default

Member Function Documentation

◆ buf_size()

size_t Botan::Timer::buf_size ( ) const
inline

Definition at line 128 of file timer.h.

129 {
130 return m_buf_size;
131 }

Referenced by to_string().

◆ bytes_per_second()

double Botan::Timer::bytes_per_second ( ) const
inline

Definition at line 133 of file timer.h.

134 {
135 return seconds() > 0.0 ? events() / seconds() : 0.0;
136 }
uint64_t events() const
Definition: timer.h:113
double seconds() const
Definition: timer.h:89

Referenced by Botan::RFC4880_S2K_Family::tune().

◆ cycles_consumed()

uint64_t Botan::Timer::cycles_consumed ( ) const
inline

Definition at line 104 of file timer.h.

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 }
uint64_t value() const
Definition: timer.h:84

◆ doing()

const std::string Botan::Timer::doing ( ) const
inline

Definition at line 123 of file timer.h.

124 {
125 return m_doing;
126 }

Referenced by operator<().

◆ events()

uint64_t Botan::Timer::events ( ) const
inline

Definition at line 113 of file timer.h.

114 {
115 return m_event_count * m_event_mult;
116 }

Referenced by Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().

◆ events_per_second()

double Botan::Timer::events_per_second ( ) const
inline

Definition at line 138 of file timer.h.

139 {
140 return seconds() > 0.0 ? events() / seconds() : 0.0;
141 }

◆ get_name()

const std::string Botan::Timer::get_name ( ) const
inline

Definition at line 118 of file timer.h.

119 {
120 return m_name;
121 }

Referenced by operator<().

◆ milliseconds()

double Botan::Timer::milliseconds ( ) const
inline

Definition at line 94 of file timer.h.

95 {
96 return value() / 1000000.0;
97 }

◆ ms_per_event()

double Botan::Timer::ms_per_event ( ) const
inline

Definition at line 99 of file timer.h.

100 {
101 return milliseconds() / events();
102 }
double milliseconds() const
Definition: timer.h:94

◆ operator<()

bool Botan::Timer::operator< ( const Timer other) const

Definition at line 89 of file timer.cpp.

90 {
91 if(this->doing() != other.doing())
92 return (this->doing() < other.doing());
93
94 return (this->get_name() < other.get_name());
95 }
const std::string get_name() const
Definition: timer.h:118

References doing(), and get_name().

◆ operator=()

Timer & Botan::Timer::operator= ( const Timer other)
default

◆ run()

template<typename F >
auto Botan::Timer::run ( f) -> decltype(f())
inline

Definition at line 69 of file timer.h.

70 {
71 Timer_Scope timer(*this);
72 return f();
73 }

◆ run_until_elapsed()

template<typename F >
void Botan::Timer::run_until_elapsed ( std::chrono::milliseconds  msec,
f 
)
inline

Definition at line 76 of file timer.h.

77 {
78 while(this->under(msec))
79 {
80 run(f);
81 }
82 }
bool under(std::chrono::milliseconds msec)
Definition: timer.h:42
auto run(F f) -> decltype(f())
Definition: timer.h:69

Referenced by Botan::RFC4880_S2K_Family::tune(), Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().

◆ seconds()

double Botan::Timer::seconds ( ) const
inline

Definition at line 89 of file timer.h.

90 {
91 return milliseconds() / 1000.0;
92 }

◆ seconds_per_event()

double Botan::Timer::seconds_per_event ( ) const
inline

Definition at line 143 of file timer.h.

144 {
145 return events() > 0 ? seconds() / events() : 0.0;
146 }

◆ set_custom_msg()

void Botan::Timer::set_custom_msg ( std::string_view  s)
inline

Definition at line 148 of file timer.h.

149 {
150 m_custom_msg = s;
151 }

◆ start()

void Botan::Timer::start ( )

Definition at line 45 of file timer.cpp.

46 {
47 stop();
48 m_timer_start = OS::get_system_timestamp_ns();
49 m_cpu_cycles_start = OS::get_cpu_cycle_counter();
50 }
void stop()
Definition: timer.cpp:52
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition: os_utils.cpp:320
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()
Definition: os_utils.cpp:177

References Botan::OS::get_cpu_cycle_counter(), Botan::OS::get_system_timestamp_ns(), and stop().

◆ stop()

void Botan::Timer::stop ( )

Definition at line 52 of file timer.cpp.

53 {
54 if(m_timer_start)
55 {
56 if(m_cpu_cycles_start != 0)
57 {
58 const uint64_t cycles_taken = OS::get_cpu_cycle_counter() - m_cpu_cycles_start;
59 if(cycles_taken > 0)
60 {
61 m_cpu_cycles_used += static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
62 }
63 }
64
65 const uint64_t now = OS::get_system_timestamp_ns();
66
67 if(now > m_timer_start)
68 {
69 const uint64_t dur = now - m_timer_start;
70
71 m_time_used += dur;
72
73 if(m_event_count == 0)
74 {
75 m_min_time = m_max_time = dur;
76 }
77 else
78 {
79 m_max_time = std::max(m_max_time, dur);
80 m_min_time = std::min(m_min_time, dur);
81 }
82 }
83
84 m_timer_start = 0;
85 ++m_event_count;
86 }
87 }

References Botan::OS::get_cpu_cycle_counter(), and Botan::OS::get_system_timestamp_ns().

Referenced by start().

◆ to_string()

std::string Botan::Timer::to_string ( ) const

Definition at line 97 of file timer.cpp.

98 {
99 if(!m_custom_msg.empty())
100 {
101 return m_custom_msg;
102 }
103 else if(this->buf_size() == 0)
104 {
105 return result_string_ops();
106 }
107 else
108 {
109 return result_string_bps();
110 }
111 }

References buf_size().

◆ under()

bool Botan::Timer::under ( std::chrono::milliseconds  msec)
inline

Definition at line 42 of file timer.h.

43 {
44
45 return (milliseconds() < msec.count());
46 }

◆ value()

uint64_t Botan::Timer::value ( ) const
inline

Definition at line 84 of file timer.h.

85 {
86 return m_time_used;
87 }

Referenced by Botan::Argon2_Family::tune(), Botan::Bcrypt_PBKDF_Family::tune(), and Botan::Scrypt_Family::tune().


The documentation for this class was generated from the following files: