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> |
| 6 |
5 #include <string> | 7 #include <string> |
6 | 8 |
7 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
8 #include "base/file_path.h" | |
9 #include "base/file_util.h" | 10 #include "base/file_util.h" |
10 #include "base/perftimer.h" | 11 #include "base/perftimer.h" |
11 #include "base/platform_test.h" | 12 #include "base/platform_test.h" |
| 13 #if defined(OS_WIN) |
| 14 #include "base/scoped_handle.h" |
| 15 #endif |
12 #include "base/string_util.h" | 16 #include "base/string_util.h" |
13 #include "base/timer.h" | 17 #include "base/timer.h" |
14 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
15 #include "net/disk_cache/block_files.h" | 19 #include "net/disk_cache/block_files.h" |
16 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
17 #include "net/disk_cache/disk_cache_test_util.h" | 21 #include "net/disk_cache/disk_cache_test_util.h" |
18 #include "net/disk_cache/hash.h" | 22 #include "net/disk_cache/hash.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
20 | 24 |
21 using base::Time; | 25 using base::Time; |
22 | 26 |
23 extern int g_cache_tests_max_id; | 27 extern int g_cache_tests_max_id; |
24 extern volatile int g_cache_tests_received; | 28 extern volatile int g_cache_tests_received; |
25 extern volatile bool g_cache_tests_error; | 29 extern volatile bool g_cache_tests_error; |
26 | 30 |
27 typedef PlatformTest DiskCacheTest; | 31 typedef PlatformTest DiskCacheTest; |
28 | 32 |
29 namespace { | 33 namespace { |
30 | 34 |
| 35 bool EvictFileFromSystemCache(const wchar_t* name) { |
| 36 #if defined(OS_WIN) |
| 37 // Overwrite it with no buffering. |
| 38 ScopedHandle file(CreateFile(name, GENERIC_READ | GENERIC_WRITE, |
| 39 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
| 40 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); |
| 41 if (!file.IsValid()) |
| 42 return false; |
| 43 |
| 44 // Execute in chunks. It could be optimized. We want to do few of these since |
| 45 // these opterations will be slow without the cache. |
| 46 char buffer[128 * 1024]; |
| 47 int total_bytes = 0; |
| 48 DWORD bytes_read; |
| 49 for (;;) { |
| 50 if (!ReadFile(file, buffer, sizeof(buffer), &bytes_read, NULL)) |
| 51 return false; |
| 52 if (bytes_read == 0) |
| 53 break; |
| 54 |
| 55 bool final = false; |
| 56 if (bytes_read < sizeof(buffer)) |
| 57 final = true; |
| 58 |
| 59 DWORD to_write = final ? sizeof(buffer) : bytes_read; |
| 60 |
| 61 DWORD actual; |
| 62 SetFilePointer(file, total_bytes, 0, FILE_BEGIN); |
| 63 if (!WriteFile(file, buffer, to_write, &actual, NULL)) |
| 64 return false; |
| 65 total_bytes += bytes_read; |
| 66 |
| 67 if (final) { |
| 68 SetFilePointer(file, total_bytes, 0, FILE_BEGIN); |
| 69 SetEndOfFile(file); |
| 70 break; |
| 71 } |
| 72 } |
| 73 return true; |
| 74 #elif defined(OS_LINUX) |
| 75 int fd = open(WideToUTF8(std::wstring(name)).c_str(), O_RDONLY); |
| 76 if (fd < 0) |
| 77 return false; |
| 78 if (fdatasync(fd) != 0) |
| 79 return false; |
| 80 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) != 0) |
| 81 return false; |
| 82 close(fd); |
| 83 return true; |
| 84 #else |
| 85 // TODO(port): Mac has its own way to do this. |
| 86 NOTIMPLEMENTED(); |
| 87 return false; |
| 88 #endif |
| 89 } |
| 90 |
31 struct TestEntry { | 91 struct TestEntry { |
32 std::string key; | 92 std::string key; |
33 int data_len; | 93 int data_len; |
34 }; | 94 }; |
35 typedef std::vector<TestEntry> TestEntries; | 95 typedef std::vector<TestEntry> TestEntries; |
36 | 96 |
37 const int kMaxSize = 16 * 1024 - 1; | 97 const int kMaxSize = 16 * 1024 - 1; |
38 | 98 |
39 // Creates num_entries on the cache, and writes 200 bytes of metadata and up | 99 // Creates num_entries on the cache, and writes 200 bytes of metadata and up |
40 // to kMaxSize of data to each entry. | 100 // to kMaxSize of data to each entry. |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
147 for (int i = 0; i < 300000; i++) { | 207 for (int i = 0; i < 300000; i++) { |
148 std::string key = GenerateKey(true); | 208 std::string key = GenerateKey(true); |
149 disk_cache::Hash(key); | 209 disk_cache::Hash(key); |
150 } | 210 } |
151 timer.Done(); | 211 timer.Done(); |
152 } | 212 } |
153 | 213 |
154 TEST_F(DiskCacheTest, CacheBackendPerformance) { | 214 TEST_F(DiskCacheTest, CacheBackendPerformance) { |
155 MessageLoopForIO message_loop; | 215 MessageLoopForIO message_loop; |
156 | 216 |
157 std::wstring path_wstring = GetCachePath(); | 217 std::wstring path = GetCachePath(); |
158 ASSERT_TRUE(DeleteCache(path_wstring.c_str())); | 218 ASSERT_TRUE(DeleteCache(path.c_str())); |
159 disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path_wstring, | 219 disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); |
160 false, 0); | |
161 ASSERT_TRUE(NULL != cache); | 220 ASSERT_TRUE(NULL != cache); |
162 | 221 |
163 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 222 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
164 srand(seed); | 223 srand(seed); |
165 | 224 |
166 TestEntries entries; | 225 TestEntries entries; |
167 int num_entries = 1000; | 226 int num_entries = 1000; |
168 | 227 |
169 int ret = TimeWrite(num_entries, cache, &entries); | 228 int ret = TimeWrite(num_entries, cache, &entries); |
170 EXPECT_EQ(ret, g_cache_tests_received); | 229 EXPECT_EQ(ret, g_cache_tests_received); |
171 | 230 |
172 MessageLoop::current()->RunAllPending(); | 231 MessageLoop::current()->RunAllPending(); |
173 delete cache; | 232 delete cache; |
174 | 233 |
175 FilePath path = FilePath::FromWStringHack(path_wstring); | 234 std::wstring filename(path); |
| 235 file_util::AppendToPath(&filename, L"index"); |
| 236 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
176 | 237 |
177 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 238 filename = path; |
178 path.Append(FILE_PATH_LITERAL("index")))); | 239 file_util::AppendToPath(&filename, L"data_0"); |
179 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 240 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
180 path.Append(FILE_PATH_LITERAL("data_0")))); | 241 |
181 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 242 filename = path; |
182 path.Append(FILE_PATH_LITERAL("data_1")))); | 243 file_util::AppendToPath(&filename, L"data_1"); |
183 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 244 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
184 path.Append(FILE_PATH_LITERAL("data_2")))); | 245 |
185 ASSERT_TRUE(file_util::EvictFileFromSystemCache( | 246 filename = path; |
186 path.Append(FILE_PATH_LITERAL("data_3")))); | 247 file_util::AppendToPath(&filename, L"data_2"); |
| 248 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
| 249 |
| 250 filename = path; |
| 251 file_util::AppendToPath(&filename, L"data_3"); |
| 252 ASSERT_TRUE(EvictFileFromSystemCache(filename.c_str())); |
187 | 253 |
188 cache = disk_cache::CreateCacheBackend(path, false, 0); | 254 cache = disk_cache::CreateCacheBackend(path, false, 0); |
189 ASSERT_TRUE(NULL != cache); | 255 ASSERT_TRUE(NULL != cache); |
190 | 256 |
191 ret = TimeRead(num_entries, cache, entries, true); | 257 ret = TimeRead(num_entries, cache, entries, true); |
192 EXPECT_EQ(ret, g_cache_tests_received); | 258 EXPECT_EQ(ret, g_cache_tests_received); |
193 | 259 |
194 ret = TimeRead(num_entries, cache, entries, false); | 260 ret = TimeRead(num_entries, cache, entries, false); |
195 EXPECT_EQ(ret, g_cache_tests_received); | 261 EXPECT_EQ(ret, g_cache_tests_received); |
196 | 262 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
237 entry = 0; | 303 entry = 0; |
238 | 304 |
239 files.DeleteBlock(address[entry], false); | 305 files.DeleteBlock(address[entry], false); |
240 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 306 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
241 &address[entry])); | 307 &address[entry])); |
242 } | 308 } |
243 | 309 |
244 timer2.Done(); | 310 timer2.Done(); |
245 MessageLoop::current()->RunAllPending(); | 311 MessageLoop::current()->RunAllPending(); |
246 } | 312 } |
OLD | NEW |