7#include <botan/internal/timer.h>
8#include <botan/internal/os_utils.h>
17std::string format_timer_name(std::string_view
name,
18 std::string_view provider)
20 if(provider.empty() || provider ==
"base")
21 return std::string(
name);
23 std::ostringstream out;
24 out <<
name <<
" [" << provider <<
"]";
31 std::string_view provider,
32 std::string_view doing,
35 double clock_cycle_ratio,
37 : m_name(format_timer_name(
name, provider))
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)
56 if(m_cpu_cycles_start != 0)
61 m_cpu_cycles_used +=
static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
67 if(now > m_timer_start)
69 const uint64_t dur = now - m_timer_start;
73 if(m_event_count == 0)
75 m_min_time = m_max_time = dur;
79 m_max_time = std::max(m_max_time, dur);
80 m_min_time = std::min(m_min_time, dur);
99 if(!m_custom_msg.empty())
105 return result_string_ops();
109 return result_string_bps();
113std::string Timer::result_string_bps()
const
115 const size_t MiB = 1024 * 1024;
117 const double MiB_total =
static_cast<double>(
events()) / MiB;
118 const double MiB_per_sec = MiB_total /
seconds();
120 std::ostringstream oss;
125 oss <<
" " <<
doing();
130 oss <<
" buffer size " <<
buf_size() <<
" bytes:";
136 oss <<
" " << std::fixed << std::setprecision(3) << MiB_per_sec <<
" MiB/sec";
141 oss <<
" " << std::fixed << std::setprecision(2) << cycles_per_byte <<
" cycles/byte";
144 oss <<
" (" << MiB_total <<
" MiB in " <<
milliseconds() <<
" ms)\n";
149std::string Timer::result_string_ops()
const
151 std::ostringstream oss;
157 oss <<
"no events\n";
162 <<
' ' <<
doing() <<
"/sec; "
163 << std::setprecision(2) << std::fixed
169 const int precision = (cycles_per_op < 10000) ? 2 : 0;
170 oss <<
" " << std::fixed << std::setprecision(precision) << cycles_per_op <<
" cycles/op";
173 oss <<
" (" <<
events() <<
" " << (
events() == 1 ?
"op" :
"ops")
bool operator<(const Timer &other) const
double milliseconds() const
double events_per_second() const
uint64_t cycles_consumed() const
double ms_per_event() const
std::string to_string() const
const std::string doing() const
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)
const std::string get_name() const
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()