Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / base / test / histogram_tester.h
blob7ac7ca67e7efac0db0f4d8fe1921b719c77aedef
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 // The constructor will call StatisticsRecorder::Initialize() for you. Also,
30 // this takes a snapshot of all current histograms counts.
31 HistogramTester();
32 ~HistogramTester();
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;
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;
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;
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.
58 // Example usage, using gMock:
59 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
60 // ElementsAre(Bucket(1, 5), Bucket(2, 10), Bucket(3, 5)));
62 // If you build the expected list programmatically, you can use ContainerEq:
63 // EXPECT_THAT(histogram_tester.GetAllSamples("HistogramName"),
64 // ContainerEq(expected_buckets));
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);
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);
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;
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;
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_;
98 DISALLOW_COPY_AND_ASSIGN(HistogramTester);
101 struct Bucket {
102 Bucket(base::HistogramBase::Sample min, base::HistogramBase::Count count)
103 : min(min), count(count) {}
105 bool operator==(const Bucket& other) const;
107 base::HistogramBase::Sample min;
108 base::HistogramBase::Count count;
111 void PrintTo(const Bucket& value, std::ostream* os);
113 } // namespace base
115 #endif // BASE_TEST_HISTOGRAM_TESTER_H_