| OLD | NEW |
| (Empty) |
| 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 | |
| 5 #ifndef BASE_TEST_HISTOGRAM_TESTER_H_ | |
| 6 #define BASE_TEST_HISTOGRAM_TESTER_H_ | |
| 7 | |
| 8 #include <map> | |
| 9 #include <ostream> | |
| 10 #include <string> | |
| 11 #include <utility> | |
| 12 #include <vector> | |
| 13 | |
| 14 #include "base/basictypes.h" | |
| 15 #include "base/memory/scoped_ptr.h" | |
| 16 #include "base/metrics/histogram.h" | |
| 17 #include "base/metrics/histogram_base.h" | |
| 18 | |
| 19 namespace base { | |
| 20 | |
| 21 struct Bucket; | |
| 22 class HistogramSamples; | |
| 23 | |
| 24 // HistogramTester provides a simple interface for examining histograms, UMA | |
| 25 // or otherwise. Tests can use this interface to verify that histogram data is | |
| 26 // getting logged as intended. | |
| 27 class HistogramTester { | |
| 28 public: | |
| 29 // The constructor will call StatisticsRecorder::Initialize() for you. Also, | |
| 30 // this takes a snapshot of all current histograms counts. | |
| 31 HistogramTester(); | |
| 32 ~HistogramTester(); | |
| 33 | |
| 34 // 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 | |
| 36 // object was constructed. | |
| 37 void ExpectUniqueSample(const std::string& name, | |
| 38 base::HistogramBase::Sample sample, | |
| 39 base::HistogramBase::Count expected_count) const; | |
| 40 | |
| 41 // We know the exact number of samples in a bucket, but other buckets may | |
| 42 // have samples as well. Measures the diff from the snapshot taken when this | |
| 43 // object was constructed. | |
| 44 void ExpectBucketCount(const std::string& name, | |
| 45 base::HistogramBase::Sample sample, | |
| 46 base::HistogramBase::Count expected_count) const; | |
| 47 | |
| 48 // We don't know the values of the samples, but we know how many there are. | |
| 49 // This measures the diff from the snapshot taken when this object was | |
| 50 // constructed. | |
| 51 void ExpectTotalCount(const std::string& name, | |
| 52 base::HistogramBase::Count count) const; | |
| 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 | |
| 72 // Access a modified HistogramSamples containing only what has been logged | |
| 73 // to the histogram since the creation of this object. | |
| 74 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation( | |
| 75 const std::string& histogram_name); | |
| 76 | |
| 77 private: | |
| 78 // Verifies and asserts that value in the |sample| bucket matches the | |
| 79 // |expected_count|. The bucket's current value is determined from |samples| | |
| 80 // and is modified based on the snapshot stored for histogram |name|. | |
| 81 void CheckBucketCount(const std::string& name, | |
| 82 base::HistogramBase::Sample sample, | |
| 83 base::Histogram::Count expected_count, | |
| 84 const base::HistogramSamples& samples) const; | |
| 85 | |
| 86 // Verifies that the total number of values recorded for the histogram |name| | |
| 87 // is |expected_count|. This is checked against |samples| minus the snapshot | |
| 88 // that was taken for |name|. | |
| 89 void CheckTotalCount(const std::string& name, | |
| 90 base::Histogram::Count expected_count, | |
| 91 const base::HistogramSamples& samples) const; | |
| 92 | |
| 93 // Used to determine the histogram changes made during this instance's | |
| 94 // lifecycle. This instance takes ownership of the samples, which are deleted | |
| 95 // when the instance is destroyed. | |
| 96 std::map<std::string, HistogramSamples*> histograms_snapshot_; | |
| 97 | |
| 98 DISALLOW_COPY_AND_ASSIGN(HistogramTester); | |
| 99 }; | |
| 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 | |
| 113 } // namespace base | |
| 114 | |
| 115 #endif // BASE_TEST_HISTOGRAM_TESTER_H_ | |
| OLD | NEW |