rand_util_perftest.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "base/rand_util.h"
  5. #include "base/time/time.h"
  6. #include "testing/gtest/include/gtest/gtest.h"
  7. #include "testing/perf/perf_result_reporter.h"
  8. namespace base {
  9. namespace {
  10. constexpr char kMetricPrefix[] = "RandUtil.";
  11. constexpr char kThroughput[] = "throughput";
  12. } // namespace
  13. TEST(RandUtilPerfTest, RandUint64) {
  14. uint64_t inclusive_or = 0;
  15. constexpr int kIterations = 1e7;
  16. auto before = base::TimeTicks::Now();
  17. for (int iter = 0; iter < kIterations; iter++) {
  18. inclusive_or |= base::RandUint64();
  19. }
  20. auto after = base::TimeTicks::Now();
  21. perf_test::PerfResultReporter reporter(kMetricPrefix, "RandUint64");
  22. reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
  23. uint64_t nanos_per_iteration = (after - before).InNanoseconds() / kIterations;
  24. reporter.AddResult("throughput", static_cast<size_t>(nanos_per_iteration));
  25. ASSERT_NE(inclusive_or, static_cast<uint64_t>(0));
  26. }
  27. TEST(RandUtilPerfTest, InsecureRandomRandUint64) {
  28. base::InsecureRandomGenerator gen;
  29. uint64_t inclusive_or = 0;
  30. constexpr int kIterations = 1e7;
  31. auto before = base::TimeTicks::Now();
  32. for (int iter = 0; iter < kIterations; iter++) {
  33. inclusive_or |= gen.RandUint64();
  34. }
  35. auto after = base::TimeTicks::Now();
  36. perf_test::PerfResultReporter reporter(kMetricPrefix,
  37. "InsecureRandomRandUint64");
  38. reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
  39. uint64_t nanos_per_iteration = (after - before).InNanoseconds() / kIterations;
  40. reporter.AddResult("throughput", static_cast<size_t>(nanos_per_iteration));
  41. ASSERT_NE(inclusive_or, static_cast<uint64_t>(0));
  42. }
  43. } // namespace base