1 // Copyright 2019 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/hash/sha1.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <string>
10 #include <vector>
11
12 #include "base/hash/hash.h"
13 #include "base/rand_util.h"
14 #include "base/ranges/algorithm.h"
15 #include "base/strings/string_number_conversions.h"
16 #include "base/strings/string_util.h"
17 #include "base/time/time.h"
18 #include "testing/gtest/include/gtest/gtest.h"
19 #include "testing/perf/perf_result_reporter.h"
20
21 namespace base {
22 namespace {
23
Sha1Hash(base::span<const uint8_t> data)24 void Sha1Hash(base::span<const uint8_t> data) {
25 SHA1HashSpan(data);
26 }
27
FastHash(base::span<const uint8_t> data)28 void FastHash(base::span<const uint8_t> data) {
29 base::FastHash(data);
30 }
31
RunTest(const char * hash_name,void (* hash)(base::span<const uint8_t>),const size_t len)32 void RunTest(const char* hash_name,
33 void (*hash)(base::span<const uint8_t>),
34 const size_t len) {
35 constexpr char kMetricRuntime[] = "runtime";
36 constexpr char kMetricThroughput[] = "throughput";
37 // Histograms automatically calculate mean, min, max, and standard deviation,
38 // but not median, so have a separate metric for a manually calculated median.
39 constexpr char kMetricMedianThroughput[] = "median_throughput";
40
41 perf_test::PerfResultReporter reporter(hash_name,
42 NumberToString(len) + "_bytes");
43 reporter.RegisterImportantMetric(kMetricRuntime, "us");
44 reporter.RegisterImportantMetric(kMetricThroughput, "bytesPerSecond");
45 reporter.RegisterImportantMetric(kMetricMedianThroughput, "bytesPerSecond");
46
47 constexpr int kNumRuns = 111;
48 std::vector<TimeDelta> utime(kNumRuns);
49 TimeDelta total_test_time;
50 {
51 std::vector<uint8_t> buf(len);
52 RandBytes(buf.data(), len);
53
54 for (int i = 0; i < kNumRuns; ++i) {
55 const auto start = TimeTicks::Now();
56 hash(buf);
57 utime[i] = TimeTicks::Now() - start;
58 total_test_time += utime[i];
59 }
60 ranges::sort(utime);
61 }
62
63 reporter.AddResult(kMetricRuntime, total_test_time.InMicrosecondsF());
64
65 // Simply dividing len by utime gets us MB/s, but we need B/s.
66 // MB/s = (len / (bytes/megabytes)) / (usecs / usecs/sec)
67 // MB/s = (len / 1,000,000)/(usecs / 1,000,000)
68 // MB/s = (len * 1,000,000)/(usecs * 1,000,000)
69 // MB/s = len/utime
70 constexpr int kBytesPerMegabyte = 1'000'000;
71 const auto rate = [len](TimeDelta t) {
72 return kBytesPerMegabyte * (len / t.InMicrosecondsF());
73 };
74
75 reporter.AddResult(kMetricMedianThroughput, rate(utime[kNumRuns / 2]));
76
77 // Convert to a comma-separated string so we can report every data point.
78 std::vector<std::string> rate_strings(utime.size());
79 ranges::transform(utime, rate_strings.begin(),
80 [rate](const auto& t) { return NumberToString(rate(t)); });
81 reporter.AddResultList(kMetricThroughput, JoinString(rate_strings, ","));
82 }
83
84 } // namespace
85
TEST(SHA1PerfTest,Speed)86 TEST(SHA1PerfTest, Speed) {
87 for (int shift : {1, 5, 6, 7}) {
88 RunTest("SHA1.", Sha1Hash, 1024 * 1024U >> shift);
89 }
90 }
91
TEST(HashPerfTest,Speed)92 TEST(HashPerfTest, Speed) {
93 for (int shift : {1, 5, 6, 7}) {
94 RunTest("FastHash.", FastHash, 1024 * 1024U >> shift);
95 }
96 }
97
98 } // namespace base
99