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

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

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: addressed review comments by Alexei Created 4 years, 11 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
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 <stddef.h> 5 #include <stddef.h>
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/json/json_reader.h" 10 #include "base/json/json_reader.h"
11 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
12 #include "base/metrics/histogram_macros.h" 12 #include "base/metrics/histogram_macros.h"
13 #include "base/metrics/histogram_persistence.h"
13 #include "base/metrics/sparse_histogram.h" 14 #include "base/metrics/sparse_histogram.h"
14 #include "base/metrics/statistics_recorder.h" 15 #include "base/metrics/statistics_recorder.h"
15 #include "base/values.h" 16 #include "base/values.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace base { 19 namespace base {
19 20
20 class StatisticsRecorderTest : public testing::Test { 21 class StatisticsRecorderTest : public testing::Test {
21 protected: 22 protected:
22 void SetUp() override { 23 void SetUp() override {
23 // Each test will have a clean state (no Histogram / BucketRanges 24 // Each test will have a clean state (no Histogram / BucketRanges
24 // registered). 25 // registered).
25 InitializeStatisticsRecorder(); 26 InitializeStatisticsRecorder();
26 } 27 }
27 28
28 void TearDown() override { UninitializeStatisticsRecorder(); } 29 void TearDown() override {
30 UninitializeStatisticsRecorder();
31 SetPersistentHistogramMemoryAllocator(nullptr);
32 }
29 33
30 void InitializeStatisticsRecorder() { 34 void InitializeStatisticsRecorder() {
31 statistics_recorder_ = new StatisticsRecorder(); 35 statistics_recorder_ = new StatisticsRecorder();
32 } 36 }
33 37
34 void UninitializeStatisticsRecorder() { 38 void UninitializeStatisticsRecorder() {
35 delete statistics_recorder_; 39 delete statistics_recorder_;
36 statistics_recorder_ = NULL; 40 statistics_recorder_ = NULL;
37 } 41 }
38 42
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after
308 EXPECT_EQ("TestHistogram2", histogram_name); 312 EXPECT_EQ("TestHistogram2", histogram_name);
309 313
310 json.clear(); 314 json.clear();
311 UninitializeStatisticsRecorder(); 315 UninitializeStatisticsRecorder();
312 316
313 // No data should be returned. 317 // No data should be returned.
314 json = StatisticsRecorder::ToJSON(query); 318 json = StatisticsRecorder::ToJSON(query);
315 EXPECT_TRUE(json.empty()); 319 EXPECT_TRUE(json.empty());
316 } 320 }
317 321
322 TEST_F(StatisticsRecorderTest, IterationTest) {
323 StatisticsRecorder::Histograms registered_histograms;
324 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest1", 30);
325 SetPersistentHistogramMemoryAllocator(
326 new LocalPersistentMemoryAllocator(64 << 10, 0, std::string()));
327 LOCAL_HISTOGRAM_COUNTS("TestHistogram.IterationTest2", 30);
328
329 StatisticsRecorder::HistogramIterator i1 = StatisticsRecorder::begin(true);
330 EXPECT_NE(StatisticsRecorder::end(), i1);
331 EXPECT_NE(StatisticsRecorder::end(), ++i1);
332 EXPECT_EQ(StatisticsRecorder::end(), ++i1);
333
334 StatisticsRecorder::HistogramIterator i2 = StatisticsRecorder::begin(false);
335 EXPECT_NE(StatisticsRecorder::end(), i2);
336 EXPECT_EQ(StatisticsRecorder::end(), ++i2);
337 }
338
318 namespace { 339 namespace {
319 340
320 // CallbackCheckWrapper is simply a convenient way to check and store that 341 // CallbackCheckWrapper is simply a convenient way to check and store that
321 // a callback was actually run. 342 // a callback was actually run.
322 struct CallbackCheckWrapper { 343 struct CallbackCheckWrapper {
323 CallbackCheckWrapper() : called(false), last_histogram_value(0) {} 344 CallbackCheckWrapper() : called(false), last_histogram_value(0) {}
324 345
325 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) { 346 void OnHistogramChanged(base::HistogramBase::Sample histogram_value) {
326 called = true; 347 called = true;
327 last_histogram_value = histogram_value; 348 last_histogram_value = histogram_value;
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
483 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10, 504 HistogramBase* histogram = Histogram::FactoryGet("TestHistogram", 1, 1000, 10,
484 HistogramBase::kNoFlags); 505 HistogramBase::kNoFlags);
485 EXPECT_TRUE(histogram); 506 EXPECT_TRUE(histogram);
486 histogram->Add(1); 507 histogram->Add(1);
487 508
488 EXPECT_TRUE(callback_wrapper.called); 509 EXPECT_TRUE(callback_wrapper.called);
489 EXPECT_EQ(callback_wrapper.last_histogram_value, 1); 510 EXPECT_EQ(callback_wrapper.last_histogram_value, 1);
490 } 511 }
491 512
492 } // namespace base 513 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698