-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark-prng-params.cpp
More file actions
145 lines (106 loc) · 9.6 KB
/
benchmark-prng-params.cpp
File metadata and controls
145 lines (106 loc) · 9.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// SPDX-FileCopyrightText: Steven Ward
// SPDX-License-Identifier: MPL-2.0
#include "randp.hpp"
#include <benchmark/benchmark.h> // https://github.com/google/benchmark
#include <bit>
using func_t = void (&)(void*, size_t);
void
BM_rand_bytes_4GiB(benchmark::State& BM_state, func_t& fn)
{
// Perform setup here
uint8_t buf[1U << 8];
static_assert(sizeof(buf) <= 256,
"getentropy will fail if more than 256 bytes are requested");
static_assert(std::has_single_bit(sizeof(buf)), "buffer size must be a power of 2");
for (auto _ : BM_state) // NOLINT(clang-analyzer-deadcode.DeadStores)
{
// This code gets timed
for (size_t i = 0; i < (1UL << 32) / sizeof(buf); ++i)
{
fn(buf, sizeof(buf));
}
}
}
#include <algorithm>
#include <cstdlib>
#include <string>
#include <thread>
int
main([[maybe_unused]] int argc, [[maybe_unused]] char* argv[])
{
// Copied from benchmark.h
benchmark::MaybeReenterWithoutASLR(argc, argv);
benchmark::Initialize(&argc, argv);
if (benchmark::ReportUnrecognizedArguments(argc, argv))
return 1;
// {{{ determine num_threads
constexpr int min_threads = 1;
const auto max_threads =
std::max(min_threads, static_cast<int>(std::thread::hardware_concurrency()));
// https://en.wikipedia.org/wiki/Elvis_operator
//const auto max_threads = static_cast<int>(std::thread::hardware_concurrency()) ?: min_threads;
auto num_threads = min_threads;
try
{
num_threads = std::stoi(std::getenv("NUM_THREADS"));
}
catch (...)
{
num_threads = min_threads;
}
num_threads = std::clamp(num_threads, min_threads, max_threads);
/*
if (num_threads > min_threads)
// Don't use all the cores
--num_threads;
*/
// }}}
// {{{ accuracy testing
// }}}
// {{{ speed
std::string prefix;
if (num_threads == 1)
{
prefix = "rand_bytes_4GiB:";
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,1,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 1, 3>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,1,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 1, 4>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 2>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 3>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 4>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,1>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 1>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 2>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 3>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,1,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 1, 3>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,1,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 1, 4>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 2>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 3>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 4>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,1>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 1>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 2>)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 3>)->Unit(benchmark::kMillisecond);
}
else
{
prefix = "rand_bytes_4GiB:";
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,1,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 1, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,1,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 1, 4>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 2>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,2,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 2, 4>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,1>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 1>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 2>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,dm,3,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, true, 3, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,1,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 1, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,1,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 1, 4>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 2>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,2,4>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 2, 4>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,1>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 1>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,2>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 2>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
benchmark::RegisterBenchmark(prefix + "randp_bytes<def,def,enc,no-dm,3,3>", BM_rand_bytes_4GiB, randp_bytes<DEFAULT_RANDP_NUM_BLOCKS, DEFAULT_RANDP_RESEED_COUNTDOWN_MIN, true, false, 3, 3>)->Threads(num_threads)->Unit(benchmark::kMillisecond);
}
benchmark::RunSpecifiedBenchmarks();
benchmark::Shutdown();
// }}}
return 0;
}