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

randombit / botan / 13024235637

29 Jan 2025 03:41AM UTC coverage: 91.238% (+0.005%) from 91.233%
13024235637

push

github

randombit
Update release notes

94120 of 103159 relevant lines covered (91.24%)

11490419.53 hits per line

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

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

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

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

32
namespace Botan_CLI {
33

34
namespace {
35

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

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

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

45
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
46
            const Timer& t = m_results[i];
2✔
47

48
            out << "{"
2✔
49
                << "\"algo\": \"" << t.get_name() << "\", "
2✔
50
                << "\"op\": \"" << t.doing() << "\", "
2✔
51
                << "\"events\": " << t.events() << ", ";
2✔
52

53
            if(t.cycles_consumed() > 0) {
4✔
54
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
55
            }
56

57
            if(t.buf_size() > 0) {
2✔
58
               out << "\"bps\": " << static_cast<uint64_t>(t.events() / (t.value() / 1000000000.0)) << ", ";
2✔
59
               out << "\"buf_size\": " << t.buf_size() << ", ";
2✔
60
            }
61

62
            out << "\"nanos\": " << t.value() << "}";
2✔
63

64
            if(i != m_results.size() - 1) {
2✔
65
               out << ",";
1✔
66
            }
67

68
            out << "\n";
2✔
69
         }
70
         out << "]\n";
1✔
71

72
         return out.str();
2✔
73
      }
1✔
74

75
   private:
76
      std::vector<Timer> m_results;
77
};
78

79
class Summary final {
1✔
80
   public:
81
      Summary() = default;
1✔
82

83
      void add(const Timer& t) {
2✔
84
         if(t.buf_size() == 0) {
2✔
85
            m_ops_entries.push_back(t);
×
86
         } else {
87
            m_bps_entries[std::make_pair(t.doing(), t.get_name())].push_back(t);
4✔
88
         }
89
      }
2✔
90

91
      std::string print() {
1✔
92
         const size_t name_padding = 35;
1✔
93
         const size_t op_name_padding = 16;
1✔
94
         const size_t op_padding = 16;
1✔
95

96
         std::ostringstream result_ss;
1✔
97
         result_ss << std::fixed;
1✔
98

99
         if(!m_bps_entries.empty()) {
1✔
100
            result_ss << "\n";
1✔
101

102
            // add table header
103
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
104
                      << "operation";
1✔
105

106
            for(const Timer& t : m_bps_entries.begin()->second) {
2✔
107
               result_ss << std::setw(op_padding) << std::right << (std::to_string(t.buf_size()) + " bytes");
2✔
108
            }
109
            result_ss << "\n";
1✔
110

111
            // add table entries
112
            for(const auto& entry : m_bps_entries) {
3✔
113
               if(entry.second.empty()) {
2✔
114
                  continue;
×
115
               }
116

117
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
118
                         << std::left << (entry.first.first);
2✔
119

120
               for(const Timer& t : entry.second) {
4✔
121
                  if(t.events() == 0) {
2✔
122
                     result_ss << std::setw(op_padding) << std::right << "N/A";
×
123
                  } else {
124
                     result_ss << std::setw(op_padding) << std::right << std::setprecision(2)
2✔
125
                               << (t.bytes_per_second() / 1000.0);
2✔
126
                  }
127
               }
128

129
               result_ss << "\n";
2✔
130
            }
131

132
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
133
         }
134

135
         if(!m_ops_entries.empty()) {
1✔
136
            result_ss << std::setprecision(6) << "\n";
×
137

138
            // sort entries
139
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
140

141
            // add table header
142
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
×
143
                      << "operation" << std::setw(op_padding) << std::right << "sec/op" << std::setw(op_padding)
×
144
                      << std::right << "op/sec"
×
145
                      << "\n";
×
146

147
            // add table entries
148
            for(const Timer& entry : m_ops_entries) {
×
149
               result_ss << std::setw(name_padding) << std::left << entry.get_name() << std::setw(op_name_padding)
×
150
                         << std::left << entry.doing() << std::setw(op_padding) << std::right
×
151
                         << entry.seconds_per_event() << std::setw(op_padding) << std::right
×
152
                         << entry.events_per_second() << "\n";
×
153
            }
154
         }
155

156
         return result_ss.str();
2✔
157
      }
1✔
158

159
   private:
160
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
161
      std::vector<Timer> m_ops_entries;
162
};
163

164
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
28✔
165
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
28✔
166

167
   std::set<size_t> buf;
28✔
168
   for(const std::string& size_str : Command::split_on(cmdline_arg, ',')) {
54✔
169
      size_t x = 0;
29✔
170
      try {
29✔
171
         size_t converted = 0;
29✔
172
         x = static_cast<size_t>(std::stoul(size_str, &converted, 0));
29✔
173

174
         if(converted != size_str.size()) {
28✔
175
            throw CLI_Usage_Error("Invalid integer");
×
176
         }
177
      } catch(std::exception&) {
1✔
178
         throw CLI_Usage_Error("Invalid integer value '" + size_str + "' for option buf-size");
2✔
179
      }
1✔
180

181
      if(x == 0) {
28✔
182
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
183
      }
184

185
      if(x > MAX_BUF_SIZE) {
27✔
186
         throw CLI_Usage_Error("Specified buffer size is too large");
2✔
187
      }
188

189
      buf.insert(x);
26✔
190
   }
28✔
191

192
   return std::vector<size_t>(buf.begin(), buf.end());
28✔
193
}
25✔
194

195
std::string format_timer(const Timer& t, size_t time_unit) {
477✔
196
   constexpr size_t MiB = 1024 * 1024;
477✔
197

198
   std::ostringstream oss;
477✔
199

200
   oss << t.get_name() << " ";
477✔
201

202
   const uint64_t events = t.events();
477✔
203

204
   if(t.buf_size() == 0) {
477✔
205
      // Report operations/time unit
206

207
      if(events == 0) {
454✔
208
         oss << "no events ";
×
209
      } else {
210
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
908✔
211

212
         if(time_unit == 1000) {
454✔
213
            oss << std::setprecision(2) << std::fixed << (t.milliseconds() / events) << " ms/op ";
454✔
214
         } else if(time_unit == 1000 * 1000) {
×
215
            oss << std::setprecision(2) << std::fixed << (t.microseconds() / events) << " us/op ";
×
216
         } else if(time_unit == 1000 * 1000 * 1000) {
×
217
            oss << std::setprecision(0) << std::fixed << (t.nanoseconds() / events) << " ns/op ";
×
218
         }
219

220
         if(t.cycles_consumed() != 0 && events > 0) {
908✔
221
            const double cycles_per_op = static_cast<double>(t.cycles_consumed()) / events;
454✔
222
            const int precision = (cycles_per_op < 10000) ? 2 : 0;
454✔
223
            oss << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op ";
454✔
224
         }
225

226
         oss << "(" << events << " " << (events == 1 ? "op" : "ops") << " in " << t.milliseconds() << " ms)";
689✔
227
      }
228
   } else {
229
      // Bulk op - report bytes/time unit
230

231
      const double MiB_total = static_cast<double>(events) / MiB;
23✔
232
      const double MiB_per_sec = MiB_total / t.seconds();
23✔
233

234
      if(!t.doing().empty()) {
23✔
235
         oss << t.doing() << " ";
23✔
236
      }
237

238
      if(t.buf_size() > 0) {
23✔
239
         oss << "buffer size " << t.buf_size() << " bytes: ";
23✔
240
      }
241

242
      if(events == 0) {
23✔
243
         oss << "N/A ";
×
244
      } else {
245
         oss << std::fixed << std::setprecision(3) << MiB_per_sec << " MiB/sec ";
23✔
246
      }
247

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

253
      oss << "(" << MiB_total << " MiB in " << t.milliseconds() << " ms)";
23✔
254
   }
255

256
   return oss.str();
954✔
257
}
477✔
258

259
}  // namespace
260

261
class Speed final : public Command {
×
262
   public:
263
      Speed() :
29✔
264
            Command(
265
               "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✔
266
      }
29✔
267

268
      static std::vector<std::string> default_benchmark_list() {
×
269
         /*
270
         This is not intended to be exhaustive: it just hits the high
271
         points of the most interesting or widely used algorithms.
272
         */
273
         // clang-format off
274
         return {
×
275
            /* Block ciphers */
276
            "AES-128",
277
            "AES-192",
278
            "AES-256",
279
            "ARIA-128",
280
            "ARIA-192",
281
            "ARIA-256",
282
            "Blowfish",
283
            "CAST-128",
284
            "Camellia-128",
285
            "Camellia-192",
286
            "Camellia-256",
287
            "DES",
288
            "TripleDES",
289
            "GOST-28147-89",
290
            "IDEA",
291
            "Noekeon",
292
            "SHACAL2",
293
            "SM4",
294
            "Serpent",
295
            "Threefish-512",
296
            "Twofish",
297

298
            /* Cipher modes */
299
            "AES-128/CBC",
300
            "AES-128/CTR-BE",
301
            "AES-128/EAX",
302
            "AES-128/OCB",
303
            "AES-128/GCM",
304
            "AES-128/XTS",
305
            "AES-128/SIV",
306

307
            "Serpent/CBC",
308
            "Serpent/CTR-BE",
309
            "Serpent/EAX",
310
            "Serpent/OCB",
311
            "Serpent/GCM",
312
            "Serpent/XTS",
313
            "Serpent/SIV",
314

315
            "ChaCha20Poly1305",
316

317
            /* Stream ciphers */
318
            "RC4",
319
            "Salsa20",
320
            "ChaCha20",
321

322
            /* Hashes */
323
            "SHA-1",
324
            "SHA-256",
325
            "SHA-512",
326
            "SHA-3(256)",
327
            "SHA-3(512)",
328
            "RIPEMD-160",
329
            "Skein-512",
330
            "Blake2b",
331
            "Whirlpool",
332

333
            /* XOFs */
334
            "SHAKE-128",
335
            "SHAKE-256",
336

337
            /* MACs */
338
            "CMAC(AES-128)",
339
            "HMAC(SHA-256)",
340

341
            /* pubkey */
342
            "RSA",
343
            "DH",
344
            "ECDH",
345
            "ECDSA",
346
            "Ed25519",
347
            "Ed448",
348
            "X25519",
349
            "X448",
350
            "ML-KEM",
351
            "ML-DSA",
352
            "SLH-DSA",
353
            "FrodoKEM",
354
            "HSS-LMS",
355
         };
×
356
         // clang-format on
357
      }
358

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

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

363
      void go() override {
28✔
364
         std::chrono::milliseconds msec(get_arg_sz("msec"));
28✔
365
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
56✔
366
         const std::string format = get_arg("format");
28✔
367
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
31✔
368

369
         const size_t clock_speed = get_arg_sz("cpu-clock-speed");
28✔
370

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

373
         m_time_unit = [](std::string_view tu) {
115✔
374
            if(tu == "ms") {
28✔
375
               return 1000;
28✔
376
            } else if(tu == "us") {
×
377
               return 1000 * 1000;
×
378
            } else if(tu == "ns") {
×
379
               return 1000 * 1000 * 1000;
×
380
            } else {
381
               throw CLI_Usage_Error("Unknown time unit (supported: ms, us, ns)");
×
382
            }
383
         }(get_arg("time-unit"));
28✔
384

385
         /*
386
         * This argument is intended to be the ratio between the cycle counter
387
         * and the actual machine cycles. It is extremely unlikely that there is
388
         * any machine where the cycle counter increments faster than the actual
389
         * clock.
390
         */
391
         if(clock_cycle_ratio < 0.0 || clock_cycle_ratio > 1.0) {
28✔
392
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
393
         }
394

395
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
28✔
396

397
#if defined(BOTAN_HAS_OS_UTILS)
398
         if(clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
28✔
399
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
400
                              "platforms without access to a cycle counter.\n"
401
                              "Expect incorrect results\n\n";
×
402
         }
403
#endif
404

405
         if(format == "table") {
28✔
406
            m_summary = std::make_unique<Summary>();
1✔
407
         } else if(format == "json") {
27✔
408
            m_json = std::make_unique<JSON_Output>();
1✔
409
         } else if(format != "default") {
26✔
410
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
411
         }
412

413
#if defined(BOTAN_HAS_ECC_GROUP)
414
         if(ecc_groups.empty()) {
28✔
415
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
224✔
416
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
417
            auto all = Botan::EC_Group::known_named_groups();
×
418
            ecc_groups.assign(all.begin(), all.end());
×
419
         }
×
420
#endif
421

422
         std::vector<std::string> algos = get_arg_list("algos");
31✔
423

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

426
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
26✔
427
            auto bits = Botan::CPUID::bit_from_string(cpuid_to_clear);
1✔
428
            if(bits.empty()) {
1✔
429
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
430
            }
431

432
            for(auto bit : bits) {
1✔
433
               Botan::CPUID::clear_cpuid_bit(bit);
×
434
            }
435
         }
26✔
436

437
         if(verbose() || m_summary) {
25✔
438
            output() << Botan::version_string() << "\n"
2✔
439
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
440
         }
441

442
         const bool using_defaults = (algos.empty());
25✔
443
         if(using_defaults) {
25✔
444
            algos = default_benchmark_list();
×
445
         }
446

447
         PerfConfig perf_config([&](const Timer& t) { this->record_result(t); },
504✔
448
                                clock_speed,
449
                                clock_cycle_ratio,
450
                                msec,
451
                                ecc_groups,
452
                                buf_sizes,
453
                                this->error_output(),
454
                                this->rng());
25✔
455

456
         for(const auto& algo : algos) {
70✔
457
            if(auto perf = PerfTest::get(algo)) {
45✔
458
               perf->go(perf_config);
45✔
459
            } else if(verbose() || !using_defaults) {
×
460
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
461
            }
45✔
462
         }
463

464
         if(m_json) {
25✔
465
            output() << m_json->print();
2✔
466
         }
467
         if(m_summary) {
25✔
468
            output() << m_summary->print() << "\n";
3✔
469
         }
470

471
         if(verbose() && clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
25✔
472
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
473
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
474
            const double MHz = Hz / 1000000;
×
475
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
476
         }
477
      }
115✔
478

479
   private:
480
      size_t m_time_unit = 0;
481
      uint64_t m_cycles_consumed = 0;
482
      uint64_t m_ns_taken = 0;
483
      std::unique_ptr<Summary> m_summary;
484
      std::unique_ptr<JSON_Output> m_json;
485

486
      void record_result(const Timer& t) {
479✔
487
         m_ns_taken += t.value();
479✔
488
         m_cycles_consumed += t.cycles_consumed();
479✔
489
         if(m_json) {
479✔
490
            m_json->add(t);
2✔
491
         } else {
492
            output() << format_timer(t, m_time_unit) << std::endl;
954✔
493

494
            if(m_summary) {
477✔
495
               m_summary->add(t);
2✔
496
            }
497
         }
498
      }
479✔
499
};
500

501
BOTAN_REGISTER_COMMAND("speed", Speed);
29✔
502

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