| 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_path.h" |
| 9 #include "base/hash.h" | 11 #include "base/hash.h" |
| 10 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
| 11 #include "base/test/perf_time_logger.h" | 13 #include "base/test/perf_time_logger.h" |
| 12 #include "base/test/test_file_util.h" | 14 #include "base/test/test_file_util.h" |
| 13 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 14 #include "net/base/cache_type.h" | 16 #include "net/base/cache_type.h" |
| 15 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 16 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 17 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
| 18 #include "net/disk_cache/blockfile/backend_impl.h" | 20 #include "net/disk_cache/blockfile/backend_impl.h" |
| 19 #include "net/disk_cache/blockfile/block_files.h" | 21 #include "net/disk_cache/blockfile/block_files.h" |
| 20 #include "net/disk_cache/disk_cache.h" | 22 #include "net/disk_cache/disk_cache.h" |
| 21 #include "net/disk_cache/disk_cache_test_base.h" | 23 #include "net/disk_cache/disk_cache_test_base.h" |
| 22 #include "net/disk_cache/disk_cache_test_util.h" | 24 #include "net/disk_cache/disk_cache_test_util.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 24 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
| 25 | 27 |
| 26 using base::Time; | 28 using base::Time; |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 32 class DiskCachePerfTest : public DiskCacheTest { |
| 33 protected: |
| 34 void CacheBackendPerformance(net::BackendType backend_type); |
| 35 }; |
| 36 |
| 30 struct TestEntry { | 37 struct TestEntry { |
| 31 std::string key; | 38 std::string key; |
| 32 int data_len; | 39 int data_len; |
| 33 }; | 40 }; |
| 34 typedef std::vector<TestEntry> TestEntries; | 41 typedef std::vector<TestEntry> TestEntries; |
| 35 | 42 |
| 36 const int kMaxSize = 16 * 1024 - 1; | 43 const int kMaxSize = 16 * 1024 - 1; |
| 37 | 44 |
| 38 // Creates num_entries on the cache, and writes 200 bytes of metadata and up | 45 // Creates num_entries on the cache, and writes 200 bytes of metadata and up |
| 39 // to kMaxSize of data to each entry. | 46 // to kMaxSize of data to each entry. |
| 40 bool TimeWrite(int num_entries, disk_cache::Backend* cache, | 47 bool TimeWrite(int num_entries, |
| 41 TestEntries* entries) { | 48 disk_cache::Backend* cache, |
| 49 TestEntries* entries) { |
| 42 const int kSize1 = 200; | 50 const int kSize1 = 200; |
| 43 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); | 51 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); |
| 44 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); | 52 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); |
| 45 | 53 |
| 46 CacheTestFillBuffer(buffer1->data(), kSize1, false); | 54 CacheTestFillBuffer(buffer1->data(), kSize1, false); |
| 47 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); | 55 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); |
| 48 | 56 |
| 49 int expected = 0; | 57 int expected = 0; |
| 50 | 58 |
| 51 MessageLoopHelper helper; | 59 MessageLoopHelper helper; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 82 cache_entry->Close(); | 90 cache_entry->Close(); |
| 83 } | 91 } |
| 84 | 92 |
| 85 helper.WaitUntilCacheIoFinished(expected); | 93 helper.WaitUntilCacheIoFinished(expected); |
| 86 timer.Done(); | 94 timer.Done(); |
| 87 | 95 |
| 88 return (expected == helper.callbacks_called()); | 96 return (expected == helper.callbacks_called()); |
| 89 } | 97 } |
| 90 | 98 |
| 91 // Reads the data and metadata from each entry listed on |entries|. | 99 // Reads the data and metadata from each entry listed on |entries|. |
| 92 bool TimeRead(int num_entries, disk_cache::Backend* cache, | 100 bool TimeRead(int num_entries, |
| 93 const TestEntries& entries, bool cold) { | 101 disk_cache::Backend* cache, |
| 102 const TestEntries& entries, |
| 103 bool cold) { |
| 94 const int kSize1 = 200; | 104 const int kSize1 = 200; |
| 95 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); | 105 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); |
| 96 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); | 106 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); |
| 97 | 107 |
| 98 CacheTestFillBuffer(buffer1->data(), kSize1, false); | 108 CacheTestFillBuffer(buffer1->data(), kSize1, false); |
| 99 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); | 109 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); |
| 100 | 110 |
| 101 int expected = 0; | 111 int expected = 0; |
| 102 | 112 |
| 103 MessageLoopHelper helper; | 113 MessageLoopHelper helper; |
| 104 CallbackTest callback(&helper, true); | 114 CallbackTest callback(&helper, true); |
| 105 | 115 |
| 106 const char* message = cold ? "Read disk cache entries (cold)" : | 116 const char* message = cold ? "Read disk cache entries (cold)" |
| 107 "Read disk cache entries (warm)"; | 117 : "Read disk cache entries (warm)"; |
| 108 base::PerfTimeLogger timer(message); | 118 base::PerfTimeLogger timer(message); |
| 109 | 119 |
| 110 for (int i = 0; i < num_entries; i++) { | 120 for (int i = 0; i < num_entries; i++) { |
| 111 disk_cache::Entry* cache_entry; | 121 disk_cache::Entry* cache_entry; |
| 112 net::TestCompletionCallback cb; | 122 net::TestCompletionCallback cb; |
| 113 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback()); | 123 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback()); |
| 114 if (net::OK != cb.GetResult(rv)) | 124 if (net::OK != cb.GetResult(rv)) |
| 115 break; | 125 break; |
| 116 int ret = cache_entry->ReadData( | 126 int ret = cache_entry->ReadData( |
| 117 0, 0, buffer1.get(), kSize1, | 127 0, 0, buffer1.get(), kSize1, |
| (...skipping 17 matching lines...) Expand all Loading... |
| 135 timer.Done(); | 145 timer.Done(); |
| 136 | 146 |
| 137 return (expected == helper.callbacks_called()); | 147 return (expected == helper.callbacks_called()); |
| 138 } | 148 } |
| 139 | 149 |
| 140 int BlockSize() { | 150 int BlockSize() { |
| 141 // We can use form 1 to 4 blocks. | 151 // We can use form 1 to 4 blocks. |
| 142 return (rand() & 0x3) + 1; | 152 return (rand() & 0x3) + 1; |
| 143 } | 153 } |
| 144 | 154 |
| 145 } // namespace | 155 TEST_F(DiskCachePerfTest, BlockfileHashes) { |
| 146 | |
| 147 TEST_F(DiskCacheTest, Hash) { | |
| 148 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 156 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 149 srand(seed); | 157 srand(seed); |
| 150 | 158 |
| 151 base::PerfTimeLogger timer("Hash disk cache keys"); | 159 base::PerfTimeLogger timer("Hash disk cache keys"); |
| 152 for (int i = 0; i < 300000; i++) { | 160 for (int i = 0; i < 300000; i++) { |
| 153 std::string key = GenerateKey(true); | 161 std::string key = GenerateKey(true); |
| 154 base::Hash(key); | 162 base::Hash(key); |
| 155 } | 163 } |
| 156 timer.Done(); | 164 timer.Done(); |
| 157 } | 165 } |
| 158 | 166 |
| 159 TEST_F(DiskCacheTest, CacheBackendPerformance) { | 167 void DiskCachePerfTest::CacheBackendPerformance(net::BackendType backend_type) { |
| 160 base::Thread cache_thread("CacheThread"); | 168 base::Thread cache_thread("CacheThread"); |
| 161 ASSERT_TRUE(cache_thread.StartWithOptions( | 169 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 162 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 170 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 163 | 171 |
| 164 ASSERT_TRUE(CleanupCacheDir()); | 172 ASSERT_TRUE(CleanupCacheDir()); |
| 165 net::TestCompletionCallback cb; | 173 net::TestCompletionCallback cb; |
| 166 std::unique_ptr<disk_cache::Backend> cache; | 174 std::unique_ptr<disk_cache::Backend> cache; |
| 167 int rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 175 int rv = disk_cache::CreateCacheBackend( |
| 168 net::CACHE_BACKEND_BLOCKFILE, | 176 net::DISK_CACHE, backend_type, cache_path_, 0, false, |
| 169 cache_path_, | 177 cache_thread.task_runner(), NULL, &cache, cb.callback()); |
| 170 0, | |
| 171 false, | |
| 172 cache_thread.task_runner(), | |
| 173 NULL, | |
| 174 &cache, | |
| 175 cb.callback()); | |
| 176 | 178 |
| 177 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 179 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 178 | 180 |
| 179 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 181 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 180 srand(seed); | 182 srand(seed); |
| 181 | 183 |
| 182 TestEntries entries; | 184 TestEntries entries; |
| 183 int num_entries = 1000; | 185 int num_entries = 1000; |
| 184 | 186 |
| 185 EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries)); | 187 EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries)); |
| 186 | 188 |
| 187 base::MessageLoop::current()->RunUntilIdle(); | 189 base::MessageLoop::current()->RunUntilIdle(); |
| 188 cache.reset(); | 190 cache.reset(); |
| 189 | 191 |
| 190 ASSERT_TRUE(base::EvictFileFromSystemCache( | 192 // Flush all files in the cache out of system memory. |
| 191 cache_path_.AppendASCII("index"))); | 193 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*"); |
| 192 ASSERT_TRUE(base::EvictFileFromSystemCache( | 194 base::FileEnumerator enumerator(cache_path_, true /* recursive */, |
| 193 cache_path_.AppendASCII("data_0"))); | 195 base::FileEnumerator::FILES, file_pattern); |
| 194 ASSERT_TRUE(base::EvictFileFromSystemCache( | 196 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
| 195 cache_path_.AppendASCII("data_1"))); | 197 file_path = enumerator.Next()) { |
| 196 ASSERT_TRUE(base::EvictFileFromSystemCache( | 198 ASSERT_TRUE(base::EvictFileFromSystemCache(file_path)); |
| 197 cache_path_.AppendASCII("data_2"))); | 199 } |
| 198 ASSERT_TRUE(base::EvictFileFromSystemCache( | 200 // And, cache directories. |
| 199 cache_path_.AppendASCII("data_3"))); | 201 if (backend_type == net::CACHE_BACKEND_SIMPLE) { |
| 202 ASSERT_TRUE( |
| 203 base::EvictFileFromSystemCache(cache_path_.AppendASCII("index-dir"))); |
| 204 } |
| 205 ASSERT_TRUE(base::EvictFileFromSystemCache(cache_path_)); |
| 200 | 206 |
| 201 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, | 207 rv = disk_cache::CreateCacheBackend( |
| 202 net::CACHE_BACKEND_BLOCKFILE, | 208 net::DISK_CACHE, backend_type, cache_path_, 0, false, |
| 203 cache_path_, | 209 cache_thread.task_runner(), NULL, &cache, cb.callback()); |
| 204 0, | |
| 205 false, | |
| 206 cache_thread.task_runner(), | |
| 207 NULL, | |
| 208 &cache, | |
| 209 cb.callback()); | |
| 210 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 210 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
| 211 | 211 |
| 212 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true)); | 212 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true)); |
| 213 | 213 |
| 214 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false)); | 214 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false)); |
| 215 | 215 |
| 216 base::MessageLoop::current()->RunUntilIdle(); | 216 base::MessageLoop::current()->RunUntilIdle(); |
| 217 } | 217 } |
| 218 | 218 |
| 219 TEST_F(DiskCachePerfTest, CacheBackendPerformance) { |
| 220 CacheBackendPerformance(net::CACHE_BACKEND_BLOCKFILE); |
| 221 } |
| 222 |
| 223 TEST_F(DiskCachePerfTest, SimpleCacheBackendPerformance) { |
| 224 CacheBackendPerformance(net::CACHE_BACKEND_SIMPLE); |
| 225 } |
| 226 |
| 219 // Creating and deleting "entries" on a block-file is something quite frequent | 227 // Creating and deleting "entries" on a block-file is something quite frequent |
| 220 // (after all, almost everything is stored on block files). The operation is | 228 // (after all, almost everything is stored on block files). The operation is |
| 221 // almost free when the file is empty, but can be expensive if the file gets | 229 // almost free when the file is empty, but can be expensive if the file gets |
| 222 // fragmented, or if we have multiple files. This test measures that scenario, | 230 // fragmented, or if we have multiple files. This test measures that scenario, |
| 223 // by using multiple, highly fragmented files. | 231 // by using multiple, highly fragmented files. |
| 224 TEST_F(DiskCacheTest, BlockFilesPerformance) { | 232 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { |
| 225 ASSERT_TRUE(CleanupCacheDir()); | 233 ASSERT_TRUE(CleanupCacheDir()); |
| 226 | 234 |
| 227 disk_cache::BlockFiles files(cache_path_); | 235 disk_cache::BlockFiles files(cache_path_); |
| 228 ASSERT_TRUE(files.Init(true)); | 236 ASSERT_TRUE(files.Init(true)); |
| 229 | 237 |
| 230 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 238 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 231 srand(seed); | 239 srand(seed); |
| 232 | 240 |
| 233 const int kNumEntries = 60000; | 241 const int kNumEntries = 60000; |
| 234 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; | 242 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; |
| 235 | 243 |
| 236 base::PerfTimeLogger timer1("Fill three block-files"); | 244 base::PerfTimeLogger timer1("Fill three block-files"); |
| 237 | 245 |
| 238 // Fill up the 32-byte block file (use three files). | 246 // Fill up the 32-byte block file (use three files). |
| 239 for (int i = 0; i < kNumEntries; i++) { | 247 for (int i = 0; i < kNumEntries; i++) { |
| 240 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 248 EXPECT_TRUE( |
| 241 &address[i])); | 249 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[i])); |
| 242 } | 250 } |
| 243 | 251 |
| 244 timer1.Done(); | 252 timer1.Done(); |
| 245 base::PerfTimeLogger timer2("Create and delete blocks"); | 253 base::PerfTimeLogger timer2("Create and delete blocks"); |
| 246 | 254 |
| 247 for (int i = 0; i < 200000; i++) { | 255 for (int i = 0; i < 200000; i++) { |
| 248 int entry = rand() * (kNumEntries / RAND_MAX + 1); | 256 int entry = rand() * (kNumEntries / RAND_MAX + 1); |
| 249 if (entry >= kNumEntries) | 257 if (entry >= kNumEntries) |
| 250 entry = 0; | 258 entry = 0; |
| 251 | 259 |
| 252 files.DeleteBlock(address[entry], false); | 260 files.DeleteBlock(address[entry], false); |
| 253 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 261 EXPECT_TRUE( |
| 254 &address[entry])); | 262 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry])); |
| 255 } | 263 } |
| 256 | 264 |
| 257 timer2.Done(); | 265 timer2.Done(); |
| 258 base::MessageLoop::current()->RunUntilIdle(); | 266 base::MessageLoop::current()->RunUntilIdle(); |
| 259 delete[] address; | 267 delete[] address; |
| 260 } | 268 } |
| 269 |
| 270 } // namespace |
| OLD | NEW |