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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 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

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,
726✔
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) :
726✔
37
      m_name(format_timer_name(name, provider)),
726✔
38
      m_doing(doing),
1,452✔
39
      m_buf_size(buf_size),
726✔
40
      m_event_mult(event_mult),
726✔
41
      m_clock_cycle_ratio(clock_cycle_ratio),
726✔
42
      m_clock_speed(clock_speed) {}
726✔
43

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

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

59
      const uint64_t now = OS::get_system_timestamp_ns();
56,852✔
60

61
      if(now > m_timer_start) {
56,852✔
62
         const uint64_t dur = now - m_timer_start;
56,852✔
63

64
         m_time_used += dur;
56,852✔
65

66
         if(m_event_count == 0) {
56,852✔
67
            m_min_time = m_max_time = dur;
726✔
68
         } else {
69
            m_max_time = std::max(m_max_time, dur);
56,126✔
70
            m_min_time = std::min(m_min_time, dur);
56,589✔
71
         }
72
      }
73

74
      m_timer_start = 0;
56,852✔
75
      ++m_event_count;
56,852✔
76
   }
77
}
113,704✔
78

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

84
   return (this->get_name() < other.get_name());
×
85
}
86

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

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

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

103
   std::ostringstream oss;
22✔
104
   oss << get_name();
44✔
105

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

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

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

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

126
   oss << " (" << MiB_total << " MiB in " << milliseconds() << " ms)\n";
22✔
127

128
   return oss.str();
44✔
129
}
22✔
130

131
std::string Timer::result_string_ops() const {
439✔
132
   std::ostringstream oss;
439✔
133

134
   oss << get_name() << " ";
878✔
135

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

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

148
      oss << " (" << events() << " " << (events() == 1 ? "op" : "ops") << " in " << milliseconds() << " ms)\n";
629✔
149
   }
150

151
   return oss.str();
878✔
152
}
439✔
153

154
}  // 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

© 2025 Coveralls, Inc