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

randombit / botan / 12465815987

23 Dec 2024 11:07AM UTC coverage: 92.433% (+1.2%) from 91.257%
12465815987

push

github

web-flow
Merge pull request #4490 from randombit/jack/timer-units

Add option to quote performance in microseconds or nanoseconds

50265 of 54380 relevant lines covered (92.43%)

21609756.55 hits per line

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

78.3
/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/os_utils.h>
23
#include <botan/internal/stl_util.h>
24

25
#if defined(BOTAN_HAS_ECC_GROUP)
26
   #include <botan/ec_group.h>
27
#endif
28

29
namespace Botan_CLI {
30

31
namespace {
32

33
class JSON_Output final {
2✔
34
   public:
35
      void add(const Timer& timer) { m_results.push_back(timer); }
2✔
36

37
      std::string print() const {
1✔
38
         std::ostringstream out;
1✔
39

40
         out << "[\n";
1✔
41

42
         for(size_t i = 0; i != m_results.size(); ++i) {
3✔
43
            const Timer& t = m_results[i];
2✔
44

45
            out << "{"
2✔
46
                << "\"algo\": \"" << t.get_name() << "\", "
2✔
47
                << "\"op\": \"" << t.doing() << "\", "
2✔
48
                << "\"events\": " << t.events() << ", ";
2✔
49

50
            if(t.cycles_consumed() > 0) {
4✔
51
               out << "\"cycles\": " << t.cycles_consumed() << ", ";
4✔
52
            }
53

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

59
            out << "\"nanos\": " << t.value() << "}";
2✔
60

61
            if(i != m_results.size() - 1) {
2✔
62
               out << ",";
1✔
63
            }
64

65
            out << "\n";
2✔
66
         }
67
         out << "]\n";
1✔
68

69
         return out.str();
2✔
70
      }
1✔
71

72
   private:
73
      std::vector<Timer> m_results;
74
};
75

76
class Summary final {
1✔
77
   public:
78
      Summary() = default;
1✔
79

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

88
      std::string print() {
1✔
89
         const size_t name_padding = 35;
1✔
90
         const size_t op_name_padding = 16;
1✔
91
         const size_t op_padding = 16;
1✔
92

93
         std::ostringstream result_ss;
1✔
94
         result_ss << std::fixed;
1✔
95

96
         if(!m_bps_entries.empty()) {
1✔
97
            result_ss << "\n";
1✔
98

99
            // add table header
100
            result_ss << std::setw(name_padding) << std::left << "algo" << std::setw(op_name_padding) << std::left
1✔
101
                      << "operation";
1✔
102

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

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

114
               result_ss << std::setw(name_padding) << std::left << (entry.first.second) << std::setw(op_name_padding)
2✔
115
                         << std::left << (entry.first.first);
2✔
116

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

126
               result_ss << "\n";
2✔
127
            }
128

129
            result_ss << "\n[results are the number of 1000s bytes processed per second]\n";
1✔
130
         }
131

132
         if(!m_ops_entries.empty()) {
1✔
133
            result_ss << std::setprecision(6) << "\n";
×
134

135
            // sort entries
136
            std::sort(m_ops_entries.begin(), m_ops_entries.end());
×
137

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

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

153
         return result_ss.str();
2✔
154
      }
1✔
155

156
   private:
157
      std::map<std::pair<std::string, std::string>, std::vector<Timer>> m_bps_entries;
158
      std::vector<Timer> m_ops_entries;
159
};
160

161
std::vector<size_t> unique_buffer_sizes(const std::string& cmdline_arg) {
28✔
162
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
28✔
163

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

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

178
      if(x == 0) {
28✔
179
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
180
      }
181

182
      if(x > MAX_BUF_SIZE) {
27✔
183
         throw CLI_Usage_Error("Specified buffer size is too large");
2✔
184
      }
185

186
      buf.insert(x);
26✔
187
   }
28✔
188

189
   return std::vector<size_t>(buf.begin(), buf.end());
28✔
190
}
25✔
191

192
std::string format_timer(const Timer& t, size_t time_unit) {
461✔
193
   constexpr size_t MiB = 1024 * 1024;
461✔
194

195
   std::ostringstream oss;
461✔
196

197
   oss << t.get_name() << " ";
461✔
198

199
   const uint64_t events = t.events();
461✔
200

201
   if(t.buf_size() == 0) {
461✔
202
      // Report operations/time unit
203

204
      if(events == 0) {
438✔
205
         oss << "no events ";
×
206
      } else {
207
         oss << static_cast<uint64_t>(t.events_per_second()) << ' ' << t.doing() << "/sec; ";
876✔
208

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

217
         if(t.cycles_consumed() != 0 && events > 0) {
876✔
218
            const double cycles_per_op = static_cast<double>(t.cycles_consumed()) / events;
438✔
219
            const int precision = (cycles_per_op < 10000) ? 2 : 0;
438✔
220
            oss << std::fixed << std::setprecision(precision) << cycles_per_op << " cycles/op ";
438✔
221
         }
222

223
         oss << "(" << events << " " << (events == 1 ? "op" : "ops") << " in " << t.milliseconds() << " ms)";
671✔
224
      }
225
   } else {
226
      // Bulk op - report bytes/time unit
227

228
      const double MiB_total = static_cast<double>(events) / MiB;
23✔
229
      const double MiB_per_sec = MiB_total / t.seconds();
23✔
230

231
      if(!t.doing().empty()) {
23✔
232
         oss << t.doing() << " ";
23✔
233
      }
234

235
      if(t.buf_size() > 0) {
23✔
236
         oss << "buffer size " << t.buf_size() << " bytes: ";
23✔
237
      }
238

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

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

250
      oss << "(" << MiB_total << " MiB in " << t.milliseconds() << " ms)";
23✔
251
   }
252

253
   oss << "\n";
461✔
254

255
   return oss.str();
922✔
256
}
461✔
257

258
}  // namespace
259

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

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

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

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

314
            "ChaCha20Poly1305",
315

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

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

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

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

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

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

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

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

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

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

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

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

394
         clock_cycle_ratio = 1.0 / clock_cycle_ratio;
28✔
395

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

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

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

419
         std::vector<std::string> algos = get_arg_list("algos");
31✔
420

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

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

429
            for(auto bit : bits) {
1✔
430
               Botan::CPUID::clear_cpuid_bit(bit);
×
431
            }
432
         }
26✔
433

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

439
         const bool using_defaults = (algos.empty());
25✔
440
         if(using_defaults) {
25✔
441
            algos = default_benchmark_list();
×
442
         }
443

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

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

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

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

476
   private:
477
      size_t m_time_unit = 0;
478
      uint64_t m_cycles_consumed = 0;
479
      uint64_t m_ns_taken = 0;
480
      std::unique_ptr<Summary> m_summary;
481
      std::unique_ptr<JSON_Output> m_json;
482

483
      void record_result(const Timer& t) {
463✔
484
         m_ns_taken += t.value();
463✔
485
         m_cycles_consumed += t.cycles_consumed();
463✔
486
         if(m_json) {
463✔
487
            m_json->add(t);
2✔
488
         } else {
489
            output() << format_timer(t, m_time_unit);
922✔
490

491
            if(m_summary) {
461✔
492
               m_summary->add(t);
2✔
493
            }
494
         }
495
      }
463✔
496
};
497

498
BOTAN_REGISTER_COMMAND("speed", Speed);
29✔
499

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