summaryrefslogtreecommitdiffstats
path: root/chromium/base/metrics/histogram_snapshot_manager_unittest.cc
blob: 8ec03daa8d97c9579f97dc949c18bfe59c02eb2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/metrics/histogram_snapshot_manager.h"

#include <string>
#include <vector>

#include "base/macros.h"
#include "base/metrics/histogram_delta_serialization.h"
#include "base/metrics/histogram_macros.h"
#include "base/metrics/sample_vector.h"
#include "base/metrics/statistics_recorder.h"
#include "base/stl_util.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace base {

class HistogramFlattenerDeltaRecorder : public HistogramFlattener {
 public:
  HistogramFlattenerDeltaRecorder() {}

  void RecordDelta(const HistogramBase& histogram,
                   const HistogramSamples& snapshot) override {
    recorded_delta_histogram_names_.push_back(histogram.histogram_name());
    ASSERT_FALSE(ContainsKey(recorded_delta_histogram_sum_,
                             histogram.histogram_name()));
    // Keep pointer to snapshot for testing. This really isn't ideal but the
    // snapshot-manager keeps the snapshot alive until it's "forgotten".
    recorded_delta_histogram_sum_[histogram.histogram_name()] = snapshot.sum();
  }

  void InconsistencyDetected(HistogramBase::Inconsistency problem) override {
    ASSERT_TRUE(false);
  }

  void UniqueInconsistencyDetected(
      HistogramBase::Inconsistency problem) override {
    ASSERT_TRUE(false);
  }

  void InconsistencyDetectedInLoggedCount(int amount) override {
    ASSERT_TRUE(false);
  }

  void Reset() {
    recorded_delta_histogram_names_.clear();
    recorded_delta_histogram_sum_.clear();
  }

  std::vector<std::string> GetRecordedDeltaHistogramNames() {
    return recorded_delta_histogram_names_;
  }

  int64_t GetRecordedDeltaHistogramSum(const std::string& name) {
    EXPECT_TRUE(ContainsKey(recorded_delta_histogram_sum_, name));
    return recorded_delta_histogram_sum_[name];
  }

 private:
  std::vector<std::string> recorded_delta_histogram_names_;
  std::map<std::string, int64_t> recorded_delta_histogram_sum_;

  DISALLOW_COPY_AND_ASSIGN(HistogramFlattenerDeltaRecorder);
};

class HistogramSnapshotManagerTest : public testing::Test {
 protected:
  HistogramSnapshotManagerTest()
      : histogram_snapshot_manager_(&histogram_flattener_delta_recorder_) {}

  ~HistogramSnapshotManagerTest() override {}

  StatisticsRecorder statistics_recorder_;
  HistogramFlattenerDeltaRecorder histogram_flattener_delta_recorder_;
  HistogramSnapshotManager histogram_snapshot_manager_;
};

TEST_F(HistogramSnapshotManagerTest, PrepareDeltasNoFlagsFilter) {
  // kNoFlags filter should record all histograms.
  UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
  UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);

  histogram_snapshot_manager_.PrepareDeltas(
      StatisticsRecorder::begin(false), StatisticsRecorder::end(),
      HistogramBase::kNoFlags, HistogramBase::kNoFlags);

  const std::vector<std::string>& histograms =
      histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
  EXPECT_EQ(2U, histograms.size());
  EXPECT_EQ("UmaHistogram", histograms[0]);
  EXPECT_EQ("UmaStabilityHistogram", histograms[1]);
}

TEST_F(HistogramSnapshotManagerTest, PrepareDeltasUmaHistogramFlagFilter) {
  // Note that kUmaStabilityHistogramFlag includes kUmaTargetedHistogramFlag.
  UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
  UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);

  histogram_snapshot_manager_.PrepareDeltas(
      StatisticsRecorder::begin(false), StatisticsRecorder::end(),
      HistogramBase::kNoFlags, HistogramBase::kUmaTargetedHistogramFlag);

  const std::vector<std::string>& histograms =
      histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
  EXPECT_EQ(2U, histograms.size());
  EXPECT_EQ("UmaHistogram", histograms[0]);
  EXPECT_EQ("UmaStabilityHistogram", histograms[1]);
}

TEST_F(HistogramSnapshotManagerTest,
       PrepareDeltasUmaStabilityHistogramFlagFilter) {
  UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
  UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);

  histogram_snapshot_manager_.PrepareDeltas(
      StatisticsRecorder::begin(false), StatisticsRecorder::end(),
      HistogramBase::kNoFlags, HistogramBase::kUmaStabilityHistogramFlag);

  const std::vector<std::string>& histograms =
      histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
  EXPECT_EQ(1U, histograms.size());
  EXPECT_EQ("UmaStabilityHistogram", histograms[0]);
}

TEST_F(HistogramSnapshotManagerTest, CheckMerge) {
  UMA_HISTOGRAM_ENUMERATION("UmaHistogram", 1, 4);
  UMA_STABILITY_HISTOGRAM_ENUMERATION("UmaStabilityHistogram", 1, 2);

  base::HistogramBase* h1 = base::LinearHistogram::FactoryGet(
      "UmaHistogram", 1, 4, 5, 0);
  ASSERT_TRUE(h1);
  base::HistogramBase* h2 = base::LinearHistogram::FactoryGet(
      "UmaStabilityHistogram", 1, 2, 3, 0);
  ASSERT_TRUE(h2);

  histogram_snapshot_manager_.StartDeltas();
  histogram_snapshot_manager_.PrepareDelta(h1);
  histogram_snapshot_manager_.PrepareDelta(h1);  // Delta will be zero.
  histogram_snapshot_manager_.PrepareDelta(h2);
  h1->Add(2);
  h2->Add(1);
  histogram_snapshot_manager_.PrepareDelta(h2);
  histogram_snapshot_manager_.PrepareDelta(h1);
  histogram_snapshot_manager_.FinishDeltas();
  {
    const std::vector<std::string> histograms =
        histogram_flattener_delta_recorder_.GetRecordedDeltaHistogramNames();
    EXPECT_EQ(2U, histograms.size());
    EXPECT_EQ(3, histogram_flattener_delta_recorder_.
                     GetRecordedDeltaHistogramSum("UmaHistogram"));
    EXPECT_EQ(2, histogram_flattener_delta_recorder_.
                     GetRecordedDeltaHistogramSum("UmaStabilityHistogram"));
  }
}

}  // namespace base