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.
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;
40 uint64_t inclusive_or = 0;
41 constexpr int kIterations = 1e7;
43 auto before = base::TimeTicks::Now();
44 for (int iter = 0; iter < kIterations; iter++) {
45 inclusive_or |= gen.RandUint64();
47 auto after = base::TimeTicks::Now();
49 perf_test::PerfResultReporter reporter(kMetricPrefix,
50 "InsecureRandomRandUint64");
51 reporter.RegisterImportantMetric(kThroughput, "ns / iteration");
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));