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

randombit / botan / 10427677597

16 Aug 2024 11:51PM UTC coverage: 91.282% (+0.005%) from 91.277%
10427677597

push

github

web-flow
Merge pull request #4311 from randombit/jack/timer-fixes

Fix timer usage in benchmarks

87851 of 96241 relevant lines covered (91.28%)

9135190.41 hits per line

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

90.79
/src/lib/utils/timer.cpp
1
/*
2
* (C) 2018 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6

7
#include <botan/internal/timer.h>
8

9
#include <botan/internal/os_utils.h>
10
#include <algorithm>
11
#include <iomanip>
12
#include <sstream>
13

14
namespace Botan {
15

16
namespace {
17

18
std::string format_timer_name(std::string_view name, std::string_view provider) {
902✔
19
   if(provider.empty() || provider == "base") {
919✔
20
      return std::string(name);
899✔
21
   }
22

23
   std::ostringstream out;
3✔
24
   out << name << " [" << provider << "]";
3✔
25
   return out.str();
3✔
26
}
3✔
27

28
}  // namespace
29

30
Timer::Timer(std::string_view name,
902✔
31
             std::string_view provider,
32
             std::string_view doing,
33
             uint64_t event_mult,
34
             size_t buf_size,
35
             double clock_cycle_ratio,
36
             uint64_t clock_speed) :
902✔
37
      m_name(format_timer_name(name, provider)),
902✔
38
      m_doing(doing),
1,804✔
39
      m_buf_size(buf_size),
902✔
40
      m_event_mult(event_mult),
902✔
41
      m_clock_cycle_ratio(clock_cycle_ratio),
902✔
42
      m_clock_speed(clock_speed) {}
902✔
43

44
void Timer::start() {
132,521✔
45
   stop();
132,521✔
46
   m_timer_start = OS::get_system_timestamp_ns();
132,521✔
47
   m_cpu_cycles_start = OS::get_cpu_cycle_counter();
132,521✔
48
}
132,521✔
49

50
void Timer::stop() {
265,042✔
51
   if(m_timer_start) {
265,042✔
52
      const uint64_t now = OS::get_system_timestamp_ns();
132,521✔
53

54
      if(now > m_timer_start) {
132,521✔
55
         m_time_used += (now - m_timer_start);
132,521✔
56
      }
57

58
      if(m_cpu_cycles_start != 0) {
132,521✔
59
         const uint64_t cycles_taken = OS::get_cpu_cycle_counter() - m_cpu_cycles_start;
132,521✔
60
         if(cycles_taken > 0) {
132,521✔
61
            m_cpu_cycles_used += static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
132,521✔
62
         }
63
      }
64

65
      m_timer_start = 0;
132,521✔
66
      ++m_event_count;
132,521✔
67
   }
68
}
265,042✔
69

70
bool Timer::operator<(const Timer& other) const {
×
71
   if(this->doing() != other.doing()) {
×
72
      return (this->doing() < other.doing());
×
73
   }
74

75
   return (this->get_name() < other.get_name());
×
76
}
77

78
std::string Timer::to_string() const {
445✔
79
   if(!m_custom_msg.empty()) {
445✔
80
      return m_custom_msg;
4✔
81
   } else if(this->buf_size() == 0) {
441✔
82
      return result_string_ops();
419✔
83
   } else {
84
      return result_string_bps();
22✔
85
   }
86
}
87

88
std::string Timer::result_string_bps() const {
22✔
89
   const size_t MiB = 1024 * 1024;
22✔
90

91
   const double MiB_total = static_cast<double>(events()) / MiB;
22✔
92
   const double MiB_per_sec = MiB_total / seconds();
22✔
93

94
   std::ostringstream oss;
22✔
95
   oss << get_name();
22✔
96

97
   if(!doing().empty()) {
22✔
98
      oss << " " << doing();
22✔
99
   }
100

101
   if(buf_size() > 0) {
22✔
102
      oss << " buffer size " << buf_size() << " bytes:";
22✔
103
   }
104

105
   if(events() == 0) {
22✔
106
      oss << " "
×
107
          << "N/A";
×
108
   } else {
109
      oss << " " << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec";
22✔
110
   }
111

112
   if(cycles_consumed() != 0) {
44✔
113
      const double cycles_per_byte = static_cast<double>(cycles_consumed()) / events();
22✔
114
      oss << " " << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte";
22✔
115
   }
116

117
   oss << " (" << MiB_total << " MiB in " << milliseconds() << " ms)\n";
22✔
118

119
   return oss.str();
44✔
120
}
22✔
121

122
std::string Timer::result_string_ops() const {
419✔
123
   std::ostringstream oss;
419✔
124

125
   oss << get_name() << " ";
419✔
126

127
   if(events() == 0) {
419✔
128
      oss << "no events\n";
×
129
   } else {
130
      oss << static_cast<uint64_t>(events_per_second()) << ' ' << doing() << "/sec; " << std::setprecision(2)
838✔
131
          << std::fixed << ms_per_event() << " ms/op";
419✔
132

133
      if(cycles_consumed() != 0) {
838✔
134
         const double cycles_per_op = static_cast<double>(cycles_consumed()) / events();
419✔
135
         const int precision = (cycles_per_op < 10000) ? 2 : 0;
419✔
136
         oss << " " << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op";
419✔
137
      }
138

139
      oss << " (" << events() << " " << (events() == 1 ? "op" : "ops") << " in " << milliseconds() << " ms)\n";
643✔
140
   }
141

142
   return oss.str();
838✔
143
}
419✔
144

145
}  // namespace Botan
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