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

randombit / botan / 11336383167

14 Oct 2024 11:16PM UTC coverage: 91.121% (+0.001%) from 91.12%
11336383167

push

github

web-flow
Merge pull request #4369 from randombit/jack/refactor-speed-algos

Refactor performance testing of symmetric algorithms

90097 of 98876 relevant lines covered (91.12%)

9212419.84 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
            "SPHINCS+",
288
            "FrodoKEM",
289
            "HSS-LMS",
290
         };
×
291
         // clang-format on
292
      }
293

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

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

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

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

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

317
         m_clock_cycle_ratio = 1.0 / m_clock_cycle_ratio;
30✔
318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

383
               Botan::RandomNumberGenerator& rng() const override { return m_speed->rng(); }
9,935✔
384

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

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

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

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

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

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

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

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

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

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

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

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

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

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