OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <fcntl.h> | 5 #include <fcntl.h> |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
11 #include "base/perftimer.h" | 11 #include "base/perftimer.h" |
| 12 #include "base/platform_test.h" |
12 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
13 #include "base/scoped_handle.h" | 14 #include "base/scoped_handle.h" |
14 #endif | 15 #endif |
15 #include "base/string_util.h" | 16 #include "base/string_util.h" |
16 #include "base/timer.h" | 17 #include "base/timer.h" |
17 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
18 #include "net/disk_cache/block_files.h" | 19 #include "net/disk_cache/block_files.h" |
19 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
20 #include "net/disk_cache/disk_cache_test_base.h" | |
21 #include "net/disk_cache/disk_cache_test_util.h" | 21 #include "net/disk_cache/disk_cache_test_util.h" |
22 #include "net/disk_cache/hash.h" | 22 #include "net/disk_cache/hash.h" |
23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
24 | 24 |
25 using base::Time; | 25 using base::Time; |
26 | 26 |
27 extern int g_cache_tests_max_id; | 27 extern int g_cache_tests_max_id; |
28 extern volatile int g_cache_tests_received; | 28 extern volatile int g_cache_tests_received; |
29 extern volatile bool g_cache_tests_error; | 29 extern volatile bool g_cache_tests_error; |
30 | 30 |
| 31 typedef PlatformTest DiskCacheTest; |
| 32 |
31 namespace { | 33 namespace { |
32 | 34 |
33 bool EvictFileFromSystemCache(const wchar_t* name) { | 35 bool EvictFileFromSystemCache(const wchar_t* name) { |
34 #if defined(OS_WIN) | 36 #if defined(OS_WIN) |
35 // Overwrite it with no buffering. | 37 // Overwrite it with no buffering. |
36 ScopedHandle file(CreateFile(name, GENERIC_READ | GENERIC_WRITE, | 38 ScopedHandle file(CreateFile(name, GENERIC_READ | GENERIC_WRITE, |
37 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 39 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
38 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); | 40 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); |
39 if (!file.IsValid()) | 41 if (!file.IsValid()) |
40 return false; | 42 return false; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
219 | 221 |
220 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 222 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
221 srand(seed); | 223 srand(seed); |
222 | 224 |
223 TestEntries entries; | 225 TestEntries entries; |
224 int num_entries = 1000; | 226 int num_entries = 1000; |
225 | 227 |
226 int ret = TimeWrite(num_entries, cache, &entries); | 228 int ret = TimeWrite(num_entries, cache, &entries); |
227 EXPECT_EQ(ret, g_cache_tests_received); | 229 EXPECT_EQ(ret, g_cache_tests_received); |
228 | 230 |
| 231 MessageLoop::current()->RunAllPending(); |
229 delete cache; | 232 delete cache; |
230 | 233 |
231 std::wstring filename(path); | 234 std::wstring filename(path); |
232 file_util::AppendToPath(&filename, L"index"); | 235 file_util::AppendToPath(&filename, L"index"); |
233 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); | 236 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
234 | 237 |
235 filename = path; | 238 filename = path; |
236 file_util::AppendToPath(&filename, L"data_0"); | 239 file_util::AppendToPath(&filename, L"data_0"); |
237 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); | 240 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
238 | 241 |
(...skipping 11 matching lines...) Expand all Loading... |
250 | 253 |
251 cache = disk_cache::CreateCacheBackend(path, false, 0); | 254 cache = disk_cache::CreateCacheBackend(path, false, 0); |
252 ASSERT_TRUE(NULL != cache); | 255 ASSERT_TRUE(NULL != cache); |
253 | 256 |
254 ret = TimeRead(num_entries, cache, entries, true); | 257 ret = TimeRead(num_entries, cache, entries, true); |
255 EXPECT_EQ(ret, g_cache_tests_received); | 258 EXPECT_EQ(ret, g_cache_tests_received); |
256 | 259 |
257 ret = TimeRead(num_entries, cache, entries, false); | 260 ret = TimeRead(num_entries, cache, entries, false); |
258 EXPECT_EQ(ret, g_cache_tests_received); | 261 EXPECT_EQ(ret, g_cache_tests_received); |
259 | 262 |
| 263 MessageLoop::current()->RunAllPending(); |
260 delete cache; | 264 delete cache; |
261 } | 265 } |
262 | 266 |
263 // Creating and deleting "entries" on a block-file is something quite frequent | 267 // Creating and deleting "entries" on a block-file is something quite frequent |
264 // (after all, almost everything is stored on block files). The operation is | 268 // (after all, almost everything is stored on block files). The operation is |
265 // almost free when the file is empty, but can be expensive if the file gets | 269 // almost free when the file is empty, but can be expensive if the file gets |
266 // fragmented, or if we have multiple files. This test measures that scenario, | 270 // fragmented, or if we have multiple files. This test measures that scenario, |
267 // by using multiple, highly fragmented files. | 271 // by using multiple, highly fragmented files. |
268 TEST_F(DiskCacheTest, BlockFilesPerformance) { | 272 TEST_F(DiskCacheTest, BlockFilesPerformance) { |
| 273 MessageLoopForIO message_loop; |
269 std::wstring path = GetCachePath(); | 274 std::wstring path = GetCachePath(); |
270 ASSERT_TRUE(DeleteCache(path.c_str())); | 275 ASSERT_TRUE(DeleteCache(path.c_str())); |
271 | 276 |
272 disk_cache::BlockFiles files(path); | 277 disk_cache::BlockFiles files(path); |
273 ASSERT_TRUE(files.Init(true)); | 278 ASSERT_TRUE(files.Init(true)); |
274 | 279 |
275 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 280 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
276 srand(seed); | 281 srand(seed); |
277 | 282 |
278 const int kNumEntries = 60000; | 283 const int kNumEntries = 60000; |
(...skipping 17 matching lines...) Expand all Loading... |
296 int entry = rand() * (kNumEntries / RAND_MAX + 1); | 301 int entry = rand() * (kNumEntries / RAND_MAX + 1); |
297 if (entry >= kNumEntries) | 302 if (entry >= kNumEntries) |
298 entry = 0; | 303 entry = 0; |
299 | 304 |
300 files.DeleteBlock(address[entry], false); | 305 files.DeleteBlock(address[entry], false); |
301 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 306 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
302 &address[entry])); | 307 &address[entry])); |
303 } | 308 } |
304 | 309 |
305 timer2.Done(); | 310 timer2.Done(); |
| 311 MessageLoop::current()->RunAllPending(); |
306 } | 312 } |
OLD | NEW |