1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
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"
9
10 namespace base {
11
12 namespace {
13
14 constexpr char kMetricPrefix[] = "RandUtil.";
15 constexpr char kThroughput[] = "throughput";
16
17 } // namespace
18
TEST(RandUtilPerfTest,RandUint64)19 TEST(RandUtilPerfTest, RandUint64) {
20 uint64_t inclusive_or = 0;
21 constexpr int kIterations = 1e7;
22
23 auto before = base::TimeTicks::Now();
24 for (int iter = 0; iter < kIterations; iter++) {
25 inclusive_or |= base::RandUint64();
26 }
27 auto after = base::TimeTicks::Now();
28
29 perf_test::PerfResultReporter reporter(kMetricPrefix, "RandUint64");
30 reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
31
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));
35 }
36
TEST(RandUtilPerfTest,InsecureRandomRandUint64)37 TEST(RandUtilPerfTest, InsecureRandomRandUint64) {
38 base::InsecureRandomGenerator gen;
39
40 uint64_t inclusive_or = 0;
41 constexpr int kIterations = 1e7;
42
43 auto before = base::TimeTicks::Now();
44 for (int iter = 0; iter < kIterations; iter++) {
45 inclusive_or |= gen.RandUint64();
46 }
47 auto after = base::TimeTicks::Now();
48
49 perf_test::PerfResultReporter reporter(kMetricPrefix,
50 "InsecureRandomRandUint64");
51 reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
52
53 uint64_t nanos_per_iteration = (after - before).InNanoseconds() / kIterations;
54 reporter.AddResult("throughput", static_cast<size_t>(nanos_per_iteration));
55 ASSERT_NE(inclusive_or, static_cast<uint64_t>(0));
56 }
57
58 } // namespace base
59