tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

nanobenchmark_test.cc (2576B)


      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/log/check.h"
     18 #include "absl/log/log.h"
     19 #include "absl/strings/numbers.h"
     20 #include "absl/strings/str_format.h"
     21 
     22 namespace absl {
     23 ABSL_NAMESPACE_BEGIN
     24 namespace random_internal_nanobenchmark {
     25 namespace {
     26 
     27 uint64_t Div(const void*, FuncInput in) {
     28  // Here we're measuring the throughput because benchmark invocations are
     29  // independent.
     30  const int64_t d1 = 0xFFFFFFFFFFll / int64_t(in);  // IDIV
     31  return d1;
     32 }
     33 
     34 template <size_t N>
     35 void MeasureDiv(const FuncInput (&inputs)[N]) {
     36  Result results[N];
     37  Params params;
     38  params.max_evals = 6;  // avoid test timeout
     39  const size_t num_results = Measure(&Div, nullptr, inputs, N, results, params);
     40  if (num_results == 0) {
     41    LOG(WARNING)
     42        << "WARNING: Measurement failed, should not happen when using "
     43           "PinThreadToCPU unless the region to measure takes > 1 second.";
     44    return;
     45  }
     46  for (size_t i = 0; i < num_results; ++i) {
     47    LOG(INFO) << absl::StreamFormat("%5u: %6.2f ticks; MAD=%4.2f%%\n",
     48                                    results[i].input, results[i].ticks,
     49                                    results[i].variability * 100.0);
     50    CHECK_NE(results[i].ticks, 0.0f) << "Zero duration";
     51  }
     52 }
     53 
     54 void RunAll(const int argc, char* argv[]) {
     55  // Avoid migrating between cores - important on multi-socket systems.
     56  int cpu = -1;
     57  if (argc == 2) {
     58    if (!absl::SimpleAtoi(argv[1], &cpu)) {
     59      LOG(FATAL) << "The optional argument must be a CPU number >= 0.";
     60    }
     61  }
     62  PinThreadToCPU(cpu);
     63 
     64  // unpredictable == 1 but the compiler doesn't know that.
     65  const FuncInput unpredictable = argc != 999;
     66  static const FuncInput inputs[] = {unpredictable * 10, unpredictable * 100};
     67 
     68  MeasureDiv(inputs);
     69 }
     70 
     71 }  // namespace
     72 }  // namespace random_internal_nanobenchmark
     73 ABSL_NAMESPACE_END
     74 }  // namespace absl
     75 
     76 int main(int argc, char* argv[]) {
     77  absl::random_internal_nanobenchmark::RunAll(argc, argv);
     78  return 0;
     79 }