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

Side by Side Diff: net/disk_cache/disk_cache_perftest.cc

Issue 2501723002: Fix random number generation in disk_cache_perftests. (Closed)
Patch Set: moar Created 4 years, 1 month 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 | « no previous file | 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) 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 <limits> 5 #include <limits>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/bind_helpers.h" 9 #include "base/bind_helpers.h"
10 #include "base/files/file_enumerator.h" 10 #include "base/files/file_enumerator.h"
11 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
12 #include "base/hash.h" 12 #include "base/hash.h"
13 #include "base/process/process_metrics.h" 13 #include "base/process/process_metrics.h"
14 #include "base/rand_util.h"
14 #include "base/run_loop.h" 15 #include "base/run_loop.h"
15 #include "base/strings/string_util.h" 16 #include "base/strings/string_util.h"
16 #include "base/test/perf_time_logger.h" 17 #include "base/test/perf_time_logger.h"
17 #include "base/test/test_file_util.h" 18 #include "base/test/test_file_util.h"
18 #include "base/threading/thread.h" 19 #include "base/threading/thread.h"
19 #include "net/base/cache_type.h" 20 #include "net/base/cache_type.h"
20 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
21 #include "net/base/net_errors.h" 22 #include "net/base/net_errors.h"
22 #include "net/base/test_completion_callback.h" 23 #include "net/base/test_completion_callback.h"
23 #include "net/disk_cache/blockfile/backend_impl.h" 24 #include "net/disk_cache/blockfile/backend_impl.h"
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 int expected = 0; 108 int expected = 0;
108 109
109 MessageLoopHelper helper; 110 MessageLoopHelper helper;
110 CallbackTest callback(&helper, true); 111 CallbackTest callback(&helper, true);
111 112
112 base::PerfTimeLogger timer("Write disk cache entries"); 113 base::PerfTimeLogger timer("Write disk cache entries");
113 114
114 for (int i = 0; i < kNumEntries; i++) { 115 for (int i = 0; i < kNumEntries; i++) {
115 TestEntry entry; 116 TestEntry entry;
116 entry.key = GenerateKey(true); 117 entry.key = GenerateKey(true);
117 entry.data_len = rand() % kBodySize; 118 entry.data_len = base::RandInt(0, kBodySize);
118 entries_.push_back(entry); 119 entries_.push_back(entry);
119 120
120 disk_cache::Entry* cache_entry; 121 disk_cache::Entry* cache_entry;
121 net::TestCompletionCallback cb; 122 net::TestCompletionCallback cb;
122 int rv = cache_->CreateEntry(entry.key, &cache_entry, cb.callback()); 123 int rv = cache_->CreateEntry(entry.key, &cache_entry, cb.callback());
123 if (net::OK != cb.GetResult(rv)) 124 if (net::OK != cb.GetResult(rv))
124 break; 125 break;
125 int ret = cache_entry->WriteData( 126 int ret = cache_entry->WriteData(
126 0, 0, buffer1.get(), kHeadersSize, 127 0, 0, buffer1.get(), kHeadersSize,
127 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false); 128 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false);
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 cache_entry->Close(); 190 cache_entry->Close();
190 } 191 }
191 192
192 helper.WaitUntilCacheIoFinished(expected); 193 helper.WaitUntilCacheIoFinished(expected);
193 timer.Done(); 194 timer.Done();
194 195
195 return (expected == helper.callbacks_called()); 196 return (expected == helper.callbacks_called());
196 } 197 }
197 198
198 TEST_F(DiskCachePerfTest, BlockfileHashes) { 199 TEST_F(DiskCachePerfTest, BlockfileHashes) {
199 int seed = static_cast<int>(Time::Now().ToInternalValue());
200 srand(seed);
201
202 base::PerfTimeLogger timer("Hash disk cache keys"); 200 base::PerfTimeLogger timer("Hash disk cache keys");
203 for (int i = 0; i < 300000; i++) { 201 for (int i = 0; i < 300000; i++) {
204 std::string key = GenerateKey(true); 202 std::string key = GenerateKey(true);
205 base::Hash(key); 203 base::Hash(key);
206 } 204 }
207 timer.Done(); 205 timer.Done();
208 } 206 }
209 207
210 void DiskCachePerfTest::ResetAndEvictSystemDiskCache() { 208 void DiskCachePerfTest::ResetAndEvictSystemDiskCache() {
211 base::RunLoop().RunUntilIdle(); 209 base::RunLoop().RunUntilIdle();
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 TEST_F(DiskCachePerfTest, CacheBackendPerformance) { 260 TEST_F(DiskCachePerfTest, CacheBackendPerformance) {
263 CacheBackendPerformance(); 261 CacheBackendPerformance();
264 } 262 }
265 263
266 TEST_F(DiskCachePerfTest, SimpleCacheBackendPerformance) { 264 TEST_F(DiskCachePerfTest, SimpleCacheBackendPerformance) {
267 SetSimpleCacheMode(); 265 SetSimpleCacheMode();
268 CacheBackendPerformance(); 266 CacheBackendPerformance();
269 } 267 }
270 268
271 int BlockSize() { 269 int BlockSize() {
272 // We can use form 1 to 4 blocks. 270 // We can use form 1 to 4 blocks.
jkarlin 2016/11/15 12:18:29 While you're here.. s/form/from/
gavinp 2016/11/15 15:30:53 Done. Also removed the function; documented the ro
273 return (rand() & 0x3) + 1; 271 return base::RandInt(1, 4);
274 } 272 }
275 273
276 // Creating and deleting "entries" on a block-file is something quite frequent 274 // Creating and deleting "entries" on a block-file is something quite frequent
277 // (after all, almost everything is stored on block files). The operation is 275 // (after all, almost everything is stored on block files). The operation is
278 // almost free when the file is empty, but can be expensive if the file gets 276 // almost free when the file is empty, but can be expensive if the file gets
279 // fragmented, or if we have multiple files. This test measures that scenario, 277 // fragmented, or if we have multiple files. This test measures that scenario,
280 // by using multiple, highly fragmented files. 278 // by using multiple, highly fragmented files.
281 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { 279 TEST_F(DiskCachePerfTest, BlockFilesPerformance) {
282 ASSERT_TRUE(CleanupCacheDir()); 280 ASSERT_TRUE(CleanupCacheDir());
283 281
284 disk_cache::BlockFiles files(cache_path_); 282 disk_cache::BlockFiles files(cache_path_);
285 ASSERT_TRUE(files.Init(true)); 283 ASSERT_TRUE(files.Init(true));
286 284
287 int seed = static_cast<int>(Time::Now().ToInternalValue());
288 srand(seed);
289
290 const int kNumBlocks = 60000; 285 const int kNumBlocks = 60000;
291 disk_cache::Addr address[kNumBlocks]; 286 disk_cache::Addr address[kNumBlocks];
292 287
293 base::PerfTimeLogger timer1("Fill three block-files"); 288 base::PerfTimeLogger timer1("Fill three block-files");
294 289
295 // Fill up the 32-byte block file (use three files). 290 // Fill up the 32-byte block file (use three files).
296 for (int i = 0; i < kNumBlocks; i++) { 291 for (int i = 0; i < kNumBlocks; i++) {
297 EXPECT_TRUE( 292 EXPECT_TRUE(
298 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[i])); 293 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[i]));
299 } 294 }
300 295
301 timer1.Done(); 296 timer1.Done();
302 base::PerfTimeLogger timer2("Create and delete blocks"); 297 base::PerfTimeLogger timer2("Create and delete blocks");
303 298
304 for (int i = 0; i < 200000; i++) { 299 for (int i = 0; i < 200000; i++) {
305 int entry = rand() * (kNumBlocks / RAND_MAX + 1); 300 int entry = base::RandInt(0, kNumBlocks);
jkarlin 2016/11/15 12:18:29 base::RandInt(0, kNumBlocks-1) since it's inclusiv
gavinp 2016/11/15 15:30:54 Done. I can't believe these gymnastics. This is s
301
306 if (entry >= kNumBlocks) 302 if (entry >= kNumBlocks)
jkarlin 2016/11/15 12:18:29 This condition should go.
gavinp 2016/11/15 15:30:54 Done.
307 entry = 0; 303 entry = 0;
308 304
309 files.DeleteBlock(address[entry], false); 305 files.DeleteBlock(address[entry], false);
310 EXPECT_TRUE( 306 EXPECT_TRUE(
311 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry])); 307 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry]));
312 } 308 }
313 309
314 timer2.Done(); 310 timer2.Done();
315 base::RunLoop().RunUntilIdle(); 311 base::RunLoop().RunUntilIdle();
316 } 312 }
317 313
318 } // namespace 314 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698