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

randombit / botan / 14120723934

28 Mar 2025 02:31AM UTC coverage: 91.539% (+0.004%) from 91.535%
14120723934

Pull #4798

github

web-flow
Merge db2c0eef1 into 70cd16046
Pull Request #4798: Move most architecture-specific logic out of CPUID and into a submodule

95384 of 104200 relevant lines covered (91.54%)

11667903.22 hits per line

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

77.78
/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/fmt.h>
21
#include <botan/internal/stl_util.h>
22
#include <botan/internal/target_info.h>
23

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

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

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

36
namespace Botan_CLI {
37

38
namespace {
39

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

44
      std::string print() const {
1✔
45
         std::ostringstream out;
1✔
46

47
         out << "[\n";
1✔
48

49
         out << "{"
1✔
50
             << "\"arch\": \"" << BOTAN_TARGET_ARCH << "\", "
51
             << "\"version\": \"" << Botan::short_version_cstr() << "\", ";
1✔
52

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

57
         out << "\"compiler\": \"" << BOTAN_COMPILER_INVOCATION_STRING << "\""
1✔
58
             << "},\n";
1✔
59

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

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

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

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

77
            out << "\"nanos\": " << t.value() << "}";
2✔
78

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

83
            out << "\n";
2✔
84
         }
85
         out << "]\n";
1✔
86

87
         return out.str();
2✔
88
      }
1✔
89

90
   private:
91
      std::vector<Timer> m_results;
92
};
93

94
class Summary final {
1✔
95
   public:
96
      Summary() = default;
1✔
97

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

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

111
         std::ostringstream result_ss;
1✔
112
         result_ss << std::fixed;
1✔
113

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

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

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

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

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

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

144
               result_ss << "\n";
2✔
145
            }
146

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

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

153
            // sort entries
154
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
155

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

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

171
         return result_ss.str();
2✔
172
      }
1✔
173

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

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

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

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

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

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

204
      buf.insert(x);
26✔
205
   }
28✔
206

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

210
std::string format_timer(const Timer& t, size_t time_unit) {
489✔
211
   constexpr size_t MiB = 1024 * 1024;
489✔
212

213
   std::ostringstream oss;
489✔
214

215
   oss << t.get_name() << " ";
489✔
216

217
   const uint64_t events = t.events();
489✔
218

219
   if(t.buf_size() == 0) {
489✔
220
      // Report operations/time unit
221

222
      if(events == 0) {
466✔
223
         oss << "no events ";
×
224
      } else {
225
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
932✔
226

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

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

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

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

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

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

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

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

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

271
   return oss.str();
978✔
272
}
489✔
273

274
}  // namespace
275

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

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

313
            /* Cipher modes */
314
            "AES-128/CBC",
315
            "AES-128/CTR-BE",
316
            "AES-128/EAX",
317
            "AES-128/OCB",
318
            "AES-128/GCM",
319
            "AES-128/XTS",
320
            "AES-128/SIV",
321

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

330
            "ChaCha20Poly1305",
331

332
            /* Stream ciphers */
333
            "RC4",
334
            "Salsa20",
335
            "ChaCha20",
336

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

348
            /* XOFs */
349
            "SHAKE-128",
350
            "SHAKE-256",
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
            auto all = Botan::EC_Group::known_named_groups();
×
433
            ecc_groups.assign(all.begin(), all.end());
×
434
         }
×
435
#endif
436

437
         std::vector<std::string> algos = get_arg_list("algos");
31✔
438

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

441
#if defined(BOTAN_HAS_CPUID)
442
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
26✔
443
            if(auto bit = Botan::CPUID::bit_from_string(cpuid_to_clear)) {
1✔
444
               Botan::CPUID::clear_cpuid_bit(*bit);
×
445
            } else {
446
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
447
            }
448
         }
25✔
449
#endif
450

451
         if(verbose() || m_summary) {
25✔
452
#if defined(BOTAN_HAS_CPUID)
453
            output() << Botan::version_string() << "\n"
2✔
454
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
455
#else
456
            output() << Botan::version_string() << "\n\n";
457
#endif
458
         }
459

460
         const bool using_defaults = (algos.empty());
25✔
461
         if(using_defaults) {
25✔
462
            algos = default_benchmark_list();
×
463
         }
464

465
         PerfConfig perf_config([&](const Timer& t) { this->record_result(t); },
516✔
466
                                clock_speed,
467
                                clock_cycle_ratio,
468
                                msec,
469
                                ecc_groups,
470
                                buf_sizes,
471
                                this->error_output(),
472
                                this->rng());
25✔
473

474
         for(const auto& algo : algos) {
70✔
475
            if(auto perf = PerfTest::get(algo)) {
45✔
476
               perf->go(perf_config);
45✔
477
            } else if(verbose() || !using_defaults) {
×
478
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
479
            }
45✔
480
         }
481

482
         if(m_json) {
25✔
483
            output() << m_json->print();
2✔
484
         }
485
         if(m_summary) {
25✔
486
            output() << m_summary->print() << "\n";
3✔
487
         }
488

489
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
25✔
490
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
491
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
492
            const double MHz = Hz / 1000000;
×
493
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
494
         }
495
      }
115✔
496

497
   private:
498
      size_t m_time_unit = 0;
499
      uint64_t m_cycles_consumed = 0;
500
      uint64_t m_ns_taken = 0;
501
      std::unique_ptr<Summary> m_summary;
502
      std::unique_ptr<JSON_Output> m_json;
503

504
      void record_result(const Timer& t) {
491✔
505
         m_ns_taken += t.value();
491✔
506
         m_cycles_consumed += t.cycles_consumed();
491✔
507
         if(m_json) {
491✔
508
            m_json->add(t);
2✔
509
         } else {
510
            output() << format_timer(t, m_time_unit) << std::endl;
978✔
511

512
            if(m_summary) {
489✔
513
               m_summary->add(t);
2✔
514
            }
515
         }
516
      }
491✔
517
};
518

519
BOTAN_REGISTER_COMMAND("speed", Speed);
29✔
520

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

© 2025 Coveralls, Inc