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

randombit / botan / 26995937053

04 Jun 2026 09:38PM UTC coverage: 89.394% (-2.3%) from 91.672%
26995937053

push

github

web-flow
Merge pull request #5642 from randombit/jack/prefetch-in-ks

Improve prefetching for table based implementations

110588 of 123708 relevant lines covered (89.39%)

11056434.37 hits per line

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

71.0
/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/parsing.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) {
29✔
178
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
29✔
179

180
   std::set<size_t> buf;
29✔
181
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
56✔
182
      if(const auto sz = Botan::parse_sz(size_str)) {
30✔
183
         if(*sz == 0) {
29✔
184
            throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
185
         }
186
         if(*sz > MAX_BUF_SIZE) {
28✔
187
            throw CLI_Usage_Error("Specified buffer size is too large");
2✔
188
         }
189

190
         buf.insert(*sz);
27✔
191
      } else {
192
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
2✔
193
      }
194
   }
29✔
195

196
   return std::vector<size_t>(buf.begin(), buf.end());
29✔
197
}
26✔
198

199
std::string format_timer(const Timer& t, size_t time_unit) {
415✔
200
   constexpr size_t MiB = 1024 * 1024;
415✔
201

202
   std::ostringstream oss;
415✔
203

204
   oss << t.get_name() << " ";
415✔
205

206
   const uint64_t events = t.events();
415✔
207

208
   if(t.buf_size() == 0) {
415✔
209
      // Report operations/time unit
210

211
      if(events == 0) {
392✔
212
         oss << "no events ";
×
213
      } else {
214
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
784✔
215

216
         if(time_unit == 1000) {
392✔
217
            oss << std::setprecision(2) << std::fixed << (t.milliseconds() / events) << " ms/op ";
392✔
218
         } else if(time_unit == 1000 * 1000) {
×
219
            oss << std::setprecision(2) << std::fixed << (t.microseconds() / events) << " us/op ";
×
220
         } else if(time_unit == 1000 * 1000 * 1000) {
×
221
            oss << std::setprecision(0) << std::fixed << (t.nanoseconds() / events) << " ns/op ";
×
222
         }
223

224
         if(t.cycles_consumed() != 0 && events > 0) {
784✔
225
            const double cycles_per_op = static_cast<double>(t.cycles_consumed()) / events;
392✔
226
            const int precision = (cycles_per_op < 10000) ? 2 : 0;
392✔
227
            oss << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op ";
392✔
228
         }
229

230
         oss << "(" << events << " " << (events == 1 ? "op" : "ops") << " in " << t.milliseconds() << " ms)";
666✔
231
      }
232
   } else {
233
      // Bulk op - report bytes/time unit
234

235
      const double MiB_total = static_cast<double>(events) / MiB;
23✔
236
      const double MiB_per_sec = MiB_total / t.seconds();
23✔
237

238
      if(!t.doing().empty()) {
23✔
239
         oss << t.doing() << " ";
23✔
240
      }
241

242
      if(t.buf_size() > 0) {
23✔
243
         oss << "buffer size " << t.buf_size() << " bytes: ";
23✔
244
      }
245

246
      if(events == 0) {
23✔
247
         oss << "N/A ";
×
248
      } else {
249
         oss << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec ";
23✔
250
      }
251

252
      if(t.cycles_consumed() != 0 && events > 0) {
46✔
253
         const double cycles_per_byte = static_cast<double>(t.cycles_consumed()) / events;
23✔
254
         oss << std::fixed << std::setprecision(2) << cycles_per_byte << " cycles/byte ";
23✔
255
      }
256

257
      oss << "(" << MiB_total << " MiB in " << t.milliseconds() << " ms)";
23✔
258
   }
259

260
   return oss.str();
830✔
261
}
415✔
262

263
std::vector<std::string> interpret_ecc_groups(const std::string& arg) {
29✔
264
   if(arg.empty()) {
29✔
265
      return {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
29✔
266
   }
267
   if(arg == "nist") {
×
268
      return {"secp224r1", "secp256r1", "secp384r1", "secp521r1"};
×
269
   }
270

271
#if defined(BOTAN_HAS_ECC_GROUP)
272
   if(arg == "all") {
×
273
      const auto& all = Botan::EC_Group::known_named_groups();
×
274
      return std::vector<std::string>(all.begin(), all.end());
×
275
   }
276

277
   if(arg == "generic") {
×
278
      std::vector<std::string> groups;
×
279
      for(const auto& group_name : Botan::EC_Group::known_named_groups()) {
×
280
         const Botan::EC_Group group(group_name);
×
281
         if(group.engine() == Botan::EC_Group_Engine::Generic) {
×
282
            groups.push_back(group_name);
×
283
         }
284
      }
×
285
      return groups;
×
286
   }
×
287

288
   if(arg == "pcurves") {
×
289
      std::vector<std::string> groups;
×
290
      for(const auto& group_name : Botan::EC_Group::known_named_groups()) {
×
291
         const Botan::EC_Group group(group_name);
×
292
         if(group.engine() == Botan::EC_Group_Engine::Optimized) {
×
293
            groups.push_back(group_name);
×
294
         }
295
      }
×
296
      return groups;
×
297
   }
×
298
#endif
299

300
   return Command::split_on(arg, ',');
×
301
}
302

303
class Speed final : public Command {
×
304
   public:
305
      Speed() :
30✔
306
            Command(
307
               "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") {
60✔
308
      }
30✔
309

310
      static std::vector<std::string> default_benchmark_list() {
×
311
         /*
312
         This is not intended to be exhaustive: it just hits the high
313
         points of the most interesting or widely used algorithms.
314
         */
315
         // clang-format off
316
         return {
×
317
            /* Block ciphers */
318
            "AES-128",
319
            "AES-192",
320
            "AES-256",
321
            "ARIA-128",
322
            "ARIA-192",
323
            "ARIA-256",
324
            "Blowfish",
325
            "CAST-128",
326
            "Camellia-128",
327
            "Camellia-192",
328
            "Camellia-256",
329
            "DES",
330
            "TripleDES",
331
            "GOST-28147-89",
332
            "IDEA",
333
            "Noekeon",
334
            "SHACAL2",
335
            "SM4",
336
            "Serpent",
337
            "Threefish-512",
338
            "Twofish",
339

340
            /* Cipher modes */
341
            "AES-128/CBC",
342
            "AES-128/CTR-BE",
343
            "AES-128/EAX",
344
            "AES-128/OCB",
345
            "AES-128/GCM",
346
            "AES-128/XTS",
347
            "AES-128/SIV",
348
            "Ascon-AEAD128",
349

350
            "Serpent/CBC",
351
            "Serpent/CTR-BE",
352
            "Serpent/EAX",
353
            "Serpent/OCB",
354
            "Serpent/GCM",
355
            "Serpent/XTS",
356
            "Serpent/SIV",
357

358
            "ChaCha20Poly1305",
359

360
            /* Stream ciphers */
361
            "RC4",
362
            "Salsa20",
363
            "ChaCha20",
364

365
            /* Hashes */
366
            "SHA-1",
367
            "SHA-256",
368
            "SHA-512",
369
            "SHA-3(256)",
370
            "SHA-3(512)",
371
            "Ascon-Hash256",
372
            "RIPEMD-160",
373
            "Skein-512",
374
            "Blake2b",
375
            "Whirlpool",
376

377
            /* XOFs */
378
            "SHAKE-128",
379
            "SHAKE-256",
380
            "Ascon-XOF128",
381

382
            /* MACs */
383
            "CMAC(AES-128)",
384
            "HMAC(SHA-256)",
385

386
            /* pubkey */
387
            "RSA",
388
            "DH",
389
            "ECDH",
390
            "ECDSA",
391
            "Ed25519",
392
            "Ed448",
393
            "X25519",
394
            "X448",
395
            "ML-KEM",
396
            "ML-DSA",
397
            "SLH-DSA",
398
            "FrodoKEM",
399
            "HSS-LMS",
400
         };
×
401
         // clang-format on
402
      }
403

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

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

408
      void go() override {
29✔
409
         const uint64_t milliseconds = get_arg_sz("msec");
29✔
410
         const std::string ecc_groups_arg = get_arg("ecc-groups");
29✔
411
         const std::string format = get_arg("format");
32✔
412
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
32✔
413

414
         const size_t clock_speed = get_arg_sz("cpu-clock-speed");
29✔
415

416
         double clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
29✔
417

418
         m_time_unit = [](std::string_view tu) {
119✔
419
            if(tu == "ms") {
29✔
420
               return 1000;
29✔
421
            } else if(tu == "us") {
×
422
               return 1000 * 1000;
×
423
            } else if(tu == "ns") {
×
424
               return 1000 * 1000 * 1000;
×
425
            } else {
426
               throw CLI_Usage_Error("Unknown time unit (supported: ms, us, ns)");
×
427
            }
428
         }(get_arg("time-unit"));
29✔
429

430
         /*
431
         * This argument is intended to be the ratio between the cycle counter
432
         * and the actual machine cycles. It is extremely unlikely that there is
433
         * any machine where the cycle counter increments faster than the actual
434
         * clock.
435
         */
436
         if(clock_cycle_ratio < 0.0 || clock_cycle_ratio > 1.0) {
29✔
437
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
438
         }
439

440
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
29✔
441

442
#if defined(BOTAN_HAS_OS_UTILS)
443
         if(clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
29✔
444
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
445
                              "platforms without access to a cycle counter.\n"
446
                              "Expect incorrect results\n\n";
×
447
         }
448
#endif
449

450
         if(format == "table") {
29✔
451
            m_summary = std::make_unique<Summary>();
1✔
452
         } else if(format == "json") {
28✔
453
            m_json = std::make_unique<JSON_Output>();
4✔
454
         } else if(format != "default") {
27✔
455
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
456
         }
457

458
         const auto ecc_groups = interpret_ecc_groups(ecc_groups_arg);
29✔
459

460
         std::vector<std::string> algos = get_arg_list("algos");
29✔
461

462
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
61✔
463

464
#if defined(BOTAN_HAS_CPUID)
465
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
27✔
466
            if(auto bit = Botan::CPUID::bit_from_string(cpuid_to_clear)) {
1✔
467
               Botan::CPUID::clear_cpuid_bit(*bit);
×
468
            } else {
469
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
470
            }
471
         }
26✔
472
#endif
473

474
         if(verbose() || m_summary) {
26✔
475
#if defined(BOTAN_HAS_CPUID)
476
            output() << Botan::version_string() << "\n"
2✔
477
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
478
#else
479
            output() << Botan::version_string() << "\n\n";
480
#endif
481
         }
482

483
         const bool using_defaults = (algos.empty());
26✔
484
         if(using_defaults) {
26✔
485
            algos = default_benchmark_list();
×
486
         }
487

488
         const PerfConfig perf_config([&](const Timer& t) { this->record_result(t); },
443✔
489
                                      clock_speed,
490
                                      clock_cycle_ratio,
491
                                      milliseconds,
492
                                      ecc_groups,
493
                                      buf_sizes,
494
                                      this->error_output(),
495
                                      this->rng());
26✔
496

497
         for(const auto& algo : algos) {
68✔
498
            if(auto perf = PerfTest::get(algo)) {
42✔
499
               perf->go(perf_config);
42✔
500
            } else if(verbose() || !using_defaults) {
×
501
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
502
            }
42✔
503
         }
504

505
         if(m_json) {
26✔
506
            output() << m_json->print();
2✔
507
         }
508
         if(m_summary) {
26✔
509
            output() << m_summary->print() << "\n";
3✔
510
         }
511

512
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
26✔
513
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
514
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
515
            const double MHz = Hz / 1000000;
×
516
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
517
         }
518
      }
64✔
519

520
   private:
521
      size_t m_time_unit = 0;
522
      uint64_t m_cycles_consumed = 0;
523
      uint64_t m_ns_taken = 0;
524
      std::unique_ptr<Summary> m_summary;
525
      std::unique_ptr<JSON_Output> m_json;
526

527
      void record_result(const Timer& t) {
417✔
528
         m_ns_taken += t.value();
417✔
529
         m_cycles_consumed += t.cycles_consumed();
417✔
530
         if(m_json) {
417✔
531
            m_json->add(t);
2✔
532
         } else {
533
            output() << format_timer(t, m_time_unit) << "\n" << std::flush;
830✔
534

535
            if(m_summary) {
415✔
536
               m_summary->add(t);
2✔
537
            }
538
         }
539
      }
417✔
540
};
541

542
BOTAN_REGISTER_COMMAND("speed", Speed);
30✔
543

544
}  // namespace
545

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