OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/metrics/sparse_histogram.h" | 5 #include "base/metrics/sparse_histogram.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/metrics/histogram_base.h" | 10 #include "base/metrics/histogram_base.h" |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 | 142 |
143 histogram->AddCount(1000000000, 15); | 143 histogram->AddCount(1000000000, 15); |
144 histogram->AddCount(1010000000, 25); | 144 histogram->AddCount(1010000000, 25); |
145 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); | 145 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); |
146 EXPECT_EQ(55, snapshot2->TotalCount()); | 146 EXPECT_EQ(55, snapshot2->TotalCount()); |
147 EXPECT_EQ(30, snapshot2->GetCount(1000000000)); | 147 EXPECT_EQ(30, snapshot2->GetCount(1000000000)); |
148 EXPECT_EQ(25, snapshot2->GetCount(1010000000)); | 148 EXPECT_EQ(25, snapshot2->GetCount(1010000000)); |
149 EXPECT_EQ(55250000000LL, snapshot2->sum()); | 149 EXPECT_EQ(55250000000LL, snapshot2->sum()); |
150 } | 150 } |
151 | 151 |
| 152 // Make sure that counts returned by Histogram::SnapshotDelta do not overflow |
| 153 // even when a total count (returned by Histogram::SnapshotSample) does. |
| 154 TEST_P(SparseHistogramTest, AddCount_LargeCountsDontOverflow) { |
| 155 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse")); |
| 156 std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples()); |
| 157 EXPECT_EQ(0, snapshot->TotalCount()); |
| 158 EXPECT_EQ(0, snapshot->sum()); |
| 159 |
| 160 const int count = (1 << 30) - 1; |
| 161 |
| 162 // Repeat N times to make sure that there is no internal value overflow. |
| 163 for (int i = 0; i < 10; ++i) { |
| 164 histogram->AddCount(42, count); |
| 165 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotDelta(); |
| 166 EXPECT_EQ(count, samples->TotalCount()); |
| 167 EXPECT_EQ(count, samples->GetCount(42)); |
| 168 } |
| 169 } |
| 170 |
152 TEST_P(SparseHistogramTest, MacroBasicTest) { | 171 TEST_P(SparseHistogramTest, MacroBasicTest) { |
153 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); | 172 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); |
154 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); | 173 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); |
155 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); | 174 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); |
156 | 175 |
157 StatisticsRecorder::Histograms histograms; | 176 StatisticsRecorder::Histograms histograms; |
158 StatisticsRecorder::GetHistograms(&histograms); | 177 StatisticsRecorder::GetHistograms(&histograms); |
159 | 178 |
160 ASSERT_EQ(1U, histograms.size()); | 179 ASSERT_EQ(1U, histograms.size()); |
161 HistogramBase* sparse_histogram = histograms[0]; | 180 HistogramBase* sparse_histogram = histograms[0]; |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 EXPECT_EQ(1, count); | 377 EXPECT_EQ(1, count); |
359 EXPECT_EQ(cases[i].sample, min); | 378 EXPECT_EQ(cases[i].sample, min); |
360 EXPECT_EQ(cases[i].expected_max, max); | 379 EXPECT_EQ(cases[i].expected_max, max); |
361 | 380 |
362 it->Next(); | 381 it->Next(); |
363 EXPECT_TRUE(it->Done()); | 382 EXPECT_TRUE(it->Done()); |
364 } | 383 } |
365 } | 384 } |
366 | 385 |
367 } // namespace base | 386 } // namespace base |
OLD | NEW |