Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: base/metrics/sparse_histogram_unittest.cc

Issue 2867303004: [histogram] Make histograms more resistant to overflows. (Closed)
Patch Set: final final fix Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/metrics/sparse_histogram.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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"
11 #include "base/metrics/histogram_macros.h" 11 #include "base/metrics/histogram_macros.h"
12 #include "base/metrics/histogram_samples.h" 12 #include "base/metrics/histogram_samples.h"
13 #include "base/metrics/metrics_hashes.h"
13 #include "base/metrics/persistent_histogram_allocator.h" 14 #include "base/metrics/persistent_histogram_allocator.h"
14 #include "base/metrics/persistent_memory_allocator.h" 15 #include "base/metrics/persistent_memory_allocator.h"
15 #include "base/metrics/sample_map.h" 16 #include "base/metrics/sample_map.h"
16 #include "base/metrics/statistics_recorder.h" 17 #include "base/metrics/statistics_recorder.h"
17 #include "base/pickle.h" 18 #include "base/pickle.h"
18 #include "base/strings/stringprintf.h" 19 #include "base/strings/stringprintf.h"
19 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
20 21
21 namespace base { 22 namespace base {
22 23
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 143
143 histogram->AddCount(1000000000, 15); 144 histogram->AddCount(1000000000, 15);
144 histogram->AddCount(1010000000, 25); 145 histogram->AddCount(1010000000, 25);
145 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples()); 146 std::unique_ptr<HistogramSamples> snapshot2(histogram->SnapshotSamples());
146 EXPECT_EQ(55, snapshot2->TotalCount()); 147 EXPECT_EQ(55, snapshot2->TotalCount());
147 EXPECT_EQ(30, snapshot2->GetCount(1000000000)); 148 EXPECT_EQ(30, snapshot2->GetCount(1000000000));
148 EXPECT_EQ(25, snapshot2->GetCount(1010000000)); 149 EXPECT_EQ(25, snapshot2->GetCount(1010000000));
149 EXPECT_EQ(55250000000LL, snapshot2->sum()); 150 EXPECT_EQ(55250000000LL, snapshot2->sum());
150 } 151 }
151 152
153 // Make sure that counts returned by Histogram::SnapshotDelta do not overflow
154 // even when a total count (returned by Histogram::SnapshotSample) does.
155 TEST_P(SparseHistogramTest, AddCount_LargeCountsDontOverflow) {
156 std::unique_ptr<SparseHistogram> histogram(NewSparseHistogram("Sparse"));
157 std::unique_ptr<HistogramSamples> snapshot(histogram->SnapshotSamples());
158 EXPECT_EQ(0, snapshot->TotalCount());
159 EXPECT_EQ(0, snapshot->sum());
160
161 const int count = (1 << 30) - 1;
162
163 // Repeat N times to make sure that there is no internal value overflow.
164 for (int i = 0; i < 10; ++i) {
165 histogram->AddCount(42, count);
166 std::unique_ptr<HistogramSamples> samples = histogram->SnapshotDelta();
167 EXPECT_EQ(count, samples->TotalCount());
168 EXPECT_EQ(count, samples->GetCount(42));
169 }
170 }
171
152 TEST_P(SparseHistogramTest, MacroBasicTest) { 172 TEST_P(SparseHistogramTest, MacroBasicTest) {
153 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); 173 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
154 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200); 174 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 200);
155 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100); 175 UMA_HISTOGRAM_SPARSE_SLOWLY("Sparse", 100);
156 176
157 StatisticsRecorder::Histograms histograms; 177 StatisticsRecorder::Histograms histograms;
158 StatisticsRecorder::GetHistograms(&histograms); 178 StatisticsRecorder::GetHistograms(&histograms);
159 179
160 ASSERT_EQ(1U, histograms.size()); 180 ASSERT_EQ(1U, histograms.size());
161 HistogramBase* sparse_histogram = histograms[0]; 181 HistogramBase* sparse_histogram = histograms[0];
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after
357 377
358 EXPECT_EQ(1, count); 378 EXPECT_EQ(1, count);
359 EXPECT_EQ(cases[i].sample, min); 379 EXPECT_EQ(cases[i].sample, min);
360 EXPECT_EQ(cases[i].expected_max, max); 380 EXPECT_EQ(cases[i].expected_max, max);
361 381
362 it->Next(); 382 it->Next();
363 EXPECT_TRUE(it->Done()); 383 EXPECT_TRUE(it->Done());
364 } 384 }
365 } 385 }
366 386
387 TEST_P(SparseHistogramTest, HistogramNameHash) {
388 const char kName[] = "TestName";
389 HistogramBase* histogram = SparseHistogram::FactoryGet(
390 kName, HistogramBase::kUmaTargetedHistogramFlag);
391 EXPECT_EQ(histogram->name_hash(), HashMetricName(kName));
392 }
393
367 } // namespace base 394 } // namespace base
OLDNEW
« no previous file with comments | « base/metrics/sparse_histogram.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698