Chromium Code Reviews| 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 <ostream> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 60 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5))); | 60 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5))); |
| 61 // | 61 // |
| 62 // If you build the expected list programmatically, you can use ContainerEq: | 62 // If you build the expected list programmatically, you can use ContainerEq: |
| 63 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), | 63 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"), |
| 64 // ContainerEq(expected_buckets)); | 64 // ContainerEq(expected_buckets)); |
| 65 // | 65 // |
| 66 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a | 66 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a |
| 67 // slightly less helpful failure message: | 67 // slightly less helpful failure message: |
| 68 // EXPECT_EQ(expected_buckets, | 68 // EXPECT_EQ(expected_buckets, |
| 69 // histogram_tester.GetAllSamples("HistogramName")); | 69 // histogram_tester.GetAllSamples("HistogramName")); |
| 70 std::vector<Bucket> GetAllSamples(const std::string& name); | 70 std::vector<Bucket> GetAllSamples(const std::string& name) const; |
|
twifkak
2015/07/23 21:21:54
Oops, good catch.
ncarter (slow)
2015/07/23 21:50:43
Acknowledged.
| |
| 71 | |
| 72 // Finds histograms whose names start with |query|, and returns them along | |
| 73 // with the counts of any samples added since the creation of this object. | |
| 74 // Histograms that are unchanged are omitted from the result. The return value | |
| 75 // is a map whose keys are the histogram name, and whose values are the sample | |
| 76 // count. | |
| 77 // | |
| 78 // This is useful for cases where the code under test is choosing among a | |
| 79 // family of related histograms and incrementing one of them. Typically you | |
| 80 // should pass the result of this function directly to EXPECT_THAT. | |
| 81 // | |
| 82 // Example usage, using gmock (which produces better failure messages): | |
| 83 // #include "testing/gmock/include/gmock/gmock-matchers.h" | |
|
twifkak
2015/07/23 21:21:54
The documentation at https://wiki.corp.google.com/
ncarter (slow)
2015/07/23 21:50:43
Done.
| |
| 84 // ... | |
| 85 // base::HistogramTester::CountsMap expected_counts; | |
| 86 // expected_counts["MyMetric.A"] = 1; | |
| 87 // expected_counts["MyMetric.B"] = 1; | |
| 88 // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."), | |
| 89 // testing::ContainerEq(expected_counts)); | |
| 90 using CountsMap = std::map<std::string, base::HistogramBase::Count>; | |
|
twifkak
2015/07/23 21:21:54
Using decls aren't explicitly called out in the st
ncarter (slow)
2015/07/23 21:50:43
Yeah, I'm torn about this generally. I'm usually a
| |
| 91 CountsMap GetTotalCountsForPrefix(const std::string& query) const; | |
| 71 | 92 |
| 72 // Access a modified HistogramSamples containing only what has been logged | 93 // Access a modified HistogramSamples containing only what has been logged |
| 73 // to the histogram since the creation of this object. | 94 // to the histogram since the creation of this object. |
| 74 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( | 95 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( |
| 75 const std::string& histogram_name); | 96 const std::string& histogram_name) const; |
| 76 | 97 |
| 77 private: | 98 private: |
| 78 // Verifies and asserts that value in the |sample| bucket matches the | 99 // Verifies and asserts that value in the |sample| bucket matches the |
| 79 // |expected_count|. The bucket's current value is determined from |samples| | 100 // |expected_count|. The bucket's current value is determined from |samples| |
| 80 // and is modified based on the snapshot stored for histogram |name|. | 101 // and is modified based on the snapshot stored for histogram |name|. |
| 81 void CheckBucketCount(const std::string& name, | 102 void CheckBucketCount(const std::string& name, |
| 82 base::HistogramBase::Sample sample, | 103 base::HistogramBase::Sample sample, |
| 83 base::Histogram::Count expected_count, | 104 base::Histogram::Count expected_count, |
| 84 const base::HistogramSamples& samples) const; | 105 const base::HistogramSamples& samples) const; |
| 85 | 106 |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 106 | 127 |
| 107 base::HistogramBase::Sample min; | 128 base::HistogramBase::Sample min; |
| 108 base::HistogramBase::Count count; | 129 base::HistogramBase::Count count; |
| 109 }; | 130 }; |
| 110 | 131 |
| 111 void PrintTo(const Bucket& value, std::ostream* os); | 132 void PrintTo(const Bucket& value, std::ostream* os); |
| 112 | 133 |
| 113 } // namespace base | 134 } // namespace base |
| 114 | 135 |
| 115 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ | 136 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ |
| OLD | NEW |