7#include <botan/internal/timer.h>
9#include <botan/internal/os_utils.h>
18std::string format_timer_name(std::string_view
name, std::string_view provider) {
19 if(provider.empty() || provider ==
"base") {
20 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,
36 uint64_t clock_speed) :
37 m_name(format_timer_name(
name, provider)),
40 m_event_mult(event_mult),
41 m_clock_cycle_ratio(clock_cycle_ratio),
42 m_clock_speed(clock_speed) {}
52 if(m_cpu_cycles_start != 0) {
54 if(cycles_taken > 0) {
55 m_cpu_cycles_used +=
static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
61 if(now > m_timer_start) {
62 const uint64_t dur = now - m_timer_start;
66 if(m_event_count == 0) {
67 m_min_time = m_max_time = dur;
69 m_max_time = std::max(m_max_time, dur);
70 m_min_time = std::min(m_min_time, dur);
88 if(!m_custom_msg.empty()) {
91 return result_string_ops();
93 return result_string_bps();
97std::string Timer::result_string_bps()
const {
98 const size_t MiB = 1024 * 1024;
100 const double MiB_total =
static_cast<double>(
events()) / MiB;
101 const double MiB_per_sec = MiB_total /
seconds();
103 std::ostringstream oss;
106 if(!
doing().empty()) {
107 oss <<
" " <<
doing();
111 oss <<
" buffer size " <<
buf_size() <<
" bytes:";
118 oss <<
" " << std::fixed << std::setprecision(3) << MiB_per_sec <<
" MiB/sec";
123 oss <<
" " << std::fixed << std::setprecision(2) << cycles_per_byte <<
" cycles/byte";
126 oss <<
" (" << MiB_total <<
" MiB in " <<
milliseconds() <<
" ms)\n";
131std::string Timer::result_string_ops()
const {
132 std::ostringstream oss;
137 oss <<
"no events\n";
144 const int precision = (cycles_per_op < 10000) ? 2 : 0;
145 oss <<
" " << std::fixed << std::setprecision(precision) << cycles_per_op <<
" cycles/op";
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
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
const std::string & doing() const
uint64_t BOTAN_TEST_API get_system_timestamp_ns()
uint64_t BOTAN_TEST_API get_cpu_cycle_counter()