| 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" | 9 #include "base/files/file_enumerator.h" |
| 10 #include "base/files/file_path.h" | 10 #include "base/files/file_path.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "net/disk_cache/disk_cache.h" | 22 #include "net/disk_cache/disk_cache.h" |
| 23 #include "net/disk_cache/disk_cache_test_base.h" | 23 #include "net/disk_cache/disk_cache_test_base.h" |
| 24 #include "net/disk_cache/disk_cache_test_util.h" | 24 #include "net/disk_cache/disk_cache_test_util.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
| 26 #include "testing/platform_test.h" | 26 #include "testing/platform_test.h" |
| 27 | 27 |
| 28 using base::Time; | 28 using base::Time; |
| 29 | 29 |
| 30 namespace { | 30 namespace { |
| 31 | 31 |
| 32 class DiskCachePerfTest : public DiskCacheTest { | 32 class DiskCachePerfTest : public DiskCacheTestWithCache { |
| 33 protected: | 33 protected: |
| 34 void CacheBackendPerformance(net::BackendType backend_type); | 34 void CacheBackendPerformance(); |
| 35 }; | 35 }; |
| 36 | 36 |
| 37 struct TestEntry { | 37 struct TestEntry { |
| 38 std::string key; | 38 std::string key; |
| 39 int data_len; | 39 int data_len; |
| 40 }; | 40 }; |
| 41 typedef std::vector<TestEntry> TestEntries; | 41 typedef std::vector<TestEntry> TestEntries; |
| 42 | 42 |
| 43 const int kMaxSize = 16 * 1024 - 1; | 43 const int kMaxSize = 16 * 1024 - 1; |
| 44 | 44 |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 srand(seed); | 157 srand(seed); |
| 158 | 158 |
| 159 base::PerfTimeLogger timer("Hash disk cache keys"); | 159 base::PerfTimeLogger timer("Hash disk cache keys"); |
| 160 for (int i = 0; i < 300000; i++) { | 160 for (int i = 0; i < 300000; i++) { |
| 161 std::string key = GenerateKey(true); | 161 std::string key = GenerateKey(true); |
| 162 base::Hash(key); | 162 base::Hash(key); |
| 163 } | 163 } |
| 164 timer.Done(); | 164 timer.Done(); |
| 165 } | 165 } |
| 166 | 166 |
| 167 void DiskCachePerfTest::CacheBackendPerformance(net::BackendType backend_type) { | 167 void DiskCachePerfTest::CacheBackendPerformance() { |
| 168 base::Thread cache_thread("CacheThread"); | 168 base::Thread cache_thread("CacheThread"); |
| 169 ASSERT_TRUE(cache_thread.StartWithOptions( | 169 ASSERT_TRUE(cache_thread.StartWithOptions( |
| 170 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); | 170 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); |
| 171 | 171 |
| 172 ASSERT_TRUE(CleanupCacheDir()); | |
| 173 net::TestCompletionCallback cb; | |
| 174 std::unique_ptr<disk_cache::Backend> cache; | |
| 175 int rv = disk_cache::CreateCacheBackend( | |
| 176 net::DISK_CACHE, backend_type, cache_path_, 0, false, | |
| 177 cache_thread.task_runner(), NULL, &cache, cb.callback()); | |
| 178 | |
| 179 ASSERT_EQ(net::OK, cb.GetResult(rv)); | |
| 180 | |
| 181 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 172 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
| 182 srand(seed); | 173 srand(seed); |
| 183 | 174 |
| 175 InitCache(); |
| 176 |
| 184 TestEntries entries; | 177 TestEntries entries; |
| 185 int num_entries = 1000; | 178 int num_entries = 1000; |
| 186 | 179 |
| 187 EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries)); | 180 EXPECT_TRUE(TimeWrite(num_entries, cache_.get(), &entries)); |
| 188 | 181 |
| 189 base::MessageLoop::current()->RunUntilIdle(); | 182 base::MessageLoop::current()->RunUntilIdle(); |
| 190 cache.reset(); | 183 cache_.reset(); |
| 191 | 184 |
| 192 // Flush all files in the cache out of system memory. | 185 // Flush all files in the cache out of system memory. |
| 193 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*"); | 186 const base::FilePath::StringType file_pattern = FILE_PATH_LITERAL("*"); |
| 194 base::FileEnumerator enumerator(cache_path_, true /* recursive */, | 187 base::FileEnumerator enumerator(cache_path_, true /* recursive */, |
| 195 base::FileEnumerator::FILES, file_pattern); | 188 base::FileEnumerator::FILES, file_pattern); |
| 196 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); | 189 for (base::FilePath file_path = enumerator.Next(); !file_path.empty(); |
| 197 file_path = enumerator.Next()) { | 190 file_path = enumerator.Next()) { |
| 198 ASSERT_TRUE(base::EvictFileFromSystemCache(file_path)); | 191 ASSERT_TRUE(base::EvictFileFromSystemCache(file_path)); |
| 199 } | 192 } |
| 200 // And, cache directories. | 193 // And, cache directories. |
| 201 if (backend_type == net::CACHE_BACKEND_SIMPLE) { | 194 if (simple_cache_mode_) { |
| 202 ASSERT_TRUE( | 195 ASSERT_TRUE( |
| 203 base::EvictFileFromSystemCache(cache_path_.AppendASCII("index-dir"))); | 196 base::EvictFileFromSystemCache(cache_path_.AppendASCII("index-dir"))); |
| 204 } | 197 } |
| 205 ASSERT_TRUE(base::EvictFileFromSystemCache(cache_path_)); | 198 ASSERT_TRUE(base::EvictFileFromSystemCache(cache_path_)); |
| 206 | 199 |
| 207 rv = disk_cache::CreateCacheBackend( | 200 DisableFirstCleanup(); |
| 208 net::DISK_CACHE, backend_type, cache_path_, 0, false, | 201 InitCache(); |
| 209 cache_thread.task_runner(), NULL, &cache, cb.callback()); | |
| 210 ASSERT_EQ(net::OK, cb.GetResult(rv)); | |
| 211 | 202 |
| 212 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true)); | 203 EXPECT_TRUE(TimeRead(num_entries, cache_.get(), entries, true)); |
| 213 | 204 EXPECT_TRUE(TimeRead(num_entries, cache_.get(), entries, false)); |
| 214 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false)); | |
| 215 | |
| 216 base::MessageLoop::current()->RunUntilIdle(); | 205 base::MessageLoop::current()->RunUntilIdle(); |
| 217 } | 206 } |
| 218 | 207 |
| 219 TEST_F(DiskCachePerfTest, CacheBackendPerformance) { | 208 TEST_F(DiskCachePerfTest, CacheBackendPerformance) { |
| 220 CacheBackendPerformance(net::CACHE_BACKEND_BLOCKFILE); | 209 CacheBackendPerformance(); |
| 221 } | 210 } |
| 222 | 211 |
| 223 TEST_F(DiskCachePerfTest, SimpleCacheBackendPerformance) { | 212 TEST_F(DiskCachePerfTest, SimpleCacheBackendPerformance) { |
| 224 CacheBackendPerformance(net::CACHE_BACKEND_SIMPLE); | 213 SetSimpleCacheMode(); |
| 214 CacheBackendPerformance(); |
| 225 } | 215 } |
| 226 | 216 |
| 227 // Creating and deleting "entries" on a block-file is something quite frequent | 217 // Creating and deleting "entries" on a block-file is something quite frequent |
| 228 // (after all, almost everything is stored on block files). The operation is | 218 // (after all, almost everything is stored on block files). The operation is |
| 229 // almost free when the file is empty, but can be expensive if the file gets | 219 // almost free when the file is empty, but can be expensive if the file gets |
| 230 // fragmented, or if we have multiple files. This test measures that scenario, | 220 // fragmented, or if we have multiple files. This test measures that scenario, |
| 231 // by using multiple, highly fragmented files. | 221 // by using multiple, highly fragmented files. |
| 232 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { | 222 TEST_F(DiskCachePerfTest, BlockFilesPerformance) { |
| 233 ASSERT_TRUE(CleanupCacheDir()); | 223 ASSERT_TRUE(CleanupCacheDir()); |
| 234 | 224 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 261 EXPECT_TRUE( | 251 EXPECT_TRUE( |
| 262 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry])); | 252 files.CreateBlock(disk_cache::RANKINGS, BlockSize(), &address[entry])); |
| 263 } | 253 } |
| 264 | 254 |
| 265 timer2.Done(); | 255 timer2.Done(); |
| 266 base::MessageLoop::current()->RunUntilIdle(); | 256 base::MessageLoop::current()->RunUntilIdle(); |
| 267 delete[] address; | 257 delete[] address; |
| 268 } | 258 } |
| 269 | 259 |
| 270 } // namespace | 260 } // namespace |
| OLD | NEW |