Botan 3.3.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) const
 
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.

36 :
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) {}
size_t buf_size() const
Definition timer.h:87
const std::string & doing() const
Definition timer.h:85
std::string name

◆ Timer() [2/4]

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

Definition at line 26 of file timer.h.

26: Timer(name, "", "", 1, 0, 0.0, 0) {}
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 28 of file timer.h.

28: Timer(name, "", "", buf_size, buf_size, 0.0, 0) {}

◆ 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 87 of file timer.h.

87{ return m_buf_size; }

Referenced by to_string().

◆ bytes_per_second()

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

Definition at line 89 of file timer.h.

89{ return seconds() > 0.0 ? events() / seconds() : 0.0; }
uint64_t events() const
Definition timer.h:81
double seconds() const
Definition timer.h:68

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

◆ cycles_consumed()

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

Definition at line 74 of file timer.h.

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

◆ doing()

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

Definition at line 85 of file timer.h.

85{ return m_doing; }

Referenced by operator<().

◆ events()

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

Definition at line 81 of file timer.h.

81{ return m_event_count * m_event_mult; }

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 91 of file timer.h.

91{ return seconds() > 0.0 ? events() / seconds() : 0.0; }

◆ get_name()

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

Definition at line 83 of file timer.h.

83{ return m_name; }

Referenced by operator<().

◆ milliseconds()

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

Definition at line 70 of file timer.h.

70{ return value() / 1000000.0; }

◆ ms_per_event()

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

Definition at line 72 of file timer.h.

72{ return milliseconds() / events(); }
double milliseconds() const
Definition timer.h:70

◆ operator<()

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

Definition at line 79 of file timer.cpp.

79 {
80 if(this->doing() != other.doing()) {
81 return (this->doing() < other.doing());
82 }
83
84 return (this->get_name() < other.get_name());
85}
const std::string & get_name() const
Definition timer.h:83

References doing(), and get_name().

◆ operator=()

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

◆ run()

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

Definition at line 54 of file timer.h.

54 {
55 Timer_Scope timer(*this);
56 return f();
57 }

◆ run_until_elapsed()

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

Definition at line 60 of file timer.h.

60 {
61 while(this->under(msec)) {
62 run(f);
63 }
64 }
bool under(std::chrono::milliseconds msec) const
Definition timer.h:37
auto run(F f) -> decltype(f())
Definition timer.h:54

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 68 of file timer.h.

68{ return milliseconds() / 1000.0; }

◆ seconds_per_event()

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

Definition at line 93 of file timer.h.

93{ return events() > 0 ? seconds() / events() : 0.0; }

◆ set_custom_msg()

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

Definition at line 95 of file timer.h.

95{ m_custom_msg = s; }

◆ start()

void Botan::Timer::start ( )

Definition at line 44 of file timer.cpp.

44 {
45 stop();
46 m_timer_start = OS::get_system_timestamp_ns();
47 m_cpu_cycles_start = OS::get_cpu_cycle_counter();
48}
void stop()
Definition timer.cpp:50
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
Definition os_utils.cpp:316
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 50 of file timer.cpp.

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

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 87 of file timer.cpp.

87 {
88 if(!m_custom_msg.empty()) {
89 return m_custom_msg;
90 } else if(this->buf_size() == 0) {
91 return result_string_ops();
92 } else {
93 return result_string_bps();
94 }
95}

References buf_size().

◆ under()

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

Definition at line 37 of file timer.h.

37{ return (milliseconds() < msec.count()); }

◆ value()

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

Definition at line 66 of file timer.h.

66{ return m_time_used; }

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: