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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

88.24
/src/lib/utils/timer.h
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
      Timer(std::string_view name) : Timer(name, "", "", 1, 0, 0.0, 0) {}
254✔
27

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

30
      Timer(const Timer& other) = default;
15✔
31
      Timer& operator=(const Timer& other) = default;
×
32

33
      void start();
34

35
      void stop();
36

37
      bool under(std::chrono::milliseconds msec) { return (milliseconds() < msec.count()); }
64,867✔
38

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

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

49
         private:
50
            Timer& m_timer;
51
      };
52

53
      template <typename F>
54
      auto run(F f) -> decltype(f()) {
59,800✔
55
         Timer_Scope timer(*this);
59,800✔
56
         return f();
60,998✔
57
      }
59,800✔
58

59
      template <typename F>
60
      void run_until_elapsed(std::chrono::milliseconds msec, F f) {
288✔
61
         while(this->under(msec)) {
45,389✔
62
            run(f);
45,661✔
63
         }
64
      }
65

66
      uint64_t value() const { return m_time_used; }
2,645✔
67

68
      double seconds() const { return milliseconds() / 1000.0; }
464✔
69

70
      double milliseconds() const { return value() / 1000000.0; }
63,534✔
71

72
      double ms_per_event() const { return milliseconds() / events(); }
439✔
73

74
      uint64_t cycles_consumed() const {
932✔
75
         if(m_clock_speed != 0) {
932✔
76
            return static_cast<uint64_t>((m_clock_speed * value()) / 1000.0);
×
77
         }
78
         return m_cpu_cycles_used;
932✔
79
      }
80

81
      uint64_t events() const { return m_event_count * m_event_mult; }
2,577✔
82

83
      const std::string get_name() const { return m_name; }
928✔
84

85
      const std::string doing() const { return m_doing; }
974✔
86

87
      size_t buf_size() const { return m_buf_size; }
512✔
88

89
      double bytes_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
3✔
90

91
      double events_per_second() const { return seconds() > 0.0 ? events() / seconds() : 0.0; }
439✔
92

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

95
      void set_custom_msg(std::string_view s) { m_custom_msg = s; }
4✔
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
}
122

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