Coverage Report

Created: 2025-12-05 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.4.0/build/include/internal/botan/internal/timer.h
Line
Count
Source
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_TIMER_H_
8
#define BOTAN_TIMER_H_
9
10
#include <botan/types.h>
11
#include <chrono>
12
#include <string>
13
14
namespace Botan {
15
16
class BOTAN_TEST_API Timer final {
17
   public:
18
      Timer(std::string_view name,
19
            std::string_view provider,
20
            std::string_view doing,
21
            uint64_t event_mult,
22
            size_t buf_size,
23
            double clock_cycle_ratio,
24
            uint64_t clock_speed);
25
26
0
      Timer(std::string_view name) : Timer(name, "", "", 1, 0, 0.0, 0) {}
27
28
0
      Timer(std::string_view name, size_t buf_size) : Timer(name, "", "", buf_size, buf_size, 0.0, 0) {}
29
30
      Timer(const Timer& other) = default;
31
      Timer& operator=(const Timer& other) = default;
32
33
      void start();
34
35
      void stop();
36
37
0
      bool under(std::chrono::milliseconds msec) const { return (milliseconds() < msec.count()); }
38
39
      class Timer_Scope final {
40
         public:
41
0
            explicit Timer_Scope(Timer& timer) : m_timer(timer) { m_timer.start(); }
42
43
0
            ~Timer_Scope() {
44
0
               try {
45
0
                  m_timer.stop();
46
0
               } catch(...) {}
47
0
            }
48
49
         private:
50
            Timer& m_timer;
51
      };
52
53
      template <typename F>
54
0
      auto run(F f) -> decltype(f()) {
55
0
         Timer_Scope timer(*this);
56
0
         return f();
57
0
      }
58
59
      template <typename F>
60
0
      void run_until_elapsed(std::chrono::milliseconds msec, F f) {
61
0
         while(this->under(msec)) {
62
0
            run(f);
63
0
         }
64
0
      }
65
66
0
      uint64_t value() const { return m_time_used; }
67
68
0
      double seconds() const { return milliseconds() / 1000.0; }
69
70
0
      double milliseconds() const { return value() / 1000000.0; }
71
72
0
      double ms_per_event() const { return milliseconds() / events(); }
73
74
0
      uint64_t cycles_consumed() const {
75
0
         if(m_clock_speed != 0) {
76
0
            return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
77
0
         }
78
0
         return m_cpu_cycles_used;
79
0
      }
80
81
0
      uint64_t events() const { return m_event_count * m_event_mult; }
82
83
0
      const std::string& get_name() const { return m_name; }
84
85
0
      const std::string& doing() const { return m_doing; }
86
87
0
      size_t buf_size() const { return m_buf_size; }
88
89
0
      double bytes_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
90
91
0
      double events_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
92
93
0
      double seconds_per_event() const { return events() > 0 ? seconds() / events() : 0.0; }
94
95
0
      void set_custom_msg(std::string_view s) { m_custom_msg = s; }
96
97
      bool operator<(const Timer& other) const;
98
99
      std::string to_string() const;
100
101
   private:
102
      std::string result_string_bps() const;
103
      std::string result_string_ops() const;
104
105
      // const data
106
      std::string m_name, m_doing;
107
      size_t m_buf_size;
108
      uint64_t m_event_mult;
109
      double m_clock_cycle_ratio;
110
      uint64_t m_clock_speed;
111
112
      // set at runtime
113
      std::string m_custom_msg;
114
      uint64_t m_time_used = 0, m_timer_start = 0;
115
      uint64_t m_event_count = 0;
116
117
      uint64_t m_max_time = 0, m_min_time = 0;
118
      uint64_t m_cpu_cycles_start = 0, m_cpu_cycles_used = 0;
119
};
120
121
}  // namespace Botan
122
123
#endif