• 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

91.36
/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) {
726✔
19
   if(provider.empty() || provider == "base")
743✔
20
      return std::string(name);
1,449✔
21

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

27
}
28

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

43
void Timer::start() {
69,598✔
44
   stop();
69,598✔
45
   m_timer_start = OS::get_system_timestamp_ns();
69,598✔
46
   m_cpu_cycles_start = OS::get_cpu_cycle_counter();
69,598✔
47
}
69,598✔
48

49
void Timer::stop() {
139,196✔
50
   if(m_timer_start) {
139,196✔
51
      if(m_cpu_cycles_start != 0) {
69,598✔
52
         const uint64_t cycles_taken = OS::get_cpu_cycle_counter() - m_cpu_cycles_start;
69,598✔
53
         if(cycles_taken > 0) {
69,598✔
54
            m_cpu_cycles_used += static_cast<size_t>(cycles_taken * m_clock_cycle_ratio);
69,598✔
55
         }
56
      }
57

58
      const uint64_t now = OS::get_system_timestamp_ns();
69,598✔
59

60
      if(now > m_timer_start) {
69,598✔
61
         const uint64_t dur = now - m_timer_start;
69,598✔
62

63
         m_time_used += dur;
69,598✔
64

65
         if(m_event_count == 0) {
69,598✔
66
            m_min_time = m_max_time = dur;
726✔
67
         } else {
68
            m_max_time = std::max(m_max_time, dur);
68,872✔
69
            m_min_time = std::min(m_min_time, dur);
69,328✔
70
         }
71
      }
72

73
      m_timer_start = 0;
69,598✔
74
      ++m_event_count;
69,598✔
75
   }
76
}
139,196✔
77

78
bool Timer::operator<(const Timer& other) const {
×
79
   if(this->doing() != other.doing())
×
80
      return (this->doing() < other.doing());
×
81

82
   return (this->get_name() < other.get_name());
×
83
}
84

85
std::string Timer::to_string() const {
465✔
86
   if(!m_custom_msg.empty()) {
465✔
87
      return m_custom_msg;
4✔
88
   } else if(this->buf_size() == 0) {
461✔
89
      return result_string_ops();
439✔
90
   } else {
91
      return result_string_bps();
22✔
92
   }
93
}
94

95
std::string Timer::result_string_bps() const {
22✔
96
   const size_t MiB = 1024 * 1024;
22✔
97

98
   const double MiB_total = static_cast<double>(events()) / MiB;
22✔
99
   const double MiB_per_sec = MiB_total / seconds();
22✔
100

101
   std::ostringstream oss;
22✔
102
   oss << get_name();
44✔
103

104
   if(!doing().empty()) {
44✔
105
      oss << " " << doing();
44✔
106
   }
107

108
   if(buf_size() > 0) {
22✔
109
      oss << " buffer size " << buf_size() << " bytes:";
22✔
110
   }
111

112
   if(events() == 0)
22✔
113
      oss << " "
×
114
          << "N/A";
×
115
   else
116
      oss << " " << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec";
22✔
117

118
   if(cycles_consumed() != 0) {
44✔
119
      const double cycles_per_byte = static_cast<double>(cycles_consumed()) / events();
22✔
120
      oss << " " << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte";
22✔
121
   }
122

123
   oss << " (" << MiB_total << " MiB in " << milliseconds() << " ms)\n";
22✔
124

125
   return oss.str();
44✔
126
}
22✔
127

128
std::string Timer::result_string_ops() const {
439✔
129
   std::ostringstream oss;
439✔
130

131
   oss << get_name() << " ";
878✔
132

133
   if(events() == 0) {
439✔
134
      oss << "no events\n";
×
135
   } else {
136
      oss << static_cast<uint64_t>(events_per_second()) << ' ' << doing() << "/sec; " << std::setprecision(2)
1,317✔
137
          << std::fixed << ms_per_event() << " ms/op";
439✔
138

139
      if(cycles_consumed() != 0) {
878✔
140
         const double cycles_per_op = static_cast<double>(cycles_consumed()) / events();
439✔
141
         const int precision = (cycles_per_op < 10000) ? 2 : 0;
439✔
142
         oss << " " << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op";
439✔
143
      }
144

145
      oss << " (" << events() << " " << (events() == 1 ? "op" : "ops") << " in " << milliseconds() << " ms)\n";
633✔
146
   }
147

148
   return oss.str();
878✔
149
}
439✔
150

151
}
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

© 2025 Coveralls, Inc