hash_perftest.cc 3.2 KB

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