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

randombit / botan / 21848324667

10 Feb 2026 01:45AM UTC coverage: 90.069% (-0.001%) from 90.07%
21848324667

push

github

web-flow
Merge pull request #5304 from KaganCanSit/remove-unused-chrono-include

Remove unused chrono include from cli speed file

102222 of 113493 relevant lines covered (90.07%)

11441923.83 hits per line

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

75.68
/src/cli/speed.cpp
1
/*
2
* (C) 2009,2010,2014,2015,2017,2018,2024 Jack Lloyd
3
* (C) 2015 Simon Warta (Kullo GmbH)
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "cli.h"
9
#include "perf.h"
10

11
#include <algorithm>
12
#include <iomanip>
13
#include <map>
14
#include <set>
15
#include <sstream>
16

17
// Always available:
18
#include <botan/version.h>
19
#include <botan/internal/target_info.h>
20

21
#if defined(BOTAN_HAS_CPUID)
22
   #include <botan/internal/cpuid.h>
23
#endif
24

25
#if defined(BOTAN_HAS_OS_UTILS)
26
   #include <botan/internal/os_utils.h>
27
#endif
28

29
#if defined(BOTAN_HAS_ECC_GROUP)
30
   #include <botan/ec_group.h>
31
#endif
32

33
namespace Botan_CLI {
34

35
namespace {
36

37
class JSON_Output final {
2✔
38
   public:
39
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
40

41
      std::string print() const {
1✔
42
         std::ostringstream out;
1✔
43

44
         out << "[\n";
1✔
45

46
         out << "{"
1✔
47
             << R"("arch": ")" << BOTAN_TARGET_ARCH << "\", "
48
             << R"("version": ")" << Botan::short_version_cstr() << "\", ";
1✔
49

50
         if(auto vc_revision = Botan::version_vc_revision()) {
1✔
51
            out << R"("git": ")" << *vc_revision << "\", ";
×
52
         }
×
53

54
         out << R"("compiler": ")" << BOTAN_COMPILER_INVOCATION_STRING << "\""
1✔
55
             << "},\n";
1✔
56

57
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
58
            const Timer& t = m_results[i];
2✔
59

60
            out << "{"
2✔
61
                << R"("algo": ")" << t.get_name() << "\", "
2✔
62
                << R"("op": ")" << t.doing() << "\", "
2✔
63
                << "\"events\": " << t.events() << ", ";
2✔
64

65
            if(t.cycles_consumed() > 0) {
4✔
66
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
67
            }
68

69
            if(t.buf_size() > 0) {
2✔
70
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.nanoseconds() / 1000000000.0)) << ", ";
2✔
71
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
72
            }
73

74
            out << "\"nanos\": " << t.value() << "}";
2✔
75

76
            if(i != m_results.size() - 1) {
2✔
77
               out << ",";
1✔
78
            }
79

80
            out << "\n";
2✔
81
         }
82
         out << "]\n";
1✔
83

84
         return out.str();
2✔
85
      }
1✔
86

87
   private:
88
      std::vector<Timer> m_results;
89
};
90

91
class Summary final {
1✔
92
   public:
93
      Summary() = default;
1✔
94

95
      void add(const Timer& t) {
2✔
96
         if(t.buf_size() == 0) {
2✔
97
            m_ops_entries.push_back(t);
×
98
         } else {
99
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
100
         }
101
      }
2✔
102

103
      std::string print() {
1✔
104
         const size_t name_padding = 35;
1✔
105
         const size_t op_name_padding = 16;
1✔
106
         const size_t op_padding = 16;
1✔
107

108
         std::ostringstream result_ss;
1✔
109
         result_ss << std::fixed;
1✔
110

111
         if(!m_bps_entries.empty()) {
1✔
112
            result_ss << "\n";
1✔
113

114
            // add table header
115
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
116
                      << "operation";
1✔
117

118
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
119
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
120
            }
121
            result_ss << "\n";
1✔
122

123
            // add table entries
124
            for(const auto& entry : m_bps_entries) {
3✔
125
               if(entry.second.empty()) {
2✔
126
                  continue;
×
127
               }
128

129
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
130
                         << std::left << (entry.first.first);
2✔
131

132
               for(const Timer& t : entry.second) {
4✔
133
                  if(t.events() == 0) {
2✔
134
                     result_ss << std::setw(op_padding) << std::right << "N/A";
×
135
                  } else {
136
                     result_ss << std::setw(op_padding) << std::right << std::setprecision(2)
2✔
137
                               << (t.bytes_per_second() / 1000.0);
2✔
138
                  }
139
               }
140

141
               result_ss << "\n";
2✔
142
            }
143

144
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
145
         }
146

147
         if(!m_ops_entries.empty()) {
1✔
148
            result_ss << std::setprecision(6) << "\n";
×
149

150
            // sort entries
151
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
152

153
            // add table header
154
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
155
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
156
                      << std::right << "op/sec"
×
157
                      << "\n";
×
158

159
            // add table entries
160
            for(const Timer& entry : m_ops_entries) {
×
161
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
162
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
163
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
164
                         << entry.events_per_second() << "\n";
×
165
            }
166
         }
167

168
         return result_ss.str();
2✔
169
      }
1✔
170

171
   private:
172
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
173
      std::vector<Timer> m_ops_entries;
174
};
175

176
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
13✔
177
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
13✔
178

179
   std::set<size_t> buf;
13✔
180
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
24✔
181
      size_t x = 0;
14✔
182
      try {
14✔
183
         size_t converted = 0;
14✔
184
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
14✔
185

186
         if(converted != size_str.size()) {
13✔
187
            throw CLI_Usage_Error("Invalid integer");
×
188
         }
189
      } catch(std::exception&) {
1✔
190
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
2✔
191
      }
1✔
192

193
      if(x == 0) {
13✔
194
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
195
      }
196

197
      if(x > MAX_BUF_SIZE) {
12✔
198
         throw CLI_Usage_Error("Specified buffer size is too large");
2✔
199
      }
200

201
      buf.insert(x);
11✔
202
   }
13✔
203

204
   return std::vector<size_t>(buf.begin(), buf.end());
13✔
205
}
10✔
206

207
std::string format_timer(const Timer& t, size_t time_unit) {
167✔
208
   constexpr size_t MiB = 1024 * 1024;
167✔
209

210
   std::ostringstream oss;
167✔
211

212
   oss << t.get_name() << " ";
167✔
213

214
   const uint64_t events = t.events();
167✔
215

216
   if(t.buf_size() == 0) {
167✔
217
      // Report operations/time unit
218

219
      if(events == 0) {
144✔
220
         oss << "no events ";
×
221
      } else {
222
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
288✔
223

224
         if(time_unit == 1000) {
144✔
225
            oss << std::setprecision(2) << std::fixed << (t.milliseconds() / events) << " ms/op ";
144✔
226
         } else if(time_unit == 1000 * 1000) {
×
227
            oss << std::setprecision(2) << std::fixed << (t.microseconds() / events) << " us/op ";
×
228
         } else if(time_unit == 1000 * 1000 * 1000) {
×
229
            oss << std::setprecision(0) << std::fixed << (t.nanoseconds() / events) << " ns/op ";
×
230
         }
231

232
         if(t.cycles_consumed() != 0 && events > 0) {
288✔
233
            const double cycles_per_op = static_cast<double>(t.cycles_consumed()) / events;
144✔
234
            const int precision = (cycles_per_op < 10000) ? 2 : 0;
144✔
235
            oss << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op ";
144✔
236
         }
237

238
         oss << "(" << events << " " << (events == 1 ? "op" : "ops") << " in " << t.milliseconds() << " ms)";
249✔
239
      }
240
   } else {
241
      // Bulk op - report bytes/time unit
242

243
      const double MiB_total = static_cast<double>(events) / MiB;
23✔
244
      const double MiB_per_sec = MiB_total / t.seconds();
23✔
245

246
      if(!t.doing().empty()) {
23✔
247
         oss << t.doing() << " ";
23✔
248
      }
249

250
      if(t.buf_size() > 0) {
23✔
251
         oss << "buffer size " << t.buf_size() << " bytes: ";
23✔
252
      }
253

254
      if(events == 0) {
23✔
255
         oss << "N/A ";
×
256
      } else {
257
         oss << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec ";
23✔
258
      }
259

260
      if(t.cycles_consumed() != 0 && events > 0) {
46✔
261
         const double cycles_per_byte = static_cast<double>(t.cycles_consumed()) / events;
23✔
262
         oss << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte ";
23✔
263
      }
264

265
      oss << "(" << MiB_total << " MiB in " << t.milliseconds() << " ms)";
23✔
266
   }
267

268
   return oss.str();
334✔
269
}
167✔
270

271
}  // namespace
272

273
class Speed final : public Command {
×
274
   public:
275
      Speed() :
14✔
276
            Command(
277
               "speed --msec=500 --format=default --time-unit=ms --ecc-groups= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
28✔
278
      }
14✔
279

280
      static std::vector<std::string> default_benchmark_list() {
×
281
         /*
282
         This is not intended to be exhaustive: it just hits the high
283
         points of the most interesting or widely used algorithms.
284
         */
285
         // clang-format off
286
         return {
×
287
            /* Block ciphers */
288
            "AES-128",
289
            "AES-192",
290
            "AES-256",
291
            "ARIA-128",
292
            "ARIA-192",
293
            "ARIA-256",
294
            "Blowfish",
295
            "CAST-128",
296
            "Camellia-128",
297
            "Camellia-192",
298
            "Camellia-256",
299
            "DES",
300
            "TripleDES",
301
            "GOST-28147-89",
302
            "IDEA",
303
            "Noekeon",
304
            "SHACAL2",
305
            "SM4",
306
            "Serpent",
307
            "Threefish-512",
308
            "Twofish",
309

310
            /* Cipher modes */
311
            "AES-128/CBC",
312
            "AES-128/CTR-BE",
313
            "AES-128/EAX",
314
            "AES-128/OCB",
315
            "AES-128/GCM",
316
            "AES-128/XTS",
317
            "AES-128/SIV",
318
            "Ascon-AEAD128",
319

320
            "Serpent/CBC",
321
            "Serpent/CTR-BE",
322
            "Serpent/EAX",
323
            "Serpent/OCB",
324
            "Serpent/GCM",
325
            "Serpent/XTS",
326
            "Serpent/SIV",
327

328
            "ChaCha20Poly1305",
329

330
            /* Stream ciphers */
331
            "RC4",
332
            "Salsa20",
333
            "ChaCha20",
334

335
            /* Hashes */
336
            "SHA-1",
337
            "SHA-256",
338
            "SHA-512",
339
            "SHA-3(256)",
340
            "SHA-3(512)",
341
            "Ascon-Hash256",
342
            "RIPEMD-160",
343
            "Skein-512",
344
            "Blake2b",
345
            "Whirlpool",
346

347
            /* XOFs */
348
            "SHAKE-128",
349
            "SHAKE-256",
350
            "Ascon-XOF128",
351

352
            /* MACs */
353
            "CMAC(AES-128)",
354
            "HMAC(SHA-256)",
355

356
            /* pubkey */
357
            "RSA",
358
            "DH",
359
            "ECDH",
360
            "ECDSA",
361
            "Ed25519",
362
            "Ed448",
363
            "X25519",
364
            "X448",
365
            "ML-KEM",
366
            "ML-DSA",
367
            "SLH-DSA",
368
            "FrodoKEM",
369
            "HSS-LMS",
370
         };
×
371
         // clang-format on
372
      }
373

374
      std::string group() const override { return "misc"; }
1✔
375

376
      std::string description() const override { return "Measures the speed of algorithms"; }
1✔
377

378
      void go() override {
13✔
379
         const uint64_t milliseconds = get_arg_sz("msec");
13✔
380
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
26✔
381
         const std::string format = get_arg("format");
13✔
382
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
16✔
383

384
         const size_t clock_speed = get_arg_sz("cpu-clock-speed");
13✔
385

386
         double clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
13✔
387

388
         m_time_unit = [](std::string_view tu) {
55✔
389
            if(tu == "ms") {
13✔
390
               return 1000;
13✔
391
            } else if(tu == "us") {
×
392
               return 1000 * 1000;
×
393
            } else if(tu == "ns") {
×
394
               return 1000 * 1000 * 1000;
×
395
            } else {
396
               throw CLI_Usage_Error("Unknown time unit (supported: ms, us, ns)");
×
397
            }
398
         }(get_arg("time-unit"));
13✔
399

400
         /*
401
         * This argument is intended to be the ratio between the cycle counter
402
         * and the actual machine cycles. It is extremely unlikely that there is
403
         * any machine where the cycle counter increments faster than the actual
404
         * clock.
405
         */
406
         if(clock_cycle_ratio < 0.0 || clock_cycle_ratio > 1.0) {
13✔
407
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
408
         }
409

410
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
13✔
411

412
#if defined(BOTAN_HAS_OS_UTILS)
413
         if(clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
13✔
414
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
415
                              "platforms without access to a cycle counter.\n"
416
                              "Expect incorrect results\n\n";
×
417
         }
418
#endif
419

420
         if(format == "table") {
13✔
421
            m_summary = std::make_unique<Summary>();
1✔
422
         } else if(format == "json") {
12✔
423
            m_json = std::make_unique<JSON_Output>();
1✔
424
         } else if(format != "default") {
11✔
425
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
426
         }
427

428
#if defined(BOTAN_HAS_ECC_GROUP)
429
         if(ecc_groups.empty()) {
13✔
430
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
104✔
431
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
432
            const auto& all = Botan::EC_Group::known_named_groups();
×
433
            ecc_groups.assign(all.begin(), all.end());
×
434
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "generic") {
×
435
            ecc_groups.clear();
×
436
            for(const auto& group_name : Botan::EC_Group::known_named_groups()) {
×
437
               const Botan::EC_Group group(group_name);
×
438
               if(group.engine() == Botan::EC_Group_Engine::Generic) {
×
439
                  ecc_groups.push_back(group_name);
×
440
               }
441
            }
×
442
         }
443
#endif
444

445
         std::vector<std::string> algos = get_arg_list("algos");
16✔
446

447
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
29✔
448

449
#if defined(BOTAN_HAS_CPUID)
450
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
11✔
451
            if(auto bit = Botan::CPUID::bit_from_string(cpuid_to_clear)) {
1✔
452
               Botan::CPUID::clear_cpuid_bit(*bit);
×
453
            } else {
454
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
455
            }
456
         }
10✔
457
#endif
458

459
         if(verbose() || m_summary) {
10✔
460
#if defined(BOTAN_HAS_CPUID)
461
            output() << Botan::version_string() << "\n"
2✔
462
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
463
#else
464
            output() << Botan::version_string() << "\n\n";
465
#endif
466
         }
467

468
         const bool using_defaults = (algos.empty());
10✔
469
         if(using_defaults) {
10✔
470
            algos = default_benchmark_list();
×
471
         }
472

473
         const PerfConfig perf_config([&](const Timer& t) { this->record_result(t); },
179✔
474
                                      clock_speed,
475
                                      clock_cycle_ratio,
476
                                      milliseconds,
477
                                      ecc_groups,
478
                                      buf_sizes,
479
                                      this->error_output(),
480
                                      this->rng());
10✔
481

482
         for(const auto& algo : algos) {
36✔
483
            if(auto perf = PerfTest::get(algo)) {
26✔
484
               perf->go(perf_config);
26✔
485
            } else if(verbose() || !using_defaults) {
×
486
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
487
            }
26✔
488
         }
489

490
         if(m_json) {
10✔
491
            output() << m_json->print();
2✔
492
         }
493
         if(m_summary) {
10✔
494
            output() << m_summary->print() << "\n";
3✔
495
         }
496

497
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
10✔
498
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
499
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
500
            const double MHz = Hz / 1000000;
×
501
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
502
         }
503
      }
55✔
504

505
   private:
506
      size_t m_time_unit = 0;
507
      uint64_t m_cycles_consumed = 0;
508
      uint64_t m_ns_taken = 0;
509
      std::unique_ptr<Summary> m_summary;
510
      std::unique_ptr<JSON_Output> m_json;
511

512
      void record_result(const Timer& t) {
169✔
513
         m_ns_taken += t.value();
169✔
514
         m_cycles_consumed += t.cycles_consumed();
169✔
515
         if(m_json) {
169✔
516
            m_json->add(t);
2✔
517
         } else {
518
            output() << format_timer(t, m_time_unit) << "\n" << std::flush;
334✔
519

520
            if(m_summary) {
167✔
521
               m_summary->add(t);
2✔
522
            }
523
         }
524
      }
169✔
525
};
526

527
BOTAN_REGISTER_COMMAND("speed", Speed);
14✔
528

529
}  // namespace Botan_CLI
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