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

randombit / botan / 11341066650

15 Oct 2024 07:12AM UTC coverage: 91.131% (+0.006%) from 91.125%
11341066650

Pull #3893

github

web-flow
Merge 33000ab14 into 4996790e9
Pull Request #3893: PQC: ML-KEM

90490 of 99297 relevant lines covered (91.13%)

9098152.18 hits per line

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

80.11
/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
#include <botan/internal/timer.h>
25

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

30
namespace Botan_CLI {
31

32
using Botan::Timer;
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) {
30✔
165
   const size_t MAX_BUF_SIZE = 64 * 1024 * 1024;
30✔
166

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

174
         if(converted != size_str.size()) {
30✔
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) {
30✔
182
         throw CLI_Usage_Error("Cannot have a zero-sized buffer");
2✔
183
      }
184

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

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

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

195
}  // namespace
196

197
class Speed final : public Command {
×
198
   public:
199
      Speed() :
31✔
200
            Command(
201
               "speed --msec=500 --format=default --ecc-groups= --buf-size=1024 --clear-cpuid= --cpu-clock-speed=0 --cpu-clock-ratio=1.0 *algos") {
62✔
202
      }
31✔
203

204
      static std::vector<std::string> default_benchmark_list() {
×
205
         /*
206
         This is not intended to be exhaustive: it just hits the high
207
         points of the most interesting or widely used algorithms.
208
         */
209
         // clang-format off
210
         return {
×
211
            /* Block ciphers */
212
            "AES-128",
213
            "AES-192",
214
            "AES-256",
215
            "ARIA-128",
216
            "ARIA-192",
217
            "ARIA-256",
218
            "Blowfish",
219
            "CAST-128",
220
            "Camellia-128",
221
            "Camellia-192",
222
            "Camellia-256",
223
            "DES",
224
            "TripleDES",
225
            "GOST-28147-89",
226
            "IDEA",
227
            "Noekeon",
228
            "SHACAL2",
229
            "SM4",
230
            "Serpent",
231
            "Threefish-512",
232
            "Twofish",
233

234
            /* Cipher modes */
235
            "AES-128/CBC",
236
            "AES-128/CTR-BE",
237
            "AES-128/EAX",
238
            "AES-128/OCB",
239
            "AES-128/GCM",
240
            "AES-128/XTS",
241
            "AES-128/SIV",
242

243
            "Serpent/CBC",
244
            "Serpent/CTR-BE",
245
            "Serpent/EAX",
246
            "Serpent/OCB",
247
            "Serpent/GCM",
248
            "Serpent/XTS",
249
            "Serpent/SIV",
250

251
            "ChaCha20Poly1305",
252

253
            /* Stream ciphers */
254
            "RC4",
255
            "Salsa20",
256
            "ChaCha20",
257

258
            /* Hashes */
259
            "SHA-1",
260
            "SHA-256",
261
            "SHA-512",
262
            "SHA-3(256)",
263
            "SHA-3(512)",
264
            "RIPEMD-160",
265
            "Skein-512",
266
            "Blake2b",
267
            "Whirlpool",
268

269
            /* XOFs */
270
            "SHAKE-128",
271
            "SHAKE-256",
272

273
            /* MACs */
274
            "CMAC(AES-128)",
275
            "HMAC(SHA-256)",
276

277
            /* pubkey */
278
            "RSA",
279
            "DH",
280
            "ECDH",
281
            "ECDSA",
282
            "Ed25519",
283
            "Ed448",
284
            "X25519",
285
            "X448",
286
            "Kyber",
287
            "ML-KEM",
288
            "SPHINCS+",
289
            "FrodoKEM",
290
            "HSS-LMS",
291
         };
×
292
         // clang-format on
293
      }
294

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

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

299
      void go() override {
30✔
300
         std::chrono::milliseconds msec(get_arg_sz("msec"));
30✔
301
         std::vector<std::string> ecc_groups = Command::split_on(get_arg("ecc-groups"), ',');
60✔
302
         const std::string format = get_arg("format");
30✔
303
         const std::string clock_ratio = get_arg("cpu-clock-ratio");
33✔
304
         m_clock_speed = get_arg_sz("cpu-clock-speed");
30✔
305

306
         m_clock_cycle_ratio = std::strtod(clock_ratio.c_str(), nullptr);
30✔
307

308
         /*
309
         * This argument is intended to be the ratio between the cycle counter
310
         * and the actual machine cycles. It is extremely unlikely that there is
311
         * any machine where the cycle counter increments faster than the actual
312
         * clock.
313
         */
314
         if(m_clock_cycle_ratio < 0.0 || m_clock_cycle_ratio > 1.0) {
30✔
315
            throw CLI_Usage_Error("Unlikely CPU clock ratio of " + clock_ratio);
×
316
         }
317

318
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
30✔
319

320
         if(m_clock_speed != 0 && Botan::OS::get_cpu_cycle_counter() != 0) {
30✔
321
            error_output() << "The --cpu-clock-speed option is only intended to be used on "
×
322
                              "platforms without access to a cycle counter.\n"
323
                              "Expected incorrect results\n\n";
×
324
         }
325

326
         if(format == "table") {
30✔
327
            m_summary = std::make_unique<Summary>();
1✔
328
         } else if(format == "json") {
29✔
329
            m_json = std::make_unique<JSON_Output>();
1✔
330
         } else if(format != "default") {
28✔
331
            throw CLI_Usage_Error("Unknown --format type '" + format + "'");
×
332
         }
333

334
#if defined(BOTAN_HAS_ECC_GROUP)
335
         if(ecc_groups.empty()) {
30✔
336
            ecc_groups = {"secp256r1", "secp384r1", "secp521r1", "brainpool256r1", "brainpool384r1", "brainpool512r1"};
240✔
337
         } else if(ecc_groups.size() == 1 && ecc_groups[0] == "all") {
×
338
            auto all = Botan::EC_Group::known_named_groups();
×
339
            ecc_groups.assign(all.begin(), all.end());
×
340
         }
×
341
#endif
342

343
         std::vector<std::string> algos = get_arg_list("algos");
33✔
344

345
         const std::vector<size_t> buf_sizes = unique_buffer_sizes(get_arg("buf-size"));
63✔
346

347
         for(const std::string& cpuid_to_clear : Command::split_on(get_arg("clear-cpuid"), ',')) {
28✔
348
            auto bits = Botan::CPUID::bit_from_string(cpuid_to_clear);
1✔
349
            if(bits.empty()) {
1✔
350
               error_output() << "Warning don't know CPUID flag '" << cpuid_to_clear << "'\n";
1✔
351
            }
352

353
            for(auto bit : bits) {
1✔
354
               Botan::CPUID::clear_cpuid_bit(bit);
×
355
            }
356
         }
28✔
357

358
         if(verbose() || m_summary) {
27✔
359
            output() << Botan::version_string() << "\n"
2✔
360
                     << "CPUID: " << Botan::CPUID::to_string() << "\n\n";
3✔
361
         }
362

363
         const bool using_defaults = (algos.empty());
27✔
364
         if(using_defaults) {
27✔
365
            algos = default_benchmark_list();
×
366
         }
367

368
         class PerfConfig_Cli final : public PerfConfig {
27✔
369
            public:
370
               PerfConfig_Cli(std::chrono::milliseconds runtime,
27✔
371
                              const std::vector<std::string>& ecc_groups,
372
                              const std::vector<size_t>& buffer_sizes,
373
                              Speed* speed) :
27✔
374
                     m_runtime(runtime), m_ecc_groups(ecc_groups), m_buffer_sizes(buffer_sizes), m_speed(speed) {}
27✔
375

376
               const std::vector<size_t>& buffer_sizes() const override { return m_buffer_sizes; }
15✔
377

378
               const std::vector<std::string>& ecc_groups() const override { return m_ecc_groups; }
6✔
379

380
               std::chrono::milliseconds runtime() const override { return m_runtime; }
130✔
381

382
               std::ostream& error_output() const override { return m_speed->error_output(); }
×
383

384
               Botan::RandomNumberGenerator& rng() const override { return m_speed->rng(); }
10,289✔
385

386
               void record_result(const Botan::Timer& timer) const override { m_speed->record_result(timer); }
462✔
387

388
               std::unique_ptr<Botan::Timer> make_timer(const std::string& alg,
485✔
389
                                                        uint64_t event_mult,
390
                                                        const std::string& what,
391
                                                        const std::string& provider,
392
                                                        size_t buf_size) const override {
393
                  return m_speed->make_timer(alg, event_mult, what, provider, buf_size);
970✔
394
               }
395

396
            private:
397
               std::chrono::milliseconds m_runtime;
398
               std::vector<std::string> m_ecc_groups;
399
               std::vector<size_t> m_buffer_sizes;
400
               Speed* m_speed;
401
         };
402

403
         PerfConfig_Cli perf_config(msec, ecc_groups, buf_sizes, this);
27✔
404

405
         for(const auto& algo : algos) {
74✔
406
            using namespace std::placeholders;
47✔
407

408
            if(auto perf = PerfTest::get(algo)) {
47✔
409
               perf->go(perf_config);
47✔
410
            } else if(verbose() || !using_defaults) {
×
411
               error_output() << "Unknown algorithm '" << algo << "'\n";
×
412
            }
47✔
413
         }
414

415
         if(m_json) {
27✔
416
            output() << m_json->print();
2✔
417
         }
418
         if(m_summary) {
27✔
419
            output() << m_summary->print() << "\n";
3✔
420
         }
421

422
         if(verbose() && m_clock_speed == 0 && m_cycles_consumed > 0 && m_ns_taken > 0) {
27✔
423
            const double seconds = static_cast<double>(m_ns_taken) / 1000000000;
×
424
            const double Hz = static_cast<double>(m_cycles_consumed) / seconds;
×
425
            const double MHz = Hz / 1000000;
×
426
            output() << "\nEstimated clock speed " << MHz << " MHz\n";
×
427
         }
428
      }
123✔
429

430
   private:
431
      size_t m_clock_speed = 0;
432
      double m_clock_cycle_ratio = 0.0;
433
      uint64_t m_cycles_consumed = 0;
434
      uint64_t m_ns_taken = 0;
435
      std::unique_ptr<Summary> m_summary;
436
      std::unique_ptr<JSON_Output> m_json;
437

438
      void record_result(const Timer& t) {
462✔
439
         m_ns_taken += t.value();
462✔
440
         m_cycles_consumed += t.cycles_consumed();
462✔
441
         if(m_json) {
462✔
442
            m_json->add(t);
2✔
443
         } else {
444
            output() << t.to_string() << std::flush;
920✔
445
            if(m_summary) {
460✔
446
               m_summary->add(t);
2✔
447
            }
448
         }
449
      }
462✔
450

451
      void record_result(const std::unique_ptr<Timer>& t) { record_result(*t); }
452

453
      std::unique_ptr<Timer> make_timer(const std::string& name,
485✔
454
                                        uint64_t event_mult = 1,
455
                                        const std::string& what = "",
456
                                        const std::string& provider = "",
457
                                        size_t buf_size = 0) {
458
         return std::make_unique<Timer>(name, provider, what, event_mult, buf_size, m_clock_cycle_ratio, m_clock_speed);
485✔
459
      }
460
};
461

462
BOTAN_REGISTER_COMMAND("speed", Speed);
31✔
463

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