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

Side by Side Diff: components/metrics/leak_detector/leak_detector_unittest.cc

Issue 1868193003: Store alloc history data in memory leak report protobuf (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@leak-history
Patch Set: Call MergeFrom(); Update comment about repeated fields; Use braces for single-line for loops Created 4 years, 8 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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 "components/metrics/leak_detector/leak_detector.h" 5 #include "components/metrics/leak_detector/leak_detector.h"
6 6
7 #include <algorithm>
7 #include <set> 8 #include <set>
8 9
9 #include "base/allocator/allocator_extension.h" 10 #include "base/allocator/allocator_extension.h"
10 #include "base/macros.h" 11 #include "base/macros.h"
11 #include "content/public/test/test_browser_thread_bundle.h" 12 #include "content/public/test/test_browser_thread_bundle.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace metrics { 15 namespace metrics {
15 16
16 namespace { 17 namespace {
17 18
18 // Default values for LeakDetector params. See header file for the meaning of 19 // Default values for LeakDetector params. See header file for the meaning of
19 // each parameter. 20 // each parameter.
20 const float kDefaultSamplingRate = 1.0f; 21 const float kDefaultSamplingRate = 1.0f;
21 const size_t kDefaultMaxCallStackUnwindDepth = 4; 22 const size_t kDefaultMaxCallStackUnwindDepth = 4;
22 const uint64_t kDefaultAnalysisIntervalBytes = 32 * 1024 * 1024; // 32 MiB. 23 const uint64_t kDefaultAnalysisIntervalBytes = 32 * 1024 * 1024; // 32 MiB.
23 const uint32_t kDefaultSizeSuspicionThreshold = 4; 24 const uint32_t kDefaultSizeSuspicionThreshold = 4;
24 const uint32_t kDefaultCallStackSuspicionThreshold = 4; 25 const uint32_t kDefaultCallStackSuspicionThreshold = 4;
25 26
26 using LeakReport = LeakDetector::LeakReport;
27
28 // Observer class that receives leak reports and stores them in |reports_|. 27 // Observer class that receives leak reports and stores them in |reports_|.
29 // Only one copy of each unique report will be stored. 28 // Only one copy of each unique report will be stored.
30 class TestObserver : public LeakDetector::Observer { 29 class TestObserver : public LeakDetector::Observer {
31 public: 30 public:
32 // Contains a comparator function used to compare LeakReports for uniqueness. 31 // Contains a comparator function used to compare MemoryLeakReportProtos for
32 // uniqueness.
33 struct Comparator { 33 struct Comparator {
34 bool operator()(const LeakReport& a, const LeakReport& b) const { 34 bool operator()(const MemoryLeakReportProto& a,
35 if (a.alloc_size_bytes != b.alloc_size_bytes) 35 const MemoryLeakReportProto& b) const {
36 return a.alloc_size_bytes < b.alloc_size_bytes; 36 if (a.size_bytes() != b.size_bytes())
37 return a.size_bytes() < b.size_bytes();
37 38
38 return a.call_stack < b.call_stack; 39 return std::lexicographical_compare(a.call_stack().begin(),
40 a.call_stack().end(),
41 b.call_stack().begin(),
42 b.call_stack().end());
39 } 43 }
40 }; 44 };
41 45
42 TestObserver() {} 46 TestObserver() {}
43 47
44 void OnLeakFound(const LeakReport& report) override { 48 void OnLeaksFound(
45 reports_.insert(report); 49 const std::vector<MemoryLeakReportProto>& reports) override {
50 reports_.insert(reports.begin(), reports.end());
46 } 51 }
47 52
48 const std::set<LeakReport, Comparator>& reports() const { return reports_; } 53 const std::set<MemoryLeakReportProto, Comparator>& reports() const {
54 return reports_;
55 }
49 56
50 private: 57 private:
51 // Container for all leak reports received through OnLeakFound(). Stores only 58 // Container for all leak reports received through OnLeaksFound(). Stores only
52 // one copy of each unique report. 59 // one copy of each unique report.
53 std::set<LeakReport, Comparator> reports_; 60 std::set<MemoryLeakReportProto, Comparator> reports_;
54 61
55 DISALLOW_COPY_AND_ASSIGN(TestObserver); 62 DISALLOW_COPY_AND_ASSIGN(TestObserver);
56 }; 63 };
57 64
58 } // namespace 65 } // namespace
59 66
60 class LeakDetectorTest : public ::testing::Test { 67 class LeakDetectorTest : public ::testing::Test {
61 public: 68 public:
62 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) { 69 LeakDetectorTest() : detector_(LeakDetector::GetInstance()) {
63 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth, 70 detector_->Init(kDefaultSamplingRate, kDefaultMaxCallStackUnwindDepth,
64 kDefaultAnalysisIntervalBytes, 71 kDefaultAnalysisIntervalBytes,
65 kDefaultSizeSuspicionThreshold, 72 kDefaultSizeSuspicionThreshold,
66 kDefaultCallStackSuspicionThreshold); 73 kDefaultCallStackSuspicionThreshold);
67 } 74 }
68 75
69 protected: 76 protected:
70 // Points to the instance of LeakDetector returned by GetInstance(). 77 // Points to the instance of LeakDetector returned by GetInstance().
71 LeakDetector* detector_; 78 LeakDetector* detector_;
72 79
73 private: 80 private:
74 // For supporting content::BrowserThread operations. 81 // For supporting content::BrowserThread operations.
75 content::TestBrowserThreadBundle thread_bundle_; 82 content::TestBrowserThreadBundle thread_bundle_;
76 83
77 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest); 84 DISALLOW_COPY_AND_ASSIGN(LeakDetectorTest);
78 }; 85 };
79 86
80 TEST_F(LeakDetectorTest, NotifyObservers) { 87 TEST_F(LeakDetectorTest, NotifyObservers) {
81 // Generate two sets of leak reports. 88 // Generate two sets of leak reports.
82 std::vector<LeakReport> reports1(3); 89 std::vector<MemoryLeakReportProto> reports1(3);
83 reports1[0].alloc_size_bytes = 8; 90 reports1[0].set_size_bytes(8);
84 reports1[0].call_stack = {1, 2, 3, 4}; 91 for (uint64_t entry : {1, 2, 3, 4}) {
85 reports1[1].alloc_size_bytes = 16; 92 reports1[0].add_call_stack(entry);
86 reports1[1].call_stack = {5, 6, 7, 8}; 93 }
87 reports1[2].alloc_size_bytes = 24; 94 reports1[1].set_size_bytes(16);
88 reports1[2].call_stack = {9, 10, 11, 12}; 95 for (uint64_t entry : {5, 6, 7, 8}) {
96 reports1[1].add_call_stack(entry);
97 }
98 reports1[2].set_size_bytes(24);
99 for (uint64_t entry : {9, 10, 11, 12}) {
100 reports1[1].add_call_stack(entry);
101 }
89 102
90 std::vector<LeakReport> reports2(3); 103 std::vector<MemoryLeakReportProto> reports2(3);
91 reports2[0].alloc_size_bytes = 32; 104 reports2[0].set_size_bytes(32);
92 reports2[0].call_stack = {1, 2, 4, 8}; 105 for (uint64_t entry : {1, 2, 4, 8}) {
93 reports2[1].alloc_size_bytes = 40; 106 reports2[0].add_call_stack(entry);
94 reports2[1].call_stack = {16, 32, 64, 128}; 107 }
95 reports2[2].alloc_size_bytes = 48; 108 reports2[1].set_size_bytes(40);
96 reports2[2].call_stack = {256, 512, 1024, 2048}; 109 for (uint64_t entry : {16, 32, 64, 128}) {
110 reports2[1].add_call_stack(entry);
111 }
112 reports2[2].set_size_bytes(48);
113 for (uint64_t entry : {256, 512, 1024, 2048}) {
114 reports2[2].add_call_stack(entry);
115 }
97 116
98 // Register three observers; 117 // Register three observers;
99 TestObserver obs1, obs2, obs3; 118 TestObserver obs1, obs2, obs3;
100 detector_->AddObserver(&obs1); 119 detector_->AddObserver(&obs1);
101 detector_->AddObserver(&obs2); 120 detector_->AddObserver(&obs2);
102 detector_->AddObserver(&obs3); 121 detector_->AddObserver(&obs3);
103 122
104 // Pass both sets of reports to the leak detector. 123 // Pass both sets of reports to the leak detector.
105 detector_->NotifyObservers(reports1); 124 detector_->NotifyObservers(reports1);
106 detector_->NotifyObservers(reports2); 125 detector_->NotifyObservers(reports2);
107 126
108 // Check that all three observers got both sets of reports, passed in 127 // Check that all three observers got both sets of reports, passed in
109 // separately. 128 // separately.
110 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) { 129 for (const TestObserver* obs : {&obs1, &obs2, &obs3}) {
111 EXPECT_EQ(6U, obs->reports().size()); 130 EXPECT_EQ(6U, obs->reports().size());
112 for (const auto& report : {reports1[0], reports1[1], reports1[2], 131 for (const auto& report : {reports1[0], reports1[1], reports1[2],
113 reports2[0], reports2[1], reports2[2]}) { 132 reports2[0], reports2[1], reports2[2]}) {
114 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end()); 133 EXPECT_TRUE(obs->reports().find(report) != obs->reports().end());
115 } 134 }
116 } 135 }
117 } 136 }
118 137
119 } // namespace metrics 138 } // namespace metrics
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698