Skip to content

Commit c2a759b

Browse files
authored
Merge pull request #4364 from randombit/jack/refactor-speed
Initial refactoring of speed cmdlet
2 parents 9240603 + 85bb3e2 commit c2a759b

10 files changed

Lines changed: 1074 additions & 855 deletions

File tree

doc/dev_ref/todo.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ CLI
135135

136136
* Add a ``--completion`` option to dump autocomplete info, write
137137
support for autocompletion in bash/zsh.
138-
* Refactor ``speed``
139138
* Change `tls_server` to be a tty<->socket app, like `tls_client` is,
140139
instead of a bogus echo server.
141140
* `encrypt` / `decrypt` tools providing password based file encryption

src/cli/perf.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* (C) 2024 Jack Lloyd
3+
*
4+
* Botan is released under the Simplified BSD License (see license.txt)
5+
*/
6+
7+
#include "perf.h"
8+
#include "cli_exceptions.h"
9+
#include <map>
10+
11+
namespace Botan_CLI {
12+
13+
PerfTest::Registration::Registration(const std::string& name, const PerfTest::pt_maker_fn& maker_fn) {
14+
std::map<std::string, PerfTest::pt_maker_fn>& reg = PerfTest::global_registry();
15+
16+
if(reg.contains(name)) {
17+
throw CLI_Error("Duplicated registration of command " + name);
18+
}
19+
20+
reg.insert(std::make_pair(name, maker_fn));
21+
}
22+
23+
//static
24+
std::map<std::string, PerfTest::pt_maker_fn>& PerfTest::global_registry() {
25+
static std::map<std::string, PerfTest::pt_maker_fn> g_perf_tests;
26+
return g_perf_tests;
27+
}
28+
29+
//static
30+
std::unique_ptr<PerfTest> PerfTest::get(const std::string& name) {
31+
const auto& reg = PerfTest::global_registry();
32+
33+
auto i = reg.find(name);
34+
if(i != reg.end()) {
35+
return i->second();
36+
}
37+
38+
return nullptr;
39+
}
40+
41+
} // namespace Botan_CLI

src/cli/perf.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* (C) 2024 Jack Lloyd
3+
*
4+
* Botan is released under the Simplified BSD License (see license.txt)
5+
*/
6+
7+
#ifndef BOTAN_CLI_PERF_H_
8+
#define BOTAN_CLI_PERF_H_
9+
10+
#include <botan/rng.h>
11+
#include <botan/internal/fmt.h>
12+
#include <botan/internal/timer.h>
13+
#include <chrono>
14+
#include <functional>
15+
#include <iosfwd>
16+
#include <map>
17+
#include <string>
18+
19+
namespace Botan_CLI {
20+
21+
class PerfConfig {
22+
public:
23+
virtual ~PerfConfig() = default;
24+
25+
virtual std::chrono::milliseconds runtime() const = 0;
26+
27+
//virtual std::vector<std::string> providers() const = 0;
28+
29+
virtual const std::vector<size_t>& buffer_sizes() const = 0;
30+
31+
virtual std::ostream& error_output() const = 0;
32+
33+
virtual Botan::RandomNumberGenerator& rng() const = 0;
34+
35+
virtual void record_result(const Botan::Timer& timer) const = 0;
36+
37+
virtual std::unique_ptr<Botan::Timer> make_timer(const std::string& alg,
38+
uint64_t event_mult = 1,
39+
const std::string& what = "",
40+
const std::string& provider = "",
41+
size_t buf_size = 0) const = 0;
42+
};
43+
44+
class PerfTest {
45+
public:
46+
virtual ~PerfTest() = default;
47+
48+
// Returns nullptr if unknown / not available
49+
static std::unique_ptr<PerfTest> get(const std::string& alg);
50+
51+
virtual void go(const PerfConfig& config) = 0;
52+
53+
typedef std::function<std::unique_ptr<PerfTest>()> pt_maker_fn;
54+
55+
class Registration final {
56+
public:
57+
Registration(const std::string& name, const std::function<std::unique_ptr<PerfTest>()>& maker_fn);
58+
};
59+
60+
private:
61+
static std::map<std::string, pt_maker_fn>& global_registry();
62+
};
63+
64+
#define BOTAN_REGISTER_PERF_TEST(name, Perf_Class) \
65+
const Botan_CLI::PerfTest::Registration reg_perf_##Perf_Class( \
66+
name, []() -> std::unique_ptr<Botan_CLI::PerfTest> { return std::make_unique<Perf_Class>(); })
67+
68+
} // namespace Botan_CLI
69+
70+
#endif

0 commit comments

Comments
 (0)