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

randombit / botan / 12469514895

23 Dec 2024 11:07AM UTC coverage: 91.247% (-0.01%) from 91.257%
12469514895

push

github

web-flow
Merge pull request #4490 from randombit/jack/timer-units

Add option to quote performance in microseconds or nanoseconds

93358 of 102314 relevant lines covered (91.25%)

11393255.36 hits per line

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

81.82
/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 {
478✔
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
      Timer(const Timer& other) = default;
10✔
31
      Timer& operator=(const Timer& other) = default;
×
32

33
      void start();
34

35
      void stop();
36

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

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

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

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

53
      template <typename F>
54
      auto run(F f) -> decltype(f()) {
1,419✔
55
         Timer_Scope timer(*this);
1,419✔
56
         return f();
2,505✔
57
      }
1,419✔
58

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

66
      uint64_t value() const { return m_time_used; }
11,738✔
67

68
      double seconds() const { return value() / 1000000000.0; }
23✔
69

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

72
      double microseconds() const { return value() / 1000.0; }
×
73

74
      double nanoseconds() const { return static_cast<double>(value()); }
×
75

76
      double ms_per_event() const { return milliseconds() / events(); }
77

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

85
      uint64_t events() const { return m_event_count * m_event_mult; }
482✔
86

87
      const std::string& get_name() const { return m_name; }
463✔
88

89
      const std::string& doing() const { return m_doing; }
465✔
90

91
      size_t buf_size() const { return m_buf_size; }
514✔
92

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

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

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

99
      bool operator<(const Timer& other) const;
100

101
   private:
102
      // const data
103
      std::string m_name, m_doing;
104
      size_t m_buf_size;
105
      uint64_t m_event_mult;
106
      double m_clock_cycle_ratio;
107
      uint64_t m_clock_speed;
108

109
      // set at runtime
110
      uint64_t m_event_count = 0;
111

112
      uint64_t m_time_used = 0;
113
      uint64_t m_timer_start = 0;
114

115
      uint64_t m_cpu_cycles_used = 0;
116
      uint64_t m_cpu_cycles_start = 0;
117
};
118

119
}  // namespace Botan_CLI
120

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