OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_TEST_HISTOGRAM_TESTER_H_ | 5 #ifndef BASE_TEST_HISTOGRAM_TESTER_H_ |
6 #define BASE_TEST_HISTOGRAM_TESTER_H_ | 6 #define BASE_TEST_HISTOGRAM_TESTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
| 9 #include <ostream> |
9 #include <string> | 10 #include <string> |
| 11 #include <utility> |
| 12 #include <vector> |
10 | 13 |
11 #include "base/basictypes.h" | 14 #include "base/basictypes.h" |
12 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
13 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
14 #include "base/metrics/histogram_base.h" | 17 #include "base/metrics/histogram_base.h" |
15 | 18 |
16 namespace base { | 19 namespace base { |
17 | 20 |
| 21 struct Bucket; |
18 class HistogramSamples; | 22 class HistogramSamples; |
19 | 23 |
20 // HistogramTester provides a simple interface for examining histograms, UMA | 24 // HistogramTester provides a simple interface for examining histograms, UMA |
21 // or otherwise. Tests can use this interface to verify that histogram data is | 25 // or otherwise. Tests can use this interface to verify that histogram data is |
22 // getting logged as intended. | 26 // getting logged as intended. |
23 class HistogramTester { | 27 class HistogramTester { |
24 public: | 28 public: |
25 // The constructor will call StatisticsRecorder::Initialize() for you. Also, | 29 // The constructor will call StatisticsRecorder::Initialize() for you. Also, |
26 // this takes a snapshot of all current histograms counts. | 30 // this takes a snapshot of all current histograms counts. |
27 HistogramTester(); | 31 HistogramTester(); |
(...skipping 12 matching lines...) Expand all Loading... |
40 void ExpectBucketCount(const std::string& name, | 44 void ExpectBucketCount(const std::string& name, |
41 base::HistogramBase::Sample sample, | 45 base::HistogramBase::Sample sample, |
42 base::HistogramBase::Count expected_count) const; | 46 base::HistogramBase::Count expected_count) const; |
43 | 47 |
44 // We don't know the values of the samples, but we know how many there are. | 48 // We don't know the values of the samples, but we know how many there are. |
45 // This measures the diff from the snapshot taken when this object was | 49 // This measures the diff from the snapshot taken when this object was |
46 // constructed. | 50 // constructed. |
47 void ExpectTotalCount(const std::string& name, | 51 void ExpectTotalCount(const std::string& name, |
48 base::HistogramBase::Count count) const; | 52 base::HistogramBase::Count count) const; |
49 | 53 |
| 54 // Returns a list of all of the buckets recorded since creation of this |
| 55 // object, as vector<Bucket>, where the Bucket represents the min boundary of |
| 56 // the bucket and the count of samples recorded to that bucket since creation. |
| 57 // |
| 58 // Example usage, using gMock: |
| 59 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), |
| 60 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5))); |
| 61 // |
| 62 // If you build the expected list programmatically, you can use ContainerEq: |
| 63 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), |
| 64 // ContainerEq(expected_buckets)); |
| 65 // |
| 66 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a |
| 67 // slightly less helpful failure message: |
| 68 // EXPECT_EQ(expected_buckets, |
| 69 // histogram_tester.GetAllSamples("HistogramName")); |
| 70 std::vector<Bucket> GetAllSamples(const std::string& name); |
| 71 |
50 // Access a modified HistogramSamples containing only what has been logged | 72 // Access a modified HistogramSamples containing only what has been logged |
51 // to the histogram since the creation of this object. | 73 // to the histogram since the creation of this object. |
52 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( | 74 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( |
53 const std::string& histogram_name); | 75 const std::string& histogram_name); |
54 | 76 |
55 private: | 77 private: |
56 // Verifies and asserts that value in the |sample| bucket matches the | 78 // Verifies and asserts that value in the |sample| bucket matches the |
57 // |expected_count|. The bucket's current value is determined from |samples| | 79 // |expected_count|. The bucket's current value is determined from |samples| |
58 // and is modified based on the snapshot stored for histogram |name|. | 80 // and is modified based on the snapshot stored for histogram |name|. |
59 void CheckBucketCount(const std::string& name, | 81 void CheckBucketCount(const std::string& name, |
60 base::HistogramBase::Sample sample, | 82 base::HistogramBase::Sample sample, |
61 base::Histogram::Count expected_count, | 83 base::Histogram::Count expected_count, |
62 const base::HistogramSamples& samples) const; | 84 const base::HistogramSamples& samples) const; |
63 | 85 |
64 // Verifies that the total number of values recorded for the histogram |name| | 86 // Verifies that the total number of values recorded for the histogram |name| |
65 // is |expected_count|. This is checked against |samples| minus the snapshot | 87 // is |expected_count|. This is checked against |samples| minus the snapshot |
66 // that was taken for |name|. | 88 // that was taken for |name|. |
67 void CheckTotalCount(const std::string& name, | 89 void CheckTotalCount(const std::string& name, |
68 base::Histogram::Count expected_count, | 90 base::Histogram::Count expected_count, |
69 const base::HistogramSamples& samples) const; | 91 const base::HistogramSamples& samples) const; |
70 | 92 |
71 // Used to determine the histogram changes made during this instance's | 93 // Used to determine the histogram changes made during this instance's |
72 // lifecycle. This instance takes ownership of the samples, which are deleted | 94 // lifecycle. This instance takes ownership of the samples, which are deleted |
73 // when the instance is destroyed. | 95 // when the instance is destroyed. |
74 std::map<std::string, HistogramSamples*> histograms_snapshot_; | 96 std::map<std::string, HistogramSamples*> histograms_snapshot_; |
75 | 97 |
76 DISALLOW_COPY_AND_ASSIGN(HistogramTester); | 98 DISALLOW_COPY_AND_ASSIGN(HistogramTester); |
77 }; | 99 }; |
78 | 100 |
| 101 struct Bucket { |
| 102 Bucket(base::HistogramBase::Sample min, base::HistogramBase::Count count) |
| 103 : min(min), count(count) {} |
| 104 |
| 105 bool operator==(const Bucket& other) const; |
| 106 |
| 107 base::HistogramBase::Sample min; |
| 108 base::HistogramBase::Count count; |
| 109 }; |
| 110 |
| 111 void PrintTo(const Bucket& value, std::ostream* os); |
| 112 |
79 } // namespace base | 113 } // namespace base |
80 | 114 |
81 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ | 115 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ |
OLD | NEW |