• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "base/metrics/histogram_snapshot_manager.h"
6 
7 #include <string>
8 #include <vector>
9 
10 #include "base/macros.h"
11 #include "base/metrics/histogram_delta_serialization.h"
12 #include "base/metrics/histogram_macros.h"
13 #include "base/metrics/sample_vector.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/stl_util.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 
18 namespace base {
19 
20 class HistogramFlattenerDeltaRecorder : public HistogramFlattener {
21  public:
22   HistogramFlattenerDeltaRecorder() = default;
23 
RecordDelta(const HistogramBase & histogram,const HistogramSamples & snapshot)24   void RecordDelta(const HistogramBase& histogram,
25                    const HistogramSamples& snapshot) override {
26     recorded_delta_histogram_names_.push_back(histogram.histogram_name());
27     // Use CHECK instead of ASSERT to get full stack-trace and thus origin.
28     CHECK(!ContainsKey(recorded_delta_histogram_sum_,
29                        histogram.histogram_name()));
30     // Keep pointer to snapshot for testing. This really isn't ideal but the
31     // snapshot-manager keeps the snapshot alive until it's "forgotten".
32     recorded_delta_histogram_sum_[histogram.histogram_name()] = snapshot.sum();
33   }
34 
Reset()35   void Reset() {
36     recorded_delta_histogram_names_.clear();
37     recorded_delta_histogram_sum_.clear();
38   }
39 
GetRecordedDeltaHistogramNames()40   std::vector<std::string> GetRecordedDeltaHistogramNames() {
41     return recorded_delta_histogram_names_;
42   }
43 
GetRecordedDeltaHistogramSum(const std::string & name)44   int64_t GetRecordedDeltaHistogramSum(const std::string& name) {
45     EXPECT_TRUE(ContainsKey(recorded_delta_histogram_sum_, name));
46     return recorded_delta_histogram_sum_[name];
47   }
48 
49  private:
50   std::vector<std::string> recorded_delta_histogram_names_;
51   std::map<std::string, int64_t> recorded_delta_histogram_sum_;
52 
53   DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder);
54 };
55 
56 class HistogramSnapshotManagerTest : public testing::Test {
57  protected:
HistogramSnapshotManagerTest()58   HistogramSnapshotManagerTest()
59       : statistics_recorder_(StatisticsRecorder::CreateTemporaryForTesting()),
60         histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {}
61 
62   ~HistogramSnapshotManagerTest() override = default;
63 
64   std::unique_ptr<StatisticsRecorder> statistics_recorder_;
65   HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_;
66   HistogramSnapshotManager histogram_snapshot_manager_;
67 };
68 
TEST_F(HistogramSnapshotManagerTest,PrepareDeltasNoFlagsFilter)69 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) {
70   // kNoFlags filter should record all histograms.
71   UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
72   UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);
73 
74   StatisticsRecorder::PrepareDeltas(false, HistogramBase::kNoFlags,
75                                     HistogramBase::kNoFlags,
76                                     &histogram_snapshot_manager_);
77 
78   const std::vector<std::string>& histograms =
79       histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
80   EXPECT_EQ(2U, histograms.size());
81   EXPECT_EQ("UmaHistogram", histograms[0]);
82   EXPECT_EQ("UmaStabilityHistogram", histograms[1]);
83 }
84 
TEST_F(HistogramSnapshotManagerTest,PrepareDeltasUmaHistogramFlagFilter)85 TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) {
86   // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag.
87   UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
88   UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);
89 
90   StatisticsRecorder::PrepareDeltas(false, HistogramBase::kNoFlags,
91                                     HistogramBase::kUmaTargetedHistogramFlag,
92                                     &histogram_snapshot_manager_);
93 
94   const std::vector<std::string>& histograms =
95       histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
96   EXPECT_EQ(2U, histograms.size());
97   EXPECT_EQ("UmaHistogram", histograms[0]);
98   EXPECT_EQ("UmaStabilityHistogram", histograms[1]);
99 }
100 
TEST_F(HistogramSnapshotManagerTest,PrepareDeltasUmaStabilityHistogramFlagFilter)101 TEST_F(HistogramSnapshotManagerTest,
102        PrepareDeltasUmaStabilityHistogramFlagFilter) {
103   UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
104   UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);
105 
106   StatisticsRecorder::PrepareDeltas(false, HistogramBase::kNoFlags,
107                                     HistogramBase::kUmaStabilityHistogramFlag,
108                                     &histogram_snapshot_manager_);
109 
110   const std::vector<std::string>& histograms =
111       histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
112   EXPECT_EQ(1U, histograms.size());
113   EXPECT_EQ("UmaStabilityHistogram", histograms[0]);
114 }
115 
116 }  // namespace base
117