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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 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 <chrono>
13
#include <iomanip>
14
#include <map>
15
#include <set>
16
#include <sstream>
17

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

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

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

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

34
namespace Botan_CLI {
35

36
namespace {
37

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

202
      buf.insert(x);
26✔
203
   }
28✔
204

205
   return std::vector<size_t>(buf.begin(), buf.end());
28✔
206
}
25✔
207

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

211
   std::ostringstream oss;
494✔
212

213
   oss << t.get_name() << " ";
494✔
214

215
   const uint64_t events = t.events();
494✔
216

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

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

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

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

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

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

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

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

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

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

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

269
   return oss.str();
988✔
270
}
494✔
271

272
}  // namespace
273

274
class Speed final : public Command {
×
275
   public:
276
      Speed() :
29✔
277
            Command(
278
               "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") {
58✔
279
      }
29✔
280

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

311
            /* Cipher modes */
312
            "AES-128/CBC",
313
            "AES-128/CTR-BE",
314
            "AES-128/EAX",
315
            "AES-128/OCB",
316
            "AES-128/GCM",
317
            "AES-128/XTS",
318
            "AES-128/SIV",
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 {
28✔
379
         std::chrono::milliseconds msec(get_arg_sz("msec"));
28✔
380
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
56✔
381
         const std::string format = get_arg("format");
28✔
382
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
31✔
383

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

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

388
         m_time_unit = [](std::string_view tu) {
115✔
389
            if(tu == "ms") {
28✔
390
               return 1000;
28✔
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"));
28✔
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) {
28✔
407
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
408
         }
409

410
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
28✔
411

412
#if defined(BOTAN_HAS_OS_UTILS)
413
         if(clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
28✔
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") {
28✔
421
            m_summary = std::make_unique<Summary>();
1✔
422
         } else if(format == "json") {
27✔
423
            m_json = std::make_unique<JSON_Output>();
1✔
424
         } else if(format != "default") {
26✔
425
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
426
         }
427

428
#if defined(BOTAN_HAS_ECC_GROUP)
429
         if(ecc_groups.empty()) {
28✔
430
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
224✔
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
               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");
31✔
446

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

449
#if defined(BOTAN_HAS_CPUID)
450
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
26✔
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
         }
25✔
457
#endif
458

459
         if(verbose() || m_summary) {
25✔
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());
25✔
469
         if(using_defaults) {
25✔
470
            algos = default_benchmark_list();
×
471
         }
472

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

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

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

497
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
25✔
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
      }
115✔
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) {
496✔
513
         m_ns_taken += t.value();
496✔
514
         m_cycles_consumed += t.cycles_consumed();
496✔
515
         if(m_json) {
496✔
516
            m_json->add(t);
2✔
517
         } else {
518
            output() << format_timer(t, m_time_unit) << "\n";
988✔
519

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

527
BOTAN_REGISTER_COMMAND("speed", Speed);
29✔
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