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) {}
54 if(now > m_timer_start) {
55 m_time_used += (now - m_timer_start);
58 if(m_cpu_cycles_start != 0) {
60 if(cycles_taken > 0) {
61 m_cpu_cycles_used +=
static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
79 if(!m_custom_msg.empty()) {
82 return result_string_ops();
84 return result_string_bps();
88std::string Timer::result_string_bps()
const {
89 const size_t MiB = 1024 * 1024;
91 const double MiB_total =
static_cast<double>(
events()) / MiB;
92 const double MiB_per_sec = MiB_total /
seconds();
94 std::ostringstream oss;
97 if(!
doing().empty()) {
98 oss <<
" " <<
doing();
102 oss <<
" buffer size " <<
buf_size() <<
" bytes:";
109 oss <<
" " << std::fixed << std::setprecision(3) << MiB_per_sec <<
" MiB/sec";
114 oss <<
" " << std::fixed << std::setprecision(2) << cycles_per_byte <<
" cycles/byte";
117 oss <<
" (" << MiB_total <<
" MiB in " <<
milliseconds() <<
" ms)\n";
122std::string Timer::result_string_ops()
const {
123 std::ostringstream oss;
128 oss <<
"no events\n";
135 const int precision = (cycles_per_op < 10000) ? 2 : 0;
136 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()