Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: base/test/histogram_tester.h

Issue 2628843002: Match only prefixes of histogram names. (Closed)
Patch Set: Address comments of isherman@; omit explicit namespace. Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | base/test/histogram_tester.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <memory> 9 #include <memory>
10 #include <ostream> 10 #include <ostream>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 #include <vector> 13 #include <vector>
14 14
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/metrics/histogram.h" 16 #include "base/metrics/histogram.h"
17 #include "base/metrics/histogram_base.h" 17 #include "base/metrics/histogram_base.h"
18 #include "base/time/time.h" 18 #include "base/time/time.h"
19 19
20 namespace base { 20 namespace base {
21 21
22 struct Bucket; 22 struct Bucket;
23 class HistogramSamples; 23 class HistogramSamples;
24 24
25 // HistogramTester provides a simple interface for examining histograms, UMA 25 // HistogramTester provides a simple interface for examining histograms, UMA
26 // or otherwise. Tests can use this interface to verify that histogram data is 26 // or otherwise. Tests can use this interface to verify that histogram data is
27 // getting logged as intended. 27 // getting logged as intended.
28 class HistogramTester { 28 class HistogramTester {
29 public: 29 public:
30 using CountsMap = std::map<std::string, base::HistogramBase::Count>; 30 using CountsMap = std::map<std::string, HistogramBase::Count>;
31 31
32 // The constructor will call StatisticsRecorder::Initialize() for you. Also, 32 // The constructor will call StatisticsRecorder::Initialize() for you. Also,
33 // this takes a snapshot of all current histograms counts. 33 // this takes a snapshot of all current histograms counts.
34 HistogramTester(); 34 HistogramTester();
35 ~HistogramTester(); 35 ~HistogramTester();
36 36
37 // We know the exact number of samples in a bucket, and that no other bucket 37 // We know the exact number of samples in a bucket, and that no other bucket
38 // should have samples. Measures the diff from the snapshot taken when this 38 // should have samples. Measures the diff from the snapshot taken when this
39 // object was constructed. 39 // object was constructed.
40 void ExpectUniqueSample(const std::string& name, 40 void ExpectUniqueSample(const std::string& name,
41 base::HistogramBase::Sample sample, 41 HistogramBase::Sample sample,
42 base::HistogramBase::Count expected_count) const; 42 HistogramBase::Count expected_count) const;
43 43
44 // We know the exact number of samples in a bucket, but other buckets may 44 // We know the exact number of samples in a bucket, but other buckets may
45 // have samples as well. Measures the diff from the snapshot taken when this 45 // have samples as well. Measures the diff from the snapshot taken when this
46 // object was constructed. 46 // object was constructed.
47 void ExpectBucketCount(const std::string& name, 47 void ExpectBucketCount(const std::string& name,
48 base::HistogramBase::Sample sample, 48 HistogramBase::Sample sample,
49 base::HistogramBase::Count expected_count) const; 49 HistogramBase::Count expected_count) const;
50 50
51 // We don't know the values of the samples, but we know how many there are. 51 // We don't know the values of the samples, but we know how many there are.
52 // This measures the diff from the snapshot taken when this object was 52 // This measures the diff from the snapshot taken when this object was
53 // constructed. 53 // constructed.
54 void ExpectTotalCount(const std::string& name, 54 void ExpectTotalCount(const std::string& name,
55 base::HistogramBase::Count count) const; 55 HistogramBase::Count count) const;
56 56
57 // We know exact number of samples for buckets corresponding to a time 57 // We know exact number of samples for buckets corresponding to a time
58 // interval. Other intervals may have samples too. 58 // interval. Other intervals may have samples too.
59 void ExpectTimeBucketCount(const std::string& name, 59 void ExpectTimeBucketCount(const std::string& name,
60 base::TimeDelta sample, 60 TimeDelta sample,
61 base::HistogramBase::Count count) const; 61 HistogramBase::Count count) const;
62 62
63 // Returns a list of all of the buckets recorded since creation of this 63 // Returns a list of all of the buckets recorded since creation of this
64 // object, as vector<Bucket>, where the Bucket represents the min boundary of 64 // object, as vector<Bucket>, where the Bucket represents the min boundary of
65 // the bucket and the count of samples recorded to that bucket since creation. 65 // the bucket and the count of samples recorded to that bucket since creation.
66 // 66 //
67 // Example usage, using gMock: 67 // Example usage, using gMock:
68 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), 68 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
69 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5))); 69 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
70 // 70 //
71 // If you build the expected list programmatically, you can use ContainerEq: 71 // If you build the expected list programmatically, you can use ContainerEq:
72 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), 72 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
73 // ContainerEq(expected_buckets)); 73 // ContainerEq(expected_buckets));
74 // 74 //
75 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a 75 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a
76 // slightly less helpful failure message: 76 // slightly less helpful failure message:
77 // EXPECT_EQ(expected_buckets, 77 // EXPECT_EQ(expected_buckets,
78 // histogram_tester.GetAllSamples("HistogramName")); 78 // histogram_tester.GetAllSamples("HistogramName"));
79 std::vector<Bucket> GetAllSamples(const std::string& name) const; 79 std::vector<Bucket> GetAllSamples(const std::string& name) const;
80 80
81 // Finds histograms whose names start with |query|, and returns them along 81 // Finds histograms whose names start with |prefix|, and returns them along
82 // with the counts of any samples added since the creation of this object. 82 // with the counts of any samples added since the creation of this object.
83 // Histograms that are unchanged are omitted from the result. The return value 83 // Histograms that are unchanged are omitted from the result. The return value
84 // is a map whose keys are the histogram name, and whose values are the sample 84 // is a map whose keys are the histogram name, and whose values are the sample
85 // count. 85 // count.
86 // 86 //
87 // This is useful for cases where the code under test is choosing among a 87 // This is useful for cases where the code under test is choosing among a
88 // family of related histograms and incrementing one of them. Typically you 88 // family of related histograms and incrementing one of them. Typically you
89 // should pass the result of this function directly to EXPECT_THAT. 89 // should pass the result of this function directly to EXPECT_THAT.
90 // 90 //
91 // Example usage, using gmock (which produces better failure messages): 91 // Example usage, using gmock (which produces better failure messages):
92 // #include "testing/gmock/include/gmock/gmock.h" 92 // #include "testing/gmock/include/gmock/gmock.h"
93 // ... 93 // ...
94 // base::HistogramTester::CountsMap expected_counts; 94 // base::HistogramTester::CountsMap expected_counts;
95 // expected_counts["MyMetric.A"] = 1; 95 // expected_counts["MyMetric.A"] = 1;
96 // expected_counts["MyMetric.B"] = 1; 96 // expected_counts["MyMetric.B"] = 1;
97 // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."), 97 // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."),
98 // testing::ContainerEq(expected_counts)); 98 // testing::ContainerEq(expected_counts));
99 CountsMap GetTotalCountsForPrefix(const std::string& query) const; 99 CountsMap GetTotalCountsForPrefix(const std::string& prefix) const;
100 100
101 // Access a modified HistogramSamples containing only what has been logged 101 // Access a modified HistogramSamples containing only what has been logged
102 // to the histogram since the creation of this object. 102 // to the histogram since the creation of this object.
103 std::unique_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( 103 std::unique_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
104 const std::string& histogram_name) const; 104 const std::string& histogram_name) const;
105 105
106 private: 106 private:
107 // Verifies and asserts that value in the |sample| bucket matches the 107 // Verifies and asserts that value in the |sample| bucket matches the
108 // |expected_count|. The bucket's current value is determined from |samples| 108 // |expected_count|. The bucket's current value is determined from |samples|
109 // and is modified based on the snapshot stored for histogram |name|. 109 // and is modified based on the snapshot stored for histogram |name|.
110 void CheckBucketCount(const std::string& name, 110 void CheckBucketCount(const std::string& name,
111 base::HistogramBase::Sample sample, 111 HistogramBase::Sample sample,
112 base::Histogram::Count expected_count, 112 Histogram::Count expected_count,
113 const base::HistogramSamples& samples) const; 113 const HistogramSamples& samples) const;
114 114
115 // Verifies that the total number of values recorded for the histogram |name| 115 // Verifies that the total number of values recorded for the histogram |name|
116 // is |expected_count|. This is checked against |samples| minus the snapshot 116 // is |expected_count|. This is checked against |samples| minus the snapshot
117 // that was taken for |name|. 117 // that was taken for |name|.
118 void CheckTotalCount(const std::string& name, 118 void CheckTotalCount(const std::string& name,
119 base::Histogram::Count expected_count, 119 Histogram::Count expected_count,
120 const base::HistogramSamples& samples) const; 120 const HistogramSamples& samples) const;
121 121
122 // Used to determine the histogram changes made during this instance's 122 // Used to determine the histogram changes made during this instance's
123 // lifecycle. 123 // lifecycle.
124 std::map<std::string, std::unique_ptr<HistogramSamples>> histograms_snapshot_; 124 std::map<std::string, std::unique_ptr<HistogramSamples>> histograms_snapshot_;
125 125
126 DISALLOW_COPY_AND_ASSIGN(HistogramTester); 126 DISALLOW_COPY_AND_ASSIGN(HistogramTester);
127 }; 127 };
128 128
129 struct Bucket { 129 struct Bucket {
130 Bucket(base::HistogramBase::Sample min, base::HistogramBase::Count count) 130 Bucket(HistogramBase::Sample min, HistogramBase::Count count)
131 : min(min), count(count) {} 131 : min(min), count(count) {}
132 132
133 bool operator==(const Bucket& other) const; 133 bool operator==(const Bucket& other) const;
134 134
135 base::HistogramBase::Sample min; 135 HistogramBase::Sample min;
136 base::HistogramBase::Count count; 136 HistogramBase::Count count;
137 }; 137 };
138 138
139 void PrintTo(const Bucket& value, std::ostream* os); 139 void PrintTo(const Bucket& value, std::ostream* os);
140 140
141 } // namespace base 141 } // namespace base
142 142
143 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ 143 #endif // BASE_TEST_HISTOGRAM_TESTER_H_
OLDNEW
« no previous file with comments | « no previous file | base/test/histogram_tester.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698