• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 21768358452

06 Feb 2026 10:35PM UTC coverage: 90.064% (-0.003%) from 90.067%
21768358452

Pull #5289

github

web-flow
Merge f589db195 into 8ea0ca252
Pull Request #5289: Further misc header reductions, forward declarations, etc

102238 of 113517 relevant lines covered (90.06%)

11357432.36 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.78
/src/cli/timer.h
1
/*
2
* (C) 2018,2024 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#ifndef BOTAN_CLI_TIMER_H_
8
#define BOTAN_CLI_TIMER_H_
9

10
#include <botan/types.h>
11
#include <string>
12

13
namespace Botan_CLI {
14

15
class Timer final {
178✔
16
   public:
17
      Timer(std::string_view name,
18
            std::string_view provider,
19
            std::string_view doing,
20
            uint64_t event_mult,
21
            size_t buf_size,
22
            double clock_cycle_ratio,
23
            uint64_t clock_speed);
24

25
      explicit Timer(std::string_view name) : Timer(name, "", "", 1, 0, 0.0, 0) {}
26

27
      Timer(std::string_view name, size_t buf_size) : Timer(name, "", "", buf_size, buf_size, 0.0, 0) {}
28

29
      void start();
30

31
      void stop();
32

33
      bool under(uint64_t msec) const {
918✔
34
         const uint64_t nano = msec * 1000000;
918✔
35
         return value() < nano;
894✔
36
      }
37

38
      class Timer_Scope final {
39
         public:
40
            explicit Timer_Scope(Timer& timer) : m_timer(timer) { m_timer.start(); }
642✔
41

42
            ~Timer_Scope() {
642✔
43
               try {
642✔
44
                  m_timer.stop();
642✔
45
               } catch(...) {}
×
46
            }
642✔
47

48
            Timer_Scope(const Timer_Scope& other) = delete;
49
            Timer_Scope(Timer_Scope&& other) = delete;
50
            Timer_Scope& operator=(const Timer_Scope& other) = delete;
51
            Timer_Scope& operator=(Timer_Scope&& other) = delete;
52

53
         private:
54
            Timer& m_timer;
55
      };
56

57
      template <typename F>
58
      auto run(F f) -> decltype(f()) {
642✔
59
         const Timer_Scope timer(*this);
642✔
60
         return f();
1,270✔
61
      }
642✔
62

63
      template <typename F>
64
      void run_until_elapsed(uint64_t msec, F f) {
23✔
65
         while(this->under(msec)) {
240✔
66
            run(f);
217✔
67
         }
68
      }
69

70
      uint64_t value() const { return m_time_used; }
970✔
71

72
      double seconds() const { return nanoseconds() / 1000000000.0; }
169✔
73

74
      double milliseconds() const { return nanoseconds() / 1000000.0; }
311✔
75

76
      double microseconds() const { return nanoseconds() / 1000.0; }
×
77

78
      double nanoseconds() const { return static_cast<double>(value()); }
334✔
79

80
      uint64_t cycles_consumed() const {
340✔
81
         if(m_clock_speed != 0) {
340✔
82
            return (m_clock_speed * value()) / 1000;
×
83
         }
84
         return m_cpu_cycles_used;
340✔
85
      }
86

87
      uint64_t events() const { return m_event_count * m_event_mult; }
185✔
88

89
      const std::string& get_name() const { return m_name; }
169✔
90

91
      const std::string& doing() const { return m_doing; }
171✔
92

93
      size_t buf_size() const { return m_buf_size; }
220✔
94

95
      double bytes_per_second() const { return events_per_second(); }
2✔
96

97
      double events_per_second() const {
146✔
98
         if(seconds() > 0.0 && events() > 0) {
146✔
99
            return static_cast<double>(events()) / seconds();
146✔
100
         } else {
101
            return 0.0;
102
         }
103
      }
104

105
      double seconds_per_event() const {
×
106
         if(seconds() > 0.0 && events() > 0) {
×
107
            return seconds() / static_cast<double>(events());
×
108
         } else {
109
            return 0.0;
110
         }
111
      }
112

113
      bool operator<(const Timer& other) const;
114

115
   private:
116
      static uint64_t timestamp_ns();
117
      static uint64_t cycle_counter();
118

119
      // const data
120
      std::string m_name, m_doing;
121
      size_t m_buf_size;
122
      uint64_t m_event_mult;
123
      double m_clock_cycle_ratio;
124
      uint64_t m_clock_speed;
125

126
      // set at runtime
127
      uint64_t m_event_count = 0;
128

129
      uint64_t m_time_used = 0;
130
      uint64_t m_timer_start = 0;
131

132
      uint64_t m_cpu_cycles_used = 0;
133
      uint64_t m_cpu_cycles_start = 0;
134
};
135

136
}  // namespace Botan_CLI
137

138
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc