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

randombit / botan / 16174260280

09 Jul 2025 10:51AM UTC coverage: 90.57% (-0.001%) from 90.571%
16174260280

push

github

web-flow
Merge pull request #4964 from randombit/jack/fix-clang-tidy-narrowing-conversions

Fix some clang-tidy bugprone-narrowing-conversions warnings

99048 of 109361 relevant lines covered (90.57%)

12318034.09 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 <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
      explicit 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 {
11,053✔
35
         auto nano = std::chrono::duration_cast<std::chrono::nanoseconds>(msec);
10,630✔
36
         return (nano.count() >= 0) ? (value() < static_cast<size_t>(nano.count())) : true;
10,133✔
37
      }
38

39
      class Timer_Scope final {
40
         public:
41
            explicit Timer_Scope(Timer& timer) : m_timer(timer) { m_timer.start(); }
1,374✔
42

43
            ~Timer_Scope() {
1,374✔
44
               try {
1,374✔
45
                  m_timer.stop();
1,374✔
46
               } catch(...) {}
×
47
            }
1,374✔
48

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

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

58
      template <typename F>
59
      auto run(F f) -> decltype(f()) {
1,374✔
60
         Timer_Scope timer(*this);
1,374✔
61
         return f();
2,495✔
62
      }
1,374✔
63

64
      template <typename F>
65
      void run_until_elapsed(std::chrono::milliseconds msec, F f) {
23✔
66
         while(this->under(msec)) {
246✔
67
            run(f);
206✔
68
         }
69
      }
70

71
      uint64_t value() const { return m_time_used; }
11,325✔
72

73
      double seconds() const { return nanoseconds() / 1000000000.0; }
491✔
74

75
      double milliseconds() const { return nanoseconds() / 1000000.0; }
955✔
76

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

79
      double nanoseconds() const { return static_cast<double>(value()); }
978✔
80

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

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

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

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

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

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

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

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

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

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

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

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

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

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

137
}  // namespace Botan_CLI
138

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