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

randombit / botan / 15655660863

14 Jun 2025 04:56PM UTC coverage: 90.566% (-0.02%) from 90.585%
15655660863

push

github

web-flow
Merge pull request #4912 from randombit/jack/clang-tidy-headers-part-2

Further clang-tidy fixes in header files

98784 of 109074 relevant lines covered (90.57%)

12380355.99 hits per line

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

83.87
/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 <chrono>
12
#include <string>
13

14
namespace Botan_CLI {
15

16
class Timer final {
506✔
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
      Timer(std::string_view name) : Timer(name, "", "", 1, 0, 0.0, 0) {}
27

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

30
      void start();
31

32
      void stop();
33

34
      bool under(std::chrono::milliseconds msec) const { return (milliseconds() < msec.count()); }
10,889✔
35

36
      class Timer_Scope final {
37
         public:
38
            explicit Timer_Scope(Timer& timer) : m_timer(timer) { m_timer.start(); }
1,437✔
39

40
            ~Timer_Scope() {
1,437✔
41
               try {
1,437✔
42
                  m_timer.stop();
1,437✔
43
               } catch(...) {}
×
44
            }
1,437✔
45

46
            Timer_Scope(const Timer_Scope& other) = delete;
47
            Timer_Scope(Timer_Scope&& other) = delete;
48
            Timer_Scope& operator=(const Timer_Scope& other) = delete;
49
            Timer_Scope& operator=(Timer_Scope&& other) = delete;
50

51
         private:
52
            Timer& m_timer;
53
      };
54

55
      template <typename F>
56
      auto run(F f) -> decltype(f()) {
1,437✔
57
         Timer_Scope timer(*this);
1,437✔
58
         return f();
2,576✔
59
      }
1,437✔
60

61
      template <typename F>
62
      void run_until_elapsed(std::chrono::milliseconds msec, F f) {
23✔
63
         while(this->under(msec)) {
230✔
64
            run(f);
207✔
65
         }
66
      }
67

68
      uint64_t value() const { return m_time_used; }
11,382✔
69

70
      double seconds() const { return value() / 1000000000.0; }
23✔
71

72
      double milliseconds() const { return value() / 1000000.0; }
11,613✔
73

74
      double microseconds() const { return value() / 1000.0; }
×
75

76
      double nanoseconds() const { return static_cast<double>(value()); }
×
77

78
      double ms_per_event() const { return milliseconds() / events(); }
79

80
      uint64_t cycles_consumed() const {
984✔
81
         if(m_clock_speed != 0) {
984✔
82
            return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
×
83
         }
84
         return m_cpu_cycles_used;
984✔
85
      }
86

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

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

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

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

95
      double bytes_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
2✔
96

97
      double events_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
466✔
98

99
      double seconds_per_event() const { return events() > 0 ? seconds() / events() : 0.0; }
×
100

101
      bool operator<(const Timer& other) const;
102

103
   private:
104
      static uint64_t timestamp_ns();
105
      static uint64_t cycle_counter();
106

107
      // const data
108
      std::string m_name, m_doing;
109
      size_t m_buf_size;
110
      uint64_t m_event_mult;
111
      double m_clock_cycle_ratio;
112
      uint64_t m_clock_speed;
113

114
      // set at runtime
115
      uint64_t m_event_count = 0;
116

117
      uint64_t m_time_used = 0;
118
      uint64_t m_timer_start = 0;
119

120
      uint64_t m_cpu_cycles_used = 0;
121
      uint64_t m_cpu_cycles_start = 0;
122
};
123

124
}  // namespace Botan_CLI
125

126
#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