Update V8 to version 4.7.21.
[chromium-blink-merge.git] / base / test / histogram_tester.h
blobcfab3c60f24e267c9b6e0a08dcde3421764201f8
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.
5 #ifndef BASE_TEST_HISTOGRAM_TESTER_H_
6 #define BASE_TEST_HISTOGRAM_TESTER_H_
8 #include <map>
9 #include <ostream>
10 #include <string>
11 #include <utility>
12 #include <vector>
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"
19 namespace base {
21 struct Bucket;
22 class HistogramSamples;
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 using CountsMap = std::map<std::string, base::HistogramBase::Count>;
31 // The constructor will call StatisticsRecorder::Initialize() for you. Also,
32 // this takes a snapshot of all current histograms counts.
33 HistogramTester();
34 ~HistogramTester();
36 // We know the exact number of samples in a bucket, and that no other bucket
37 // should have samples. Measures the diff from the snapshot taken when this
38 // object was constructed.
39 void ExpectUniqueSample(const std::string& name,
40 base::HistogramBase::Sample sample,
41 base::HistogramBase::Count expected_count) const;
43 // We know the exact number of samples in a bucket, but other buckets may
44 // have samples as well. Measures the diff from the snapshot taken when this
45 // object was constructed.
46 void ExpectBucketCount(const std::string& name,
47 base::HistogramBase::Sample sample,
48 base::HistogramBase::Count expected_count) const;
50 // We don't know the values of the samples, but we know how many there are.
51 // This measures the diff from the snapshot taken when this object was
52 // constructed.
53 void ExpectTotalCount(const std::string& name,
54 base::HistogramBase::Count count) const;
56 // Returns a list of all of the buckets recorded since creation of this
57 // object, as vector<Bucket>, where the Bucket represents the min boundary of
58 // the bucket and the count of samples recorded to that bucket since creation.
60 // Example usage, using gMock:
61 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
62 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
64 // If you build the expected list programmatically, you can use ContainerEq:
65 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
66 // ContainerEq(expected_buckets));
68 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a
69 // slightly less helpful failure message:
70 // EXPECT_EQ(expected_buckets,
71 // histogram_tester.GetAllSamples("HistogramName"));
72 std::vector<Bucket> GetAllSamples(const std::string& name) const;
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.
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.
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;
94 // Access a modified HistogramSamples containing only what has been logged
95 // to the histogram since the creation of this object.
96 scoped_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
97 const std::string& histogram_name) const;
99 private:
100 // Verifies and asserts that value in the |sample| bucket matches the
101 // |expected_count|. The bucket's current value is determined from |samples|
102 // and is modified based on the snapshot stored for histogram |name|.
103 void CheckBucketCount(const std::string& name,
104 base::HistogramBase::Sample sample,
105 base::Histogram::Count expected_count,
106 const base::HistogramSamples& samples) const;
108 // Verifies that the total number of values recorded for the histogram |name|
109 // is |expected_count|. This is checked against |samples| minus the snapshot
110 // that was taken for |name|.
111 void CheckTotalCount(const std::string& name,
112 base::Histogram::Count expected_count,
113 const base::HistogramSamples& samples) const;
115 // Used to determine the histogram changes made during this instance's
116 // lifecycle. This instance takes ownership of the samples, which are deleted
117 // when the instance is destroyed.
118 std::map<std::string, HistogramSamples*> histograms_snapshot_;
120 DISALLOW_COPY_AND_ASSIGN(HistogramTester);
123 struct Bucket {
124 Bucket(base::HistogramBase::Sample min, base::HistogramBase::Count count)
125 : min(min), count(count) {}
127 bool operator==(const Bucket& other) const;
129 base::HistogramBase::Sample min;
130 base::HistogramBase::Count count;
133 void PrintTo(const Bucket& value, std::ostream* os);
135 } // namespace base
137 #endif // BASE_TEST_HISTOGRAM_TESTER_H_