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