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.
5 #include "base/rand_util.h"
6 #include "base/time/time.h"
7 #include "testing/gtest/include/gtest/gtest.h"
8 #include "testing/perf/perf_result_reporter.h"
14 constexpr char kMetricPrefix[] = "RandUtil.";
15 constexpr char kThroughput[] = "throughput";
19 TEST(RandUtilPerfTest, RandUint64) {
20 uint64_t inclusive_or = 0;
21 constexpr int kIterations = 1e7;
23 auto before = base::TimeTicks::Now();
24 for (int iter = 0; iter < kIterations; iter++) {
25 inclusive_or |= base::RandUint64();
27 auto after = base::TimeTicks::Now();
29 perf_test::PerfResultReporter reporter(kMetricPrefix, "RandUint64");
30 reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
32 uint64_t nanos_per_iteration = (after - before).InNanoseconds() / kIterations;
33 reporter.AddResult("throughput", static_cast<size_t>(nanos_per_iteration));
34 ASSERT_NE(inclusive_or, static_cast<uint64_t>(0));
37 TEST(RandUtilPerfTest, InsecureRandomRandUint64) {
38 base::InsecureRandomGenerator gen;
41 uint64_t inclusive_or = 0;
42 constexpr int kIterations = 1e7;
44 auto before = base::TimeTicks::Now();
45 for (int iter = 0; iter < kIterations; iter++) {
46 inclusive_or |= gen.RandUint64();
48 auto after = base::TimeTicks::Now();
50 perf_test::PerfResultReporter reporter(kMetricPrefix,
51 "InsecureRandomRandUint64");
52 reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
54 uint64_t nanos_per_iteration = (after - before).InNanoseconds() / kIterations;
55 reporter.AddResult("throughput", static_cast<size_t>(nanos_per_iteration));
56 ASSERT_NE(inclusive_or, static_cast<uint64_t>(0));