| 6 |
| 7 | #include <stddef.h> |
| 8 | #include <stdint.h> |
| 9 | #include <algorithm> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 |
| 13 | #include "base/rand_util.h" |
| 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/time/time.h" |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | #include "testing/perf/perf_result_reporter.h" |
| 18 |
| 19 | namespace { |
| 20 |
| 21 | constexpr int kBytesPerMegabyte = 1000000; |
| 22 |
| 23 | constexpr char kMetricPrefixSHA1[] = "SHA1."; |
| 24 | constexpr char kMetricRuntime[] = "runtime"; |
| 25 | constexpr char kMetricThroughput[] = "throughput"; |
| 26 | // Histograms automatically calculate mean, min, max, and standard deviation, |
| 27 | // but not median, so have a separate metric for our manually calculated median. |
| 28 | constexpr char kMetricMedianThroughput[] = "median_throughput"; |
| 29 |
| 30 | perf_test::PerfResultReporter SetUpReporter(const std::string& story_name) { |
| 31 | perf_test::PerfResultReporter reporter(kMetricPrefixSHA1, story_name); |
| 32 | reporter.RegisterImportantMetric(kMetricRuntime, "us"); |
| 33 | reporter.RegisterImportantMetric(kMetricThroughput, "bytesPerSecond"); |
| 34 | reporter.RegisterImportantMetric(kMetricMedianThroughput, "bytesPerSecond"); |
| 35 | return reporter; |
| 36 | } |
| 37 |
| 38 | } // namespace |
| 39 |
| 40 | static void Timing(const size_t len) { |
| 41 | std::vector<uint8_t> buf(len); |
| 42 | base::RandBytes(buf.data(), len); |
| 43 |
| 44 | const int runs = 111; |
| 45 | std::vector<base::TimeDelta> utime(runs); |
| 46 | unsigned char digest[base::kSHA1Length]; |
| 47 | memset(digest, 0, base::kSHA1Length); |
| 48 |
| 49 | double total_test_time = 0.0; |
| 50 | for (int i = 0; i < runs; ++i) { |
| 51 | auto start = base::TimeTicks::Now(); |
| 52 | base::SHA1HashBytes(buf.data(), len, digest); |
| 53 | auto end = base::TimeTicks::Now(); |
| 54 | utime[i] = end - start; |
| 55 | total_test_time += utime[i].InMicroseconds(); |
| 56 | } |
| 57 |
| 58 | std::sort(utime.begin(), utime.end()); |
| 59 | const int med = runs / 2; |
| 60 |
| 61 | // Simply dividing len by utime gets us MB/s, but we need B/s. |
| 62 | // MB/s = (len / (bytes/megabytes)) / (usecs / usecs/sec) |
| 63 | // MB/s = (len / 1,000,000)/(usecs / 1,000,000) |
| 64 | // MB/s = (len * 1,000,000)/(usecs * 1,000,000) |
| 65 | // MB/s = len/utime |
| 66 | double median_rate = kBytesPerMegabyte * len / utime[med].InMicroseconds(); |
| 67 | // Convert to a comma-separated string so we can report every data point. |
| 68 | std::string rates; |
| 69 | for (const auto& t : utime) { |
| 70 | rates += |
| 71 | base::NumberToString(kBytesPerMegabyte * len / t.InMicroseconds()) + |
| 72 | ","; |
| 73 | } |
| 74 | // Strip off trailing comma. |
| 75 | rates.pop_back(); |
| 76 |
| 77 | auto reporter = SetUpReporter(base::NumberToString(len) + "_bytes"); |
| 78 | reporter.AddResult(kMetricRuntime, total_test_time); |
| 79 | reporter.AddResult(kMetricMedianThroughput, median_rate); |
| 80 | reporter.AddResultList(kMetricThroughput, rates); |
| 81 | } |
| 82 |
| 83 | TEST(SHA1PerfTest, Speed) { |
| 84 | Timing(1024 * 1024U >> 1); |
| 85 | Timing(1024 * 1024U >> 5); |
| 86 | Timing(1024 * 1024U >> 6); |
| 87 | Timing(1024 * 1024U >> 7); |
| 88 | } |
| 89 |