| OLD | NEW |
| 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 Loading... |
| 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 Loading... |
| 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 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 | 259 |
| 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() { | |
| 272 // We can use form 1 to 4 blocks. | |
| 273 return (rand() & 0x3) + 1; | |
| 274 } | |
| 275 | |
| 276 // Creating and deleting "entries" on a block-file is something quite frequent | 269 // 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 | 270 // (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 | 271 // 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, | 272 // fragmented, or if we have multiple files. This test measures that scenario, |
| 280 // by using multiple, highly fragmented files. | 273 // by using multiple, highly fragmented files. |
| 281 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { | 274 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { |
| 282 ASSERT_TRUE(CleanupCacheDir()); | 275 ASSERT_TRUE(CleanupCacheDir()); |
| 283 | 276 |
| 284 disk_cache::BlockFiles files(cache_path_); | 277 disk_cache::BlockFiles files(cache_path_); |
| 285 ASSERT_TRUE(files.Init(true)); | 278 ASSERT_TRUE(files.Init(true)); |
| 286 | 279 |
| 287 int seed = static_cast<int>(Time::Now().ToInternalValue()); | |
| 288 srand(seed); | |
| 289 | |
| 290 const int kNumBlocks = 60000; | 280 const int kNumBlocks = 60000; |
| 291 disk_cache::Addr address[kNumBlocks]; | 281 disk_cache::Addr address[kNumBlocks]; |
| 292 | 282 |
| 293 base::PerfTimeLogger timer1("Fill three block-files"); | 283 base::PerfTimeLogger timer1("Fill three block-files"); |
| 294 | 284 |
| 295 // Fill up the 32-byte block file (use three files). | 285 // Fill up the 32-byte block file (use three files). |
| 296 for (int i = 0; i < kNumBlocks; i++) { | 286 for (int i = 0; i < kNumBlocks; i++) { |
| 287 int block_size = base::RandInt(1, 4); |
| 297 EXPECT_TRUE( | 288 EXPECT_TRUE( |
| 298 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[i])); | 289 files.CreateBlock(disk_cache::RANKINGS, block_size, &address[i])); |
| 299 } | 290 } |
| 300 | 291 |
| 301 timer1.Done(); | 292 timer1.Done(); |
| 302 base::PerfTimeLogger timer2("Create and delete blocks"); | 293 base::PerfTimeLogger timer2("Create and delete blocks"); |
| 303 | 294 |
| 304 for (int i = 0; i < 200000; i++) { | 295 for (int i = 0; i < 200000; i++) { |
| 305 int entry = rand() * (kNumBlocks / RAND_MAX + 1); | 296 int block_size = base::RandInt(1, 4); |
| 306 if (entry >= kNumBlocks) | 297 int entry = base::RandInt(0, kNumBlocks - 1); |
| 307 entry = 0; | |
| 308 | 298 |
| 309 files.DeleteBlock(address[entry], false); | 299 files.DeleteBlock(address[entry], false); |
| 310 EXPECT_TRUE( | 300 EXPECT_TRUE( |
| 311 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry])); | 301 files.CreateBlock(disk_cache::RANKINGS, block_size, &address[entry])); |
| 312 } | 302 } |
| 313 | 303 |
| 314 timer2.Done(); | 304 timer2.Done(); |
| 315 base::RunLoop().RunUntilIdle(); | 305 base::RunLoop().RunUntilIdle(); |
| 316 } | 306 } |
| 317 | 307 |
| 318 } // namespace | 308 } // namespace |
| OLD | NEW |