abseil-cpp/absl/random/internal/nanobenchmark_test.cc
Go to the documentation of this file.
1 // Copyright 2017 Google Inc. All Rights Reserved.
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/nanobenchmark.h"
16 
17 #include "absl/base/internal/raw_logging.h"
18 #include "absl/strings/numbers.h"
19 
20 namespace absl {
22 namespace random_internal_nanobenchmark {
23 namespace {
24 
25 uint64_t Div(const void*, FuncInput in) {
26  // Here we're measuring the throughput because benchmark invocations are
27  // independent.
28  const int64_t d1 = 0xFFFFFFFFFFll / int64_t(in); // IDIV
29  return d1;
30 }
31 
32 template <size_t N>
33 void MeasureDiv(const FuncInput (&inputs)[N]) {
34  Result results[N];
35  Params params;
36  params.max_evals = 6; // avoid test timeout
37  const size_t num_results = Measure(&Div, nullptr, inputs, N, results, params);
38  if (num_results == 0) {
40  WARNING,
41  "WARNING: Measurement failed, should not happen when using "
42  "PinThreadToCPU unless the region to measure takes > 1 second.\n");
43  return;
44  }
45  for (size_t i = 0; i < num_results; ++i) {
46  ABSL_RAW_LOG(INFO, "%5zu: %6.2f ticks; MAD=%4.2f%%\n", results[i].input,
47  results[i].ticks, results[i].variability * 100.0);
48  ABSL_RAW_CHECK(results[i].ticks != 0.0f, "Zero duration");
49  }
50 }
51 
52 void RunAll(const int argc, char* argv[]) {
53  // Avoid migrating between cores - important on multi-socket systems.
54  int cpu = -1;
55  if (argc == 2) {
56  if (!absl::SimpleAtoi(argv[1], &cpu)) {
57  ABSL_RAW_LOG(FATAL, "The optional argument must be a CPU number >= 0.\n");
58  }
59  }
60  PinThreadToCPU(cpu);
61 
62  // unpredictable == 1 but the compiler doesn't know that.
63  const FuncInput unpredictable = argc != 999;
64  static const FuncInput inputs[] = {unpredictable * 10, unpredictable * 100};
65 
66  MeasureDiv(inputs);
67 }
68 
69 } // namespace
70 } // namespace random_internal_nanobenchmark
72 } // namespace absl
73 
74 int main(int argc, char* argv[]) {
75  absl::random_internal_nanobenchmark::RunAll(argc, argv);
76  return 0;
77 }
absl::random_internal_nanobenchmark::FuncInput
size_t FuncInput
Definition: abseil-cpp/absl/random/internal/nanobenchmark.h:60
ABSL_RAW_CHECK
#define ABSL_RAW_CHECK(condition, message)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:59
absl::random_internal_nanobenchmark::Measure
size_t Measure(const Func func, const void *arg, const FuncInput *inputs, const size_t num_inputs, Result *results, const Params &p)
Definition: abseil-cpp/absl/random/internal/nanobenchmark.cc:780
absl::random_internal_nanobenchmark::PinThreadToCPU
void PinThreadToCPU(int cpu)
Definition: abseil-cpp/absl/random/internal/nanobenchmark.cc:691
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
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
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
python_utils.jobset.INFO
INFO
Definition: jobset.py:111
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
google::protobuf::WARNING
static const LogLevel WARNING
Definition: bloaty/third_party/protobuf/src/google/protobuf/testing/googletest.h:71
FATAL
#define FATAL(msg)
Definition: task.h:88
N
#define N
Definition: sync_test.cc:37
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
ABSL_RAW_LOG
#define ABSL_RAW_LOG(severity,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:44
main
int main(int argc, char *argv[])
Definition: abseil-cpp/absl/random/internal/nanobenchmark_test.cc:74
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ticks
static unsigned long ticks
Definition: benchmark-loop-count.c:30
absl::str_format_internal::LengthMod::ll
@ ll


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