histogram_tester.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // Copyright 2014 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. #ifndef BASE_TEST_METRICS_HISTOGRAM_TESTER_H_
  5. #define BASE_TEST_METRICS_HISTOGRAM_TESTER_H_
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <ostream>
  10. #include <string>
  11. #include <utility>
  12. #include <vector>
  13. #include "base/macros.h"
  14. #include "base/metrics/histogram.h"
  15. #include "base/metrics/histogram_base.h"
  16. #include "base/strings/string_piece.h"
  17. #include "base/time/time.h"
  18. namespace base {
  19. struct Bucket;
  20. class HistogramSamples;
  21. // HistogramTester provides a simple interface for examining histograms, UMA
  22. // or otherwise. Tests can use this interface to verify that histogram data is
  23. // getting logged as intended.
  24. //
  25. // Note: When using this class from a browser test, one might have to call
  26. // SubprocessMetricsProvider::MergeHistogramDeltasForTesting() to sync the
  27. // histogram data between the renderer and browser processes. If it is in a
  28. // content browser test, then content::FetchHistogramsFromChildProcesses()
  29. // should be used to achieve that.
  30. class HistogramTester {
  31. public:
  32. using CountsMap = std::map<std::string, HistogramBase::Count, std::less<>>;
  33. // Takes a snapshot of all current histograms counts.
  34. HistogramTester();
  35. ~HistogramTester();
  36. // We know the exact number of samples in a bucket, and that no other bucket
  37. // should have samples. Measures the diff from the snapshot taken when this
  38. // object was constructed.
  39. void ExpectUniqueSample(StringPiece name,
  40. HistogramBase::Sample sample,
  41. HistogramBase::Count expected_count) const;
  42. template <typename T>
  43. void ExpectUniqueSample(StringPiece name,
  44. T sample,
  45. HistogramBase::Count expected_count) const {
  46. ExpectUniqueSample(name, static_cast<HistogramBase::Sample>(sample),
  47. expected_count);
  48. }
  49. void ExpectUniqueTimeSample(StringPiece name,
  50. TimeDelta sample,
  51. HistogramBase::Count expected_count) const;
  52. // We know the exact number of samples in a bucket, but other buckets may
  53. // have samples as well. Measures the diff from the snapshot taken when this
  54. // object was constructed.
  55. void ExpectBucketCount(StringPiece name,
  56. HistogramBase::Sample sample,
  57. HistogramBase::Count expected_count) const;
  58. template <typename T>
  59. void ExpectBucketCount(StringPiece name,
  60. T sample,
  61. HistogramBase::Count expected_count) const {
  62. ExpectBucketCount(name, static_cast<HistogramBase::Sample>(sample),
  63. expected_count);
  64. }
  65. // We don't know the values of the samples, but we know how many there are.
  66. // This measures the diff from the snapshot taken when this object was
  67. // constructed.
  68. void ExpectTotalCount(StringPiece name, HistogramBase::Count count) const;
  69. // We know exact number of samples for buckets corresponding to a time
  70. // interval. Other intervals may have samples too.
  71. void ExpectTimeBucketCount(StringPiece name,
  72. TimeDelta sample,
  73. HistogramBase::Count count) const;
  74. // Returns a list of all of the buckets recorded since creation of this
  75. // object, as vector<Bucket>, where the Bucket represents the min boundary of
  76. // the bucket and the count of samples recorded to that bucket since creation.
  77. //
  78. // Example usage, using gMock:
  79. // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
  80. // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
  81. //
  82. // If you build the expected list programmatically, you can use ContainerEq:
  83. // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
  84. // ContainerEq(expected_buckets));
  85. //
  86. // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a
  87. // slightly less helpful failure message:
  88. // EXPECT_EQ(expected_buckets,
  89. // histogram_tester.GetAllSamples("HistogramName"));
  90. std::vector<Bucket> GetAllSamples(StringPiece name) const;
  91. // Returns the value of the |sample| bucket for ths histogram |name|.
  92. HistogramBase::Count GetBucketCount(StringPiece name,
  93. HistogramBase::Sample sample) const;
  94. template <typename T>
  95. HistogramBase::Count GetBucketCount(StringPiece name, T sample) const {
  96. return GetBucketCount(name, static_cast<HistogramBase::Sample>(sample));
  97. }
  98. // Finds histograms whose names start with |prefix|, and returns them along
  99. // with the counts of any samples added since the creation of this object.
  100. // Histograms that are unchanged are omitted from the result. The return value
  101. // is a map whose keys are the histogram name, and whose values are the sample
  102. // count.
  103. //
  104. // This is useful for cases where the code under test is choosing among a
  105. // family of related histograms and incrementing one of them. Typically you
  106. // should pass the result of this function directly to EXPECT_THAT.
  107. //
  108. // Example usage, using gmock (which produces better failure messages):
  109. // #include "testing/gmock/include/gmock/gmock.h"
  110. // ...
  111. // base::HistogramTester::CountsMap expected_counts;
  112. // expected_counts["MyMetric.A"] = 1;
  113. // expected_counts["MyMetric.B"] = 1;
  114. // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."),
  115. // testing::ContainerEq(expected_counts));
  116. CountsMap GetTotalCountsForPrefix(StringPiece prefix) const;
  117. // Access a modified HistogramSamples containing only what has been logged
  118. // to the histogram since the creation of this object.
  119. std::unique_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
  120. StringPiece histogram_name) const;
  121. // Dumps all histograms that have had new samples added to them into a string,
  122. // for debugging purposes. Note: this will dump the entire contents of any
  123. // modified histograms and not just the modified buckets.
  124. std::string GetAllHistogramsRecorded() const;
  125. private:
  126. // Verifies and asserts that value in the |sample| bucket matches the
  127. // |expected_count|. The bucket's current value is determined from |samples|
  128. // and is modified based on the snapshot stored for histogram |name|.
  129. void CheckBucketCount(StringPiece name,
  130. HistogramBase::Sample sample,
  131. Histogram::Count expected_count,
  132. const HistogramSamples& samples) const;
  133. // Verifies that the total number of values recorded for the histogram |name|
  134. // is |expected_count|. This is checked against |samples| minus the snapshot
  135. // that was taken for |name|.
  136. void CheckTotalCount(StringPiece name,
  137. Histogram::Count expected_count,
  138. const HistogramSamples& samples) const;
  139. // Sets the value for |count| to be the value in the |sample| bucket. The
  140. // bucket's current value is determined from |samples| and is modified based
  141. // on the snapshot stored for histogram |name|.
  142. void GetBucketCountForSamples(StringPiece name,
  143. HistogramBase::Sample sample,
  144. const HistogramSamples& samples,
  145. HistogramBase::Count* count) const;
  146. // Used to determine the histogram changes made during this instance's
  147. // lifecycle.
  148. std::map<std::string, std::unique_ptr<HistogramSamples>, std::less<>>
  149. histograms_snapshot_;
  150. DISALLOW_COPY_AND_ASSIGN(HistogramTester);
  151. };
  152. struct Bucket {
  153. Bucket(HistogramBase::Sample min, HistogramBase::Count count)
  154. : min(min), count(count) {}
  155. bool operator==(const Bucket& other) const;
  156. HistogramBase::Sample min;
  157. HistogramBase::Count count;
  158. };
  159. void PrintTo(const Bucket& value, std::ostream* os);
  160. } // namespace base
  161. #endif // BASE_TEST_METRICS_HISTOGRAM_TESTER_H_