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

Side by Side Diff: components/web_cache/browser/web_cache_manager_unittest.cc

Issue 2435603002: [WeakMemoryCache] Remove dead/live distinction of Resource outside core/fetch (Closed)
Patch Set: Rebase Created 4 years 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) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 <string> 7 #include <string>
8 8
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "components/web_cache/browser/web_cache_manager.h" 10 #include "components/web_cache/browser/web_cache_manager.h"
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
47 static std::set<int>& active_renderers(WebCacheManager* h) { 47 static std::set<int>& active_renderers(WebCacheManager* h) {
48 return h->active_renderers_; 48 return h->active_renderers_;
49 } 49 }
50 static std::set<int>& inactive_renderers(WebCacheManager* h) { 50 static std::set<int>& inactive_renderers(WebCacheManager* h) {
51 return h->inactive_renderers_; 51 return h->inactive_renderers_;
52 } 52 }
53 static void GatherStats(WebCacheManager* h, 53 static void GatherStats(WebCacheManager* h,
54 std::set<int> renderers, 54 std::set<int> renderers,
55 WebCacheManager::RendererInfo* stats) { 55 WebCacheManager::RendererInfo* stats) {
56 memset(stats, 0, sizeof(WebCacheManager::RendererInfo)); 56 memset(stats, 0, sizeof(WebCacheManager::RendererInfo));
57 h->GatherStats(renderers, &stats->capacity, &stats->live_size, 57 h->GatherStats(renderers, &stats->capacity, &stats->size);
58 &stats->dead_size);
59 } 58 }
60 static uint64_t GetSize(int tactic, 59 static uint64_t GetSize(int tactic,
61 const WebCacheManager::RendererInfo& stats) { 60 const WebCacheManager::RendererInfo& stats) {
62 return WebCacheManager::GetSize( 61 return WebCacheManager::GetSize(
63 static_cast<WebCacheManager::AllocationTactic>(tactic), stats.live_size, 62 static_cast<WebCacheManager::AllocationTactic>(tactic), stats.size);
64 stats.dead_size);
65 } 63 }
66 static bool AttemptTactic(WebCacheManager* h, 64 static bool AttemptTactic(WebCacheManager* h,
67 int active_tactic, 65 int active_tactic,
68 const WebCacheManager::RendererInfo& active_stats, 66 const WebCacheManager::RendererInfo& active_stats,
69 int inactive_tactic, 67 int inactive_tactic,
70 const WebCacheManager::RendererInfo& inactive_stats, 68 const WebCacheManager::RendererInfo& inactive_stats,
71 std::list<std::pair<int, uint64_t>>* strategy) { 69 std::list<std::pair<int, uint64_t>>* strategy) {
72 return h->AttemptTactic( 70 return h->AttemptTactic(
73 static_cast<WebCacheManager::AllocationTactic>(active_tactic), 71 static_cast<WebCacheManager::AllocationTactic>(active_tactic),
74 active_stats.live_size, active_stats.dead_size, 72 active_stats.size,
75 static_cast<WebCacheManager::AllocationTactic>(inactive_tactic), 73 static_cast<WebCacheManager::AllocationTactic>(inactive_tactic),
76 inactive_stats.live_size, inactive_stats.dead_size, strategy); 74 inactive_stats.size, strategy);
77 } 75 }
78 static void AddToStrategy(WebCacheManager* h, 76 static void AddToStrategy(WebCacheManager* h,
79 std::set<int> renderers, 77 std::set<int> renderers,
80 int tactic, 78 int tactic,
81 uint64_t extra_bytes_to_allocate, 79 uint64_t extra_bytes_to_allocate,
82 std::list<std::pair<int, uint64_t>>* strategy) { 80 std::list<std::pair<int, uint64_t>>* strategy) {
83 h->AddToStrategy(renderers, 81 h->AddToStrategy(renderers,
84 static_cast<WebCacheManager::AllocationTactic>(tactic), 82 static_cast<WebCacheManager::AllocationTactic>(tactic),
85 extra_bytes_to_allocate, 83 extra_bytes_to_allocate,
86 strategy); 84 strategy);
87 } 85 }
88 86
89 static bool RendererInfoEqual(const WebCacheManager::RendererInfo& lhs, 87 static bool RendererInfoEqual(const WebCacheManager::RendererInfo& lhs,
90 const WebCacheManager::RendererInfo& rhs) { 88 const WebCacheManager::RendererInfo& rhs) {
91 return lhs.capacity == rhs.capacity && lhs.live_size == rhs.live_size && 89 return lhs.capacity == rhs.capacity && lhs.size == rhs.size;
92 lhs.dead_size == rhs.dead_size;
93 } 90 }
94 91
95 enum { 92 enum {
96 DIVIDE_EVENLY = WebCacheManager::DIVIDE_EVENLY, 93 DIVIDE_EVENLY = WebCacheManager::DIVIDE_EVENLY,
97 KEEP_CURRENT_WITH_HEADROOM = WebCacheManager::KEEP_CURRENT_WITH_HEADROOM, 94 KEEP_CURRENT_WITH_HEADROOM = WebCacheManager::KEEP_CURRENT_WITH_HEADROOM,
98 KEEP_CURRENT = WebCacheManager::KEEP_CURRENT, 95 KEEP_CURRENT = WebCacheManager::KEEP_CURRENT,
99 KEEP_LIVE_WITH_HEADROOM = WebCacheManager::KEEP_LIVE_WITH_HEADROOM,
100 KEEP_LIVE = WebCacheManager::KEEP_LIVE,
101 }; 96 };
102 97
103 WebCacheManager* manager() { return &manager_; } 98 WebCacheManager* manager() { return &manager_; }
104 99
105 private: 100 private:
106 WebCacheManager manager_; 101 WebCacheManager manager_;
107 base::MessageLoop message_loop_; 102 base::MessageLoop message_loop_;
108 content::TestBrowserThread ui_thread_; 103 content::TestBrowserThread ui_thread_;
109 }; 104 };
110 105
111 // static 106 // static
112 const int WebCacheManagerTest::kRendererID = 146; 107 const int WebCacheManagerTest::kRendererID = 146;
113 108
114 // static 109 // static
115 const int WebCacheManagerTest::kRendererID2 = 245; 110 const int WebCacheManagerTest::kRendererID2 = 245;
116 111
117 // static 112 // static
118 const WebCacheManager::RendererInfo WebCacheManagerTest::kStats = { 113 const WebCacheManager::RendererInfo WebCacheManagerTest::kStats = {
119 base::Time(), 0, 1024 * 1024, 1024 * 1024, 256 * 1024, 512, 114 base::Time(), 1024 * 1024, 256 * 1024 + 512,
120 }; 115 };
121 116
122 // static 117 // static
123 const WebCacheManager::RendererInfo WebCacheManagerTest::kStats2 = { 118 const WebCacheManager::RendererInfo WebCacheManagerTest::kStats2 = {
124 base::Time(), 0, 2 * 1024 * 1024, 2 * 1024 * 1024, 2 * 256 * 1024, 2 * 512, 119 base::Time(), 2 * 1024 * 1024, 2 * 256 * 1024 + 2 * 512,
125 }; 120 };
126 121
127 TEST_F(WebCacheManagerTest, AddRemoveRendererTest) { 122 TEST_F(WebCacheManagerTest, AddRemoveRendererTest) {
128 EXPECT_EQ(0U, active_renderers(manager()).size()); 123 EXPECT_EQ(0U, active_renderers(manager()).size());
129 EXPECT_EQ(0U, inactive_renderers(manager()).size()); 124 EXPECT_EQ(0U, inactive_renderers(manager()).size());
130 125
131 manager()->Add(kRendererID); 126 manager()->Add(kRendererID);
132 EXPECT_EQ(1U, active_renderers(manager()).count(kRendererID)); 127 EXPECT_EQ(1U, active_renderers(manager()).count(kRendererID));
133 EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID)); 128 EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID));
134 129
(...skipping 18 matching lines...) Expand all
153 EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID)); 148 EXPECT_EQ(0U, inactive_renderers(manager()).count(kRendererID));
154 149
155 manager()->Remove(kRendererID); 150 manager()->Remove(kRendererID);
156 } 151 }
157 152
158 TEST_F(WebCacheManagerTest, ObserveStatsTest) { 153 TEST_F(WebCacheManagerTest, ObserveStatsTest) {
159 manager()->Add(kRendererID); 154 manager()->Add(kRendererID);
160 155
161 EXPECT_EQ(1U, stats(manager()).size()); 156 EXPECT_EQ(1U, stats(manager()).size());
162 157
163 manager()->ObserveStats(kRendererID, kStats.min_dead_capacity, 158 manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
164 kStats.max_dead_capacity, kStats.capacity,
165 kStats.live_size, kStats.dead_size);
166 159
167 EXPECT_EQ(1U, stats(manager()).size()); 160 EXPECT_EQ(1U, stats(manager()).size());
168 EXPECT_TRUE(RendererInfoEqual(kStats, stats(manager())[kRendererID])); 161 EXPECT_TRUE(RendererInfoEqual(kStats, stats(manager())[kRendererID]));
169 162
170 manager()->Remove(kRendererID); 163 manager()->Remove(kRendererID);
171 } 164 }
172 165
173 TEST_F(WebCacheManagerTest, SetGlobalSizeLimitTest) { 166 TEST_F(WebCacheManagerTest, SetGlobalSizeLimitTest) {
174 uint64_t limit = manager()->GetDefaultGlobalSizeLimit(); 167 uint64_t limit = manager()->GetDefaultGlobalSizeLimit();
175 manager()->SetGlobalSizeLimit(limit); 168 manager()->SetGlobalSizeLimit(limit);
176 EXPECT_EQ(limit, manager()->global_size_limit()); 169 EXPECT_EQ(limit, manager()->global_size_limit());
177 170
178 manager()->SetGlobalSizeLimit(0); 171 manager()->SetGlobalSizeLimit(0);
179 EXPECT_EQ(0U, manager()->global_size_limit()); 172 EXPECT_EQ(0U, manager()->global_size_limit());
180 } 173 }
181 174
182 TEST_F(WebCacheManagerTest, GatherStatsTest) { 175 TEST_F(WebCacheManagerTest, GatherStatsTest) {
183 manager()->Add(kRendererID); 176 manager()->Add(kRendererID);
184 manager()->Add(kRendererID2); 177 manager()->Add(kRendererID2);
185 178
186 manager()->ObserveStats(kRendererID, kStats.min_dead_capacity, 179 manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
187 kStats.max_dead_capacity, kStats.capacity, 180 manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
188 kStats.live_size, kStats.dead_size);
189 manager()->ObserveStats(kRendererID2, kStats2.min_dead_capacity,
190 kStats2.max_dead_capacity, kStats2.capacity,
191 kStats2.live_size, kStats2.dead_size);
192 181
193 std::set<int> renderer_set; 182 std::set<int> renderer_set;
194 renderer_set.insert(kRendererID); 183 renderer_set.insert(kRendererID);
195 184
196 WebCacheManager::RendererInfo stats; 185 WebCacheManager::RendererInfo stats;
197 GatherStats(manager(), renderer_set, &stats); 186 GatherStats(manager(), renderer_set, &stats);
198 187
199 EXPECT_TRUE(RendererInfoEqual(kStats, stats)); 188 EXPECT_TRUE(RendererInfoEqual(kStats, stats));
200 189
201 renderer_set.insert(kRendererID2); 190 renderer_set.insert(kRendererID2);
202 GatherStats(manager(), renderer_set, &stats); 191 GatherStats(manager(), renderer_set, &stats);
203 192
204 WebCacheManager::RendererInfo expected_stats = kStats; 193 WebCacheManager::RendererInfo expected_stats = kStats;
205 expected_stats.min_dead_capacity += kStats2.min_dead_capacity;
206 expected_stats.max_dead_capacity += kStats2.max_dead_capacity;
207 expected_stats.capacity += kStats2.capacity; 194 expected_stats.capacity += kStats2.capacity;
208 expected_stats.live_size += kStats2.live_size; 195 expected_stats.size += kStats2.size;
209 expected_stats.dead_size += kStats2.dead_size;
210 196
211 EXPECT_TRUE(RendererInfoEqual(expected_stats, stats)); 197 EXPECT_TRUE(RendererInfoEqual(expected_stats, stats));
212 198
213 manager()->Remove(kRendererID); 199 manager()->Remove(kRendererID);
214 manager()->Remove(kRendererID2); 200 manager()->Remove(kRendererID2);
215 } 201 }
216 202
217 TEST_F(WebCacheManagerTest, GetSizeTest) { 203 TEST_F(WebCacheManagerTest, GetSizeTest) {
218 EXPECT_EQ(0U, GetSize(DIVIDE_EVENLY, kStats)); 204 EXPECT_EQ(0U, GetSize(DIVIDE_EVENLY, kStats));
219 EXPECT_LT(256 * 1024u + 512, GetSize(KEEP_CURRENT_WITH_HEADROOM, kStats)); 205 EXPECT_LT(256 * 1024u + 512, GetSize(KEEP_CURRENT_WITH_HEADROOM, kStats));
220 EXPECT_EQ(256 * 1024u + 512, GetSize(KEEP_CURRENT, kStats)); 206 EXPECT_EQ(256 * 1024u + 512, GetSize(KEEP_CURRENT, kStats));
221 EXPECT_LT(256 * 1024u, GetSize(KEEP_LIVE_WITH_HEADROOM, kStats));
222 EXPECT_EQ(256 * 1024u, GetSize(KEEP_LIVE, kStats));
223 } 207 }
224 208
225 TEST_F(WebCacheManagerTest, AttemptTacticTest) { 209 TEST_F(WebCacheManagerTest, AttemptTacticTest) {
226 manager()->Add(kRendererID); 210 manager()->Add(kRendererID);
227 manager()->Add(kRendererID2); 211 manager()->Add(kRendererID2);
228 212
229 manager()->ObserveActivity(kRendererID); 213 manager()->ObserveActivity(kRendererID);
230 SimulateInactivity(manager(), kRendererID2); 214 SimulateInactivity(manager(), kRendererID2);
231 215
232 manager()->ObserveStats(kRendererID, kStats.min_dead_capacity, 216 manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
233 kStats.max_dead_capacity, kStats.capacity, 217 manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
234 kStats.live_size, kStats.dead_size);
235 manager()->ObserveStats(kRendererID2, kStats2.min_dead_capacity,
236 kStats2.max_dead_capacity, kStats2.capacity,
237 kStats2.live_size, kStats2.dead_size);
238
239 manager()->SetGlobalSizeLimit(kStats.live_size + kStats.dead_size +
240 kStats2.live_size + kStats2.dead_size / 2);
241 218
242 AllocationStrategy strategy; 219 AllocationStrategy strategy;
243 220
221 manager()->SetGlobalSizeLimit(kStats.size + kStats2.size - 1);
244 EXPECT_FALSE(AttemptTactic(manager(), 222 EXPECT_FALSE(AttemptTactic(manager(),
245 KEEP_CURRENT, 223 KEEP_CURRENT,
246 kStats, 224 kStats,
247 KEEP_CURRENT, 225 KEEP_CURRENT,
248 kStats2, 226 kStats2,
249 &strategy)); 227 &strategy));
250 EXPECT_TRUE(strategy.empty()); 228 EXPECT_TRUE(strategy.empty());
251 229
252 EXPECT_TRUE(AttemptTactic(manager(), 230 manager()->SetGlobalSizeLimit(kStats.size + kStats2.size);
253 KEEP_CURRENT, 231 EXPECT_TRUE(AttemptTactic(manager(), KEEP_CURRENT, kStats, KEEP_CURRENT,
254 kStats, 232 kStats2, &strategy));
255 KEEP_LIVE,
256 kStats2,
257 &strategy));
258 EXPECT_EQ(2U, strategy.size()); 233 EXPECT_EQ(2U, strategy.size());
259 234
260 AllocationStrategy::iterator iter = strategy.begin(); 235 AllocationStrategy::iterator iter = strategy.begin();
261 while (iter != strategy.end()) { 236 while (iter != strategy.end()) {
262 if (iter->first == kRendererID) 237 if (iter->first == kRendererID)
263 EXPECT_LE(kStats.live_size + kStats.dead_size, iter->second); 238 EXPECT_LE(kStats.size, iter->second);
264 else if (iter->first == kRendererID2) 239 else if (iter->first == kRendererID2)
265 EXPECT_LE(kStats2.live_size, iter->second); 240 EXPECT_LE(kStats2.size, iter->second);
266 else 241 else
267 ADD_FAILURE(); // Unexpected entry in strategy. 242 ADD_FAILURE(); // Unexpected entry in strategy.
268 ++iter; 243 ++iter;
269 } 244 }
270 245
271 manager()->Remove(kRendererID); 246 manager()->Remove(kRendererID);
272 manager()->Remove(kRendererID2); 247 manager()->Remove(kRendererID2);
273 } 248 }
274 249
275 TEST_F(WebCacheManagerTest, AddToStrategyTest) { 250 TEST_F(WebCacheManagerTest, AddToStrategyTest) {
276 manager()->Add(kRendererID); 251 manager()->Add(kRendererID);
277 manager()->Add(kRendererID2); 252 manager()->Add(kRendererID2);
278 253
279 std::set<int> renderer_set; 254 std::set<int> renderer_set;
280 renderer_set.insert(kRendererID); 255 renderer_set.insert(kRendererID);
281 renderer_set.insert(kRendererID2); 256 renderer_set.insert(kRendererID2);
282 257
283 manager()->ObserveStats(kRendererID, kStats.min_dead_capacity, 258 manager()->ObserveStats(kRendererID, kStats.capacity, kStats.size);
284 kStats.max_dead_capacity, kStats.capacity, 259 manager()->ObserveStats(kRendererID2, kStats2.capacity, kStats2.size);
285 kStats.live_size, kStats.dead_size);
286 manager()->ObserveStats(kRendererID2, kStats2.min_dead_capacity,
287 kStats2.max_dead_capacity, kStats2.capacity,
288 kStats2.live_size, kStats2.dead_size);
289 260
290 const uint64_t kExtraBytesToAllocate = 10 * 1024; 261 const uint64_t kExtraBytesToAllocate = 10 * 1024;
291 262
292 AllocationStrategy strategy; 263 AllocationStrategy strategy;
293 AddToStrategy(manager(), 264 AddToStrategy(manager(),
294 renderer_set, 265 renderer_set,
295 KEEP_CURRENT, 266 KEEP_CURRENT,
296 kExtraBytesToAllocate, 267 kExtraBytesToAllocate,
297 &strategy); 268 &strategy);
298 269
299 EXPECT_EQ(2U, strategy.size()); 270 EXPECT_EQ(2U, strategy.size());
300 271
301 uint64_t total_bytes = 0; 272 uint64_t total_bytes = 0;
302 AllocationStrategy::iterator iter = strategy.begin(); 273 AllocationStrategy::iterator iter = strategy.begin();
303 while (iter != strategy.end()) { 274 while (iter != strategy.end()) {
304 total_bytes += iter->second; 275 total_bytes += iter->second;
305 276
306 if (iter->first == kRendererID) 277 if (iter->first == kRendererID)
307 EXPECT_LE(kStats.live_size + kStats.dead_size, iter->second); 278 EXPECT_LE(kStats.size, iter->second);
308 else if (iter->first == kRendererID2) 279 else if (iter->first == kRendererID2)
309 EXPECT_LE(kStats2.live_size + kStats2.dead_size, iter->second); 280 EXPECT_LE(kStats2.size, iter->second);
310 else 281 else
311 ADD_FAILURE(); // Unexpected entry in strategy. 282 ADD_FAILURE(); // Unexpected entry in strategy.
312 ++iter; 283 ++iter;
313 } 284 }
314 285
315 uint64_t expected_total_bytes = kExtraBytesToAllocate + kStats.live_size + 286 uint64_t expected_total_bytes =
316 kStats.dead_size + kStats2.live_size + 287 kExtraBytesToAllocate + kStats.size + kStats2.size;
317 kStats2.dead_size;
318 288
319 EXPECT_GE(expected_total_bytes, total_bytes); 289 EXPECT_GE(expected_total_bytes, total_bytes);
320 290
321 manager()->Remove(kRendererID); 291 manager()->Remove(kRendererID);
322 manager()->Remove(kRendererID2); 292 manager()->Remove(kRendererID2);
323 } 293 }
324 294
325 // Regression test for http://crbug.com/12362. 295 // Regression test for http://crbug.com/12362.
326 // There are three operations in the following order will cause the crash: 296 // There are three operations in the following order will cause the crash:
327 // Remove(kRendererID) -> ObserveActivity(kRendererID) -> Remove(kRendererID2) 297 // Remove(kRendererID) -> ObserveActivity(kRendererID) -> Remove(kRendererID2)
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
467 manager()->ReviseAllocationStrategy(); 437 manager()->ReviseAllocationStrategy();
468 438
469 manager()->Remove(kRendererID); 439 manager()->Remove(kRendererID);
470 manager()->ReviseAllocationStrategy(); 440 manager()->ReviseAllocationStrategy();
471 441
472 manager()->ObserveActivity(kRendererID); 442 manager()->ObserveActivity(kRendererID);
473 manager()->ReviseAllocationStrategy(); 443 manager()->ReviseAllocationStrategy();
474 } 444 }
475 445
476 } // namespace web_cache 446 } // namespace web_cache
OLDNEW
« no previous file with comments | « components/web_cache/browser/web_cache_manager.cc ('k') | components/web_cache/public/interfaces/web_cache.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698