abseil-cpp/absl/random/internal/randen_benchmarks.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #include "absl/random/internal/randen.h"
16 
17 #include <cstdint>
18 #include <cstdio>
19 #include <cstring>
20 
21 #include "absl/base/internal/raw_logging.h"
22 #include "absl/random/internal/nanobenchmark.h"
23 #include "absl/random/internal/platform.h"
24 #include "absl/random/internal/randen_engine.h"
25 #include "absl/random/internal/randen_hwaes.h"
26 #include "absl/random/internal/randen_slow.h"
27 #include "absl/strings/numbers.h"
28 
29 namespace {
30 
34 
42 
43 // Local state parameters.
44 static constexpr size_t kStateSizeT = Randen::kStateBytes / sizeof(uint64_t);
45 static constexpr size_t kSeedSizeT = Randen::kSeedBytes / sizeof(uint32_t);
46 
47 // Randen implementation benchmarks.
48 template <typename T>
49 struct AbsorbFn : public T {
50  mutable uint64_t state[kStateSizeT] = {};
51  mutable uint32_t seed[kSeedSizeT] = {};
52 
53  static constexpr size_t bytes() { return sizeof(seed); }
54 
55  FuncOutput operator()(const FuncInput num_iters) const {
56  for (size_t i = 0; i < num_iters; ++i) {
57  this->Absorb(seed, state);
58  }
59  return state[0];
60  }
61 };
62 
63 template <typename T>
64 struct GenerateFn : public T {
65  mutable uint64_t state[kStateSizeT];
66  GenerateFn() { std::memset(state, 0, sizeof(state)); }
67 
68  static constexpr size_t bytes() { return sizeof(state); }
69 
70  FuncOutput operator()(const FuncInput num_iters) const {
71  const auto* keys = this->GetKeys();
72  for (size_t i = 0; i < num_iters; ++i) {
73  this->Generate(keys, state);
74  }
75  return state[0];
76  }
77 };
78 
79 template <typename UInt>
80 struct Engine {
82 
83  static constexpr size_t bytes() { return sizeof(UInt); }
84 
85  FuncOutput operator()(const FuncInput num_iters) const {
86  for (size_t i = 0; i < num_iters - 1; ++i) {
87  rng();
88  }
89  return rng();
90  }
91 };
92 
93 template <size_t N>
94 void Print(const char* name, const size_t n, const Result (&results)[N],
95  const size_t bytes) {
96  if (n == 0) {
98  WARNING,
99  "WARNING: Measurement failed, should not happen when using "
100  "PinThreadToCPU unless the region to measure takes > 1 second.\n");
101  return;
102  }
103 
104  static const double ns_per_tick = 1e9 / InvariantTicksPerSecond();
105  static constexpr const double kNsPerS = 1e9; // ns/s
106  static constexpr const double kMBPerByte = 1.0 / 1048576.0; // Mb / b
107  static auto header = [] {
108  return printf("%20s %8s: %12s ticks; %9s (%9s) %8s\n", "Name", "Count",
109  "Total", "Variance", "Time", "bytes/s");
110  }();
111  (void)header;
112 
113  for (size_t i = 0; i < n; ++i) {
114  const double ticks_per_call = results[i].ticks / results[i].input;
115  const double ns_per_call = ns_per_tick * ticks_per_call;
116  const double bytes_per_ns = bytes / ns_per_call;
117  const double mb_per_s = bytes_per_ns * kNsPerS * kMBPerByte;
118  // Output
119  printf("%20s %8zu: %12.2f ticks; MAD=%4.2f%% (%6.1f ns) %8.1f Mb/s\n",
121  results[i].variability * 100.0, ns_per_call, mb_per_s);
122  }
123 }
124 
125 // Fails here
126 template <typename Op, size_t N>
127 void Measure(const char* name, const FuncInput (&inputs)[N]) {
128  Op op;
129 
130  Result results[N];
131  Params params;
132  params.verbose = false;
133  params.max_evals = 6; // avoid test timeout
134  const size_t num_results = MeasureClosure(op, inputs, N, results, params);
135  Print(name, num_results, results, op.bytes());
136 }
137 
138 // unpredictable == 1 but the compiler does not know that.
139 void RunAll(const int argc, char* argv[]) {
140  if (argc == 2) {
141  int cpu = -1;
142  if (!absl::SimpleAtoi(argv[1], &cpu)) {
143  ABSL_RAW_LOG(FATAL, "The optional argument must be a CPU number >= 0.\n");
144  }
145  PinThreadToCPU(cpu);
146  }
147 
148  // The compiler cannot reduce this to a constant.
149  const FuncInput unpredictable = (argc != 999);
150  static const FuncInput inputs[] = {unpredictable * 100, unpredictable * 1000};
151 
152 #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES
153  Measure<AbsorbFn<RandenHwAes>>("Absorb (HwAes)", inputs);
154 #endif
155  Measure<AbsorbFn<RandenSlow>>("Absorb (Slow)", inputs);
156 
157 #if !defined(ABSL_INTERNAL_DISABLE_AES) && ABSL_HAVE_ACCELERATED_AES
158  Measure<GenerateFn<RandenHwAes>>("Generate (HwAes)", inputs);
159 #endif
160  Measure<GenerateFn<RandenSlow>>("Generate (Slow)", inputs);
161 
162  // Measure the production engine.
163  static const FuncInput inputs1[] = {unpredictable * 1000,
164  unpredictable * 10000};
165  Measure<Engine<uint64_t>>("randen_engine<uint64_t>", inputs1);
166  Measure<Engine<uint32_t>>("randen_engine<uint32_t>", inputs1);
167 }
168 
169 } // namespace
170 
171 int main(int argc, char* argv[]) {
172  RunAll(argc, argv);
173  return 0;
174 }
absl::random_internal_nanobenchmark::FuncInput
size_t FuncInput
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:60
memset
return memset(p, 0, total)
keys
const void * keys
Definition: abseil-cpp/absl/random/internal/randen.cc:49
absl::random_internal_nanobenchmark::InvariantTicksPerSecond
double InvariantTicksPerSecond()
Definition: abseil-cpp/absl/random/internal/nanobenchmark.cc:727
absl::random_internal_nanobenchmark::PinThreadToCPU
void PinThreadToCPU(int cpu)
Definition: abseil-cpp/absl/random/internal/nanobenchmark.cc:691
testing::gtest_printers_test::Print
std::string Print(const T &value)
Definition: bloaty/third_party/googletest/googletest/test/googletest-printers-test.cc:233
seed
static const uint8_t seed[20]
Definition: dsa_test.cc:79
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
setup.name
name
Definition: setup.py:542
absl::random_internal_nanobenchmark::FuncOutput
uint64_t FuncOutput
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:63
T
#define T(upbtypeconst, upbtype, ctype, default_value)
absl::random_internal::RandenSlow
Definition: abseil-cpp/absl/random/internal/randen_slow.h:29
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
absl::random_internal::randen_engine
Definition: abseil-cpp/absl/random/internal/randen_engine.h:45
absl::SimpleAtoi
ABSL_NAMESPACE_BEGIN ABSL_MUST_USE_RESULT bool SimpleAtoi(absl::string_view str, int_type *out)
Definition: abseil-cpp/absl/strings/numbers.h:271
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
benchmarks.python.py_benchmark.results
list results
Definition: bloaty/third_party/protobuf/benchmarks/python/py_benchmark.py:145
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
header
struct absl::base_internal::@2940::AllocList::Header header
absl::random_internal::Randen
Definition: abseil-cpp/absl/random/internal/randen.h:34
google::protobuf::WARNING
static const LogLevel WARNING
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:71
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
Json::UInt
unsigned int UInt
Definition: third_party/bloaty/third_party/protobuf/conformance/third_party/jsoncpp/json.h:229
FATAL
#define FATAL(msg)
Definition: task.h:88
N
#define N
Definition: sync_test.cc:37
opencensus.proto.stats.v1.stats_pb2.Measure
Measure
Definition: stats_pb2.py:438
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
absl::random_internal::RandenHwAes
Definition: abseil-cpp/absl/random/internal/randen_hwaes.h:34
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
re2::Engine
Engine
Definition: bloaty/third_party/re2/re2/testing/tester.h:22
main
int main(int argc, char *argv[])
Definition: abseil-cpp/absl/random/internal/randen_benchmarks.cc:171
absl::random_internal_nanobenchmark::MeasureClosure
static size_t MeasureClosure(const Closure &closure, const FuncInput *inputs, const size_t num_inputs, Result *results, const Params &p=Params())
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:159
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
op
static grpc_op * op
Definition: test/core/fling/client.cc:47
ABSL_RAW_LOG
#define ABSL_RAW_LOG(severity,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:44
absl::random_internal_nanobenchmark::Params
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:71
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
state
static struct rpc_state state
Definition: bad_server_response_test.cc:87
ticks
static unsigned long ticks
Definition: benchmark-loop-count.c:30


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:50