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/basictypes.h" | 7 #include "base/basictypes.h" |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | |
10 #include "base/perftimer.h" | 8 #include "base/perftimer.h" |
11 #include "base/string_util.h" | 9 #include "base/string_util.h" |
12 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
13 #include "base/test/test_file_util.h" | 11 #include "base/test/test_file_util.h" |
14 #include "base/timer.h" | 12 #include "base/timer.h" |
15 #include "net/base/io_buffer.h" | 13 #include "net/base/io_buffer.h" |
16 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
17 #include "net/base/test_completion_callback.h" | 15 #include "net/base/test_completion_callback.h" |
18 #include "net/disk_cache/block_files.h" | 16 #include "net/disk_cache/block_files.h" |
| 17 #include "net/disk_cache/backend_impl.h" |
19 #include "net/disk_cache/disk_cache.h" | 18 #include "net/disk_cache/disk_cache.h" |
| 19 #include "net/disk_cache/disk_cache_test_base.h" |
20 #include "net/disk_cache/disk_cache_test_util.h" | 20 #include "net/disk_cache/disk_cache_test_util.h" |
21 #include "net/disk_cache/hash.h" | 21 #include "net/disk_cache/hash.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 22 #include "testing/gtest/include/gtest/gtest.h" |
23 #include "testing/platform_test.h" | 23 #include "testing/platform_test.h" |
24 | 24 |
25 using base::Time; | 25 using base::Time; |
26 | 26 |
27 typedef PlatformTest DiskCacheTest; | |
28 | |
29 namespace { | 27 namespace { |
30 | 28 |
31 struct TestEntry { | 29 struct TestEntry { |
32 std::string key; | 30 std::string key; |
33 int data_len; | 31 int data_len; |
34 }; | 32 }; |
35 typedef std::vector<TestEntry> TestEntries; | 33 typedef std::vector<TestEntry> TestEntries; |
36 | 34 |
37 const int kMaxSize = 16 * 1024 - 1; | 35 const int kMaxSize = 16 * 1024 - 1; |
38 | 36 |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 | 142 |
145 PerfTimeLogger timer("Hash disk cache keys"); | 143 PerfTimeLogger timer("Hash disk cache keys"); |
146 for (int i = 0; i < 300000; i++) { | 144 for (int i = 0; i < 300000; i++) { |
147 std::string key = GenerateKey(true); | 145 std::string key = GenerateKey(true); |
148 disk_cache::Hash(key); | 146 disk_cache::Hash(key); |
149 } | 147 } |
150 timer.Done(); | 148 timer.Done(); |
151 } | 149 } |
152 | 150 |
153 TEST_F(DiskCacheTest, CacheBackendPerformance) { | 151 TEST_F(DiskCacheTest, CacheBackendPerformance) { |
154 MessageLoopForIO message_loop; | |
155 | |
156 base::Thread cache_thread("CacheThread"); | 152 base::Thread cache_thread("CacheThread"); |
157 ASSERT_TRUE(cache_thread.StartWithOptions( | 153 ASSERT_TRUE(cache_thread.StartWithOptions( |
158 base::Thread::Options(MessageLoop::TYPE_IO, 0))); | 154 base::Thread::Options(MessageLoop::TYPE_IO, 0))); |
159 | 155 |
160 ScopedTestCache test_cache; | 156 ASSERT_TRUE(CleanupCacheDir()); |
161 TestOldCompletionCallback cb; | 157 TestOldCompletionCallback cb; |
162 disk_cache::Backend* cache; | 158 disk_cache::Backend* cache; |
163 int rv = disk_cache::CreateCacheBackend( | 159 int rv = disk_cache::CreateCacheBackend( |
164 net::DISK_CACHE, test_cache.path(), 0, false, | 160 net::DISK_CACHE, cache_path_, 0, false, |
165 cache_thread.message_loop_proxy(), NULL, &cache, &cb); | 161 cache_thread.message_loop_proxy(), NULL, &cache, &cb); |
166 | 162 |
167 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 163 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
168 | 164 |
169 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 165 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
170 srand(seed); | 166 srand(seed); |
171 | 167 |
172 TestEntries entries; | 168 TestEntries entries; |
173 int num_entries = 1000; | 169 int num_entries = 1000; |
174 | 170 |
175 EXPECT_TRUE(TimeWrite(num_entries, cache, &entries)); | 171 EXPECT_TRUE(TimeWrite(num_entries, cache, &entries)); |
176 | 172 |
177 MessageLoop::current()->RunAllPending(); | 173 MessageLoop::current()->RunAllPending(); |
178 delete cache; | 174 delete cache; |
179 | 175 |
180 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 176 ASSERT_TRUE(file_util::EvictFileFromSystemCache( |
181 test_cache.path().AppendASCII("index"))); | 177 cache_path_.AppendASCII("index"))); |
182 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 178 ASSERT_TRUE(file_util::EvictFileFromSystemCache( |
183 test_cache.path().AppendASCII("data_0"))); | 179 cache_path_.AppendASCII("data_0"))); |
184 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 180 ASSERT_TRUE(file_util::EvictFileFromSystemCache( |
185 test_cache.path().AppendASCII("data_1"))); | 181 cache_path_.AppendASCII("data_1"))); |
186 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 182 ASSERT_TRUE(file_util::EvictFileFromSystemCache( |
187 test_cache.path().AppendASCII("data_2"))); | 183 cache_path_.AppendASCII("data_2"))); |
188 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 184 ASSERT_TRUE(file_util::EvictFileFromSystemCache( |
189 test_cache.path().AppendASCII("data_3"))); | 185 cache_path_.AppendASCII("data_3"))); |
190 | 186 |
191 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, test_cache.path(), 0, | 187 rv = disk_cache::CreateCacheBackend(net::DISK_CACHE, cache_path_, 0, |
192 false, cache_thread.message_loop_proxy(), | 188 false, cache_thread.message_loop_proxy(), |
193 NULL, &cache, &cb); | 189 NULL, &cache, &cb); |
194 ASSERT_EQ(net::OK, cb.GetResult(rv)); | 190 ASSERT_EQ(net::OK, cb.GetResult(rv)); |
195 | 191 |
196 EXPECT_TRUE(TimeRead(num_entries, cache, entries, true)); | 192 EXPECT_TRUE(TimeRead(num_entries, cache, entries, true)); |
197 | 193 |
198 EXPECT_TRUE(TimeRead(num_entries, cache, entries, false)); | 194 EXPECT_TRUE(TimeRead(num_entries, cache, entries, false)); |
199 | 195 |
200 MessageLoop::current()->RunAllPending(); | 196 MessageLoop::current()->RunAllPending(); |
201 delete cache; | 197 delete cache; |
202 } | 198 } |
203 | 199 |
204 // Creating and deleting "entries" on a block-file is something quite frequent | 200 // Creating and deleting "entries" on a block-file is something quite frequent |
205 // (after all, almost everything is stored on block files). The operation is | 201 // (after all, almost everything is stored on block files). The operation is |
206 // almost free when the file is empty, but can be expensive if the file gets | 202 // almost free when the file is empty, but can be expensive if the file gets |
207 // fragmented, or if we have multiple files. This test measures that scenario, | 203 // fragmented, or if we have multiple files. This test measures that scenario, |
208 // by using multiple, highly fragmented files. | 204 // by using multiple, highly fragmented files. |
209 TEST_F(DiskCacheTest, BlockFilesPerformance) { | 205 TEST_F(DiskCacheTest, BlockFilesPerformance) { |
210 MessageLoopForIO message_loop; | 206 ASSERT_TRUE(CleanupCacheDir()); |
211 | 207 |
212 ScopedTestCache test_cache; | 208 disk_cache::BlockFiles files(cache_path_); |
213 | |
214 disk_cache::BlockFiles files(test_cache.path()); | |
215 ASSERT_TRUE(files.Init(true)); | 209 ASSERT_TRUE(files.Init(true)); |
216 | 210 |
217 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 211 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
218 srand(seed); | 212 srand(seed); |
219 | 213 |
220 const int kNumEntries = 60000; | 214 const int kNumEntries = 60000; |
221 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; | 215 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; |
222 | 216 |
223 PerfTimeLogger timer1("Fill three block-files"); | 217 PerfTimeLogger timer1("Fill three block-files"); |
224 | 218 |
(...skipping 13 matching lines...) Expand all Loading... |
238 | 232 |
239 files.DeleteBlock(address[entry], false); | 233 files.DeleteBlock(address[entry], false); |
240 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 234 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
241 &address[entry])); | 235 &address[entry])); |
242 } | 236 } |
243 | 237 |
244 timer2.Done(); | 238 timer2.Done(); |
245 MessageLoop::current()->RunAllPending(); | 239 MessageLoop::current()->RunAllPending(); |
246 delete[] address; | 240 delete[] address; |
247 } | 241 } |
OLD | NEW |