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

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

Issue 1190423006: base/test/histogram_tester.h: Add a way to query a whole family of related (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes from twifkak Created 5 years, 5 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 <ostream> 9 #include <ostream>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.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 18
19 namespace base { 19 namespace base {
20 20
21 struct Bucket; 21 struct Bucket;
22 class HistogramSamples; 22 class HistogramSamples;
23 23
24 // HistogramTester provides a simple interface for examining histograms, UMA 24 // HistogramTester provides a simple interface for examining histograms, UMA
25 // 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
26 // getting logged as intended. 26 // getting logged as intended.
27 class HistogramTester { 27 class HistogramTester {
28 public: 28 public:
29 using CountsMap = std::map<std::string, base::HistogramBase::Count>;
30
29 // The constructor will call StatisticsRecorder::Initialize() for you. Also, 31 // The constructor will call StatisticsRecorder::Initialize() for you. Also,
30 // this takes a snapshot of all current histograms counts. 32 // this takes a snapshot of all current histograms counts.
31 HistogramTester(); 33 HistogramTester();
32 ~HistogramTester(); 34 ~HistogramTester();
33 35
34 // We know the exact number of samples in a bucket, and that no other bucket 36 // We know the exact number of samples in a bucket, and that no other bucket
35 // should have samples. Measures the diff from the snapshot taken when this 37 // should have samples. Measures the diff from the snapshot taken when this
36 // object was constructed. 38 // object was constructed.
37 void ExpectUniqueSample(const std::string& name, 39 void ExpectUniqueSample(const std::string& name,
38 base::HistogramBase::Sample sample, 40 base::HistogramBase::Sample sample,
(...skipping 21 matching lines...) Expand all
60 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5))); 62 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
61 // 63 //
62 // If you build the expected list programmatically, you can use ContainerEq: 64 // If you build the expected list programmatically, you can use ContainerEq:
63 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), 65 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
64 // ContainerEq(expected_buckets)); 66 // ContainerEq(expected_buckets));
65 // 67 //
66 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a 68 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a
67 // slightly less helpful failure message: 69 // slightly less helpful failure message:
68 // EXPECT_EQ(expected_buckets, 70 // EXPECT_EQ(expected_buckets,
69 // histogram_tester.GetAllSamples("HistogramName")); 71 // histogram_tester.GetAllSamples("HistogramName"));
70 std::vector<Bucket> GetAllSamples(const std::string& name); 72 std::vector<Bucket> GetAllSamples(const std::string& name) const;
73
74 // Finds histograms whose names start with |query|, and returns them along
75 // with the counts of any samples added since the creation of this object.
76 // Histograms that are unchanged are omitted from the result. The return value
77 // is a map whose keys are the histogram name, and whose values are the sample
78 // count.
79 //
80 // This is useful for cases where the code under test is choosing among a
81 // family of related histograms and incrementing one of them. Typically you
82 // should pass the result of this function directly to EXPECT_THAT.
83 //
84 // Example usage, using gmock (which produces better failure messages):
85 // #include "testing/gmock/include/gmock/gmock.h"
86 // ...
87 // base::HistogramTester::CountsMap expected_counts;
88 // expected_counts["MyMetric.A"] = 1;
89 // expected_counts["MyMetric.B"] = 1;
90 // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."),
91 // testing::ContainerEq(expected_counts));
92 CountsMap GetTotalCountsForPrefix(const std::string& query) const;
71 93
72 // Access a modified HistogramSamples containing only what has been logged 94 // Access a modified HistogramSamples containing only what has been logged
73 // to the histogram since the creation of this object. 95 // to the histogram since the creation of this object.
74 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( 96 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
75 const std::string& histogram_name); 97 const std::string& histogram_name) const;
76 98
77 private: 99 private:
78 // Verifies and asserts that value in the |sample| bucket matches the 100 // Verifies and asserts that value in the |sample| bucket matches the
79 // |expected_count|. The bucket's current value is determined from |samples| 101 // |expected_count|. The bucket's current value is determined from |samples|
80 // and is modified based on the snapshot stored for histogram |name|. 102 // and is modified based on the snapshot stored for histogram |name|.
81 void CheckBucketCount(const std::string& name, 103 void CheckBucketCount(const std::string& name,
82 base::HistogramBase::Sample sample, 104 base::HistogramBase::Sample sample,
83 base::Histogram::Count expected_count, 105 base::Histogram::Count expected_count,
84 const base::HistogramSamples& samples) const; 106 const base::HistogramSamples& samples) const;
85 107
(...skipping 20 matching lines...) Expand all
106 128
107 base::HistogramBase::Sample min; 129 base::HistogramBase::Sample min;
108 base::HistogramBase::Count count; 130 base::HistogramBase::Count count;
109 }; 131 };
110 132
111 void PrintTo(const Bucket& value, std::ostream* os); 133 void PrintTo(const Bucket& value, std::ostream* os);
112 134
113 } // namespace base 135 } // namespace base
114 136
115 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ 137 #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