blob: b31c63153f8c5125acc3ea3e3b82efed919d9c93 [file] [log] [blame]
mlerman@chromium.orga7980682014-08-22 07:00:38 +09001// 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>
dchengcc8e4d82016-04-05 06:25:51 +09009#include <memory>
twifkak120eb322015-07-01 04:13:21 +090010#include <ostream>
mlerman@chromium.orga7980682014-08-22 07:00:38 +090011#include <string>
twifkak120eb322015-07-01 04:13:21 +090012#include <utility>
13#include <vector>
mlerman@chromium.orga7980682014-08-22 07:00:38 +090014
avif09d5392015-12-24 12:28:02 +090015#include "base/macros.h"
mlerman@chromium.orga7980682014-08-22 07:00:38 +090016#include "base/metrics/histogram.h"
17#include "base/metrics/histogram_base.h"
nikunjb7961de92016-11-15 07:06:25 +090018#include "base/time/time.h"
mlerman@chromium.orga7980682014-08-22 07:00:38 +090019
20namespace base {
21
twifkak120eb322015-07-01 04:13:21 +090022struct Bucket;
mlerman@chromium.orga7980682014-08-22 07:00:38 +090023class HistogramSamples;
24
25// HistogramTester provides a simple interface for examining histograms, UMA
26// or otherwise. Tests can use this interface to verify that histogram data is
27// getting logged as intended.
28class HistogramTester {
29 public:
pkalinnikov5b63bda2017-01-14 03:30:38 +090030 using CountsMap = std::map<std::string, HistogramBase::Count>;
nickb1c693a2015-07-28 03:29:59 +090031
François Degros0dd05ce2018-01-08 11:53:34 +090032 // Takes a snapshot of all current histograms counts.
mlerman@chromium.orga7980682014-08-22 07:00:38 +090033 HistogramTester();
34 ~HistogramTester();
35
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,
pkalinnikov5b63bda2017-01-14 03:30:38 +090040 HistogramBase::Sample sample,
41 HistogramBase::Count expected_count) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +090042
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,
pkalinnikov5b63bda2017-01-14 03:30:38 +090047 HistogramBase::Sample sample,
48 HistogramBase::Count expected_count) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +090049
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,
pkalinnikov5b63bda2017-01-14 03:30:38 +090054 HistogramBase::Count count) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +090055
nikunjb7961de92016-11-15 07:06:25 +090056 // We know exact number of samples for buckets corresponding to a time
57 // interval. Other intervals may have samples too.
58 void ExpectTimeBucketCount(const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +090059 TimeDelta sample,
60 HistogramBase::Count count) const;
nikunjb7961de92016-11-15 07:06:25 +090061
twifkak120eb322015-07-01 04:13:21 +090062 // Returns a list of all of the buckets recorded since creation of this
63 // object, as vector<Bucket>, where the Bucket represents the min boundary of
64 // the bucket and the count of samples recorded to that bucket since creation.
65 //
66 // Example usage, using gMock:
67 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
68 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
69 //
70 // If you build the expected list programmatically, you can use ContainerEq:
71 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
72 // ContainerEq(expected_buckets));
73 //
74 // or EXPECT_EQ if you prefer not to depend on gMock, at the expense of a
75 // slightly less helpful failure message:
76 // EXPECT_EQ(expected_buckets,
77 // histogram_tester.GetAllSamples("HistogramName"));
nickb1c693a2015-07-28 03:29:59 +090078 std::vector<Bucket> GetAllSamples(const std::string& name) const;
79
lunalu1f210752017-05-20 13:25:28 +090080 // Returns the value of the |sample| bucket for ths histogram |name|.
81 HistogramBase::Count GetBucketCount(const std::string& name,
82 HistogramBase::Sample sample) const;
83
pkalinnikov5b63bda2017-01-14 03:30:38 +090084 // Finds histograms whose names start with |prefix|, and returns them along
nickb1c693a2015-07-28 03:29:59 +090085 // with the counts of any samples added since the creation of this object.
86 // Histograms that are unchanged are omitted from the result. The return value
87 // is a map whose keys are the histogram name, and whose values are the sample
88 // count.
89 //
90 // This is useful for cases where the code under test is choosing among a
91 // family of related histograms and incrementing one of them. Typically you
92 // should pass the result of this function directly to EXPECT_THAT.
93 //
94 // Example usage, using gmock (which produces better failure messages):
95 // #include "testing/gmock/include/gmock/gmock.h"
96 // ...
97 // base::HistogramTester::CountsMap expected_counts;
98 // expected_counts["MyMetric.A"] = 1;
99 // expected_counts["MyMetric.B"] = 1;
100 // EXPECT_THAT(histogram_tester.GetTotalCountsForPrefix("MyMetric."),
101 // testing::ContainerEq(expected_counts));
pkalinnikov5b63bda2017-01-14 03:30:38 +0900102 CountsMap GetTotalCountsForPrefix(const std::string& prefix) const;
twifkak120eb322015-07-01 04:13:21 +0900103
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900104 // Access a modified HistogramSamples containing only what has been logged
105 // to the histogram since the creation of this object.
dchengcc8e4d82016-04-05 06:25:51 +0900106 std::unique_ptr<HistogramSamples> GetHistogramSamplesSinceCreation(
nickb1c693a2015-07-28 03:29:59 +0900107 const std::string& histogram_name) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900108
109 private:
110 // Verifies and asserts that value in the |sample| bucket matches the
111 // |expected_count|. The bucket's current value is determined from |samples|
112 // and is modified based on the snapshot stored for histogram |name|.
113 void CheckBucketCount(const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +0900114 HistogramBase::Sample sample,
115 Histogram::Count expected_count,
116 const HistogramSamples& samples) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900117
118 // Verifies that the total number of values recorded for the histogram |name|
119 // is |expected_count|. This is checked against |samples| minus the snapshot
120 // that was taken for |name|.
121 void CheckTotalCount(const std::string& name,
pkalinnikov5b63bda2017-01-14 03:30:38 +0900122 Histogram::Count expected_count,
123 const HistogramSamples& samples) const;
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900124
lunalu1f210752017-05-20 13:25:28 +0900125 // Sets the value for |count| to be the value in the |sample| bucket. The
126 // bucket's current value is determined from |samples| and is modified based
127 // on the snapshot stored for histogram |name|.
128 void GetBucketCountForSamples(const std::string& name,
129 HistogramBase::Sample sample,
130 const HistogramSamples& samples,
131 HistogramBase::Count* count) const;
132
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900133 // Used to determine the histogram changes made during this instance's
aviace57a82016-10-27 13:10:51 +0900134 // lifecycle.
135 std::map<std::string, std::unique_ptr<HistogramSamples>> histograms_snapshot_;
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900136
137 DISALLOW_COPY_AND_ASSIGN(HistogramTester);
138};
139
twifkak120eb322015-07-01 04:13:21 +0900140struct Bucket {
pkalinnikov5b63bda2017-01-14 03:30:38 +0900141 Bucket(HistogramBase::Sample min, HistogramBase::Count count)
twifkak120eb322015-07-01 04:13:21 +0900142 : min(min), count(count) {}
143
144 bool operator==(const Bucket& other) const;
145
pkalinnikov5b63bda2017-01-14 03:30:38 +0900146 HistogramBase::Sample min;
147 HistogramBase::Count count;
twifkak120eb322015-07-01 04:13:21 +0900148};
149
150void PrintTo(const Bucket& value, std::ostream* os);
151
mlerman@chromium.orga7980682014-08-22 07:00:38 +0900152} // namespace base
153
154#endif // BASE_TEST_HISTOGRAM_TESTER_H_