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 #include <unistd.h> |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
5 #include "base/file_util.h" | 11 #include "base/file_util.h" |
6 #include "base/perftimer.h" | 12 #include "base/perftimer.h" |
| 13 #if defined(OS_WIN) |
7 #include "base/scoped_handle.h" | 14 #include "base/scoped_handle.h" |
| 15 #endif |
| 16 #include "base/string_util.h" |
8 #include "base/timer.h" | 17 #include "base/timer.h" |
9 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
10 #include "net/disk_cache/block_files.h" | 19 #include "net/disk_cache/block_files.h" |
11 #include "net/disk_cache/disk_cache.h" | 20 #include "net/disk_cache/disk_cache.h" |
12 #include "net/disk_cache/disk_cache_test_base.h" | 21 #include "net/disk_cache/disk_cache_test_base.h" |
13 #include "net/disk_cache/disk_cache_test_util.h" | 22 #include "net/disk_cache/disk_cache_test_util.h" |
14 #include "net/disk_cache/hash.h" | 23 #include "net/disk_cache/hash.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
16 | 25 |
17 extern int g_cache_tests_max_id; | 26 extern int g_cache_tests_max_id; |
18 extern volatile int g_cache_tests_received; | 27 extern volatile int g_cache_tests_received; |
19 extern volatile bool g_cache_tests_error; | 28 extern volatile bool g_cache_tests_error; |
20 | 29 |
21 namespace { | 30 namespace { |
22 | 31 |
23 bool EvictFileFromSystemCache(const wchar_t* name) { | 32 bool EvictFileFromSystemCache(const wchar_t* name) { |
| 33 #if defined(OS_WIN) |
24 // Overwrite it with no buffering. | 34 // Overwrite it with no buffering. |
25 ScopedHandle file(CreateFile(name, GENERIC_READ | GENERIC_WRITE, | 35 ScopedHandle file(CreateFile(name, GENERIC_READ | GENERIC_WRITE, |
26 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, | 36 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
27 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); | 37 OPEN_EXISTING, FILE_FLAG_NO_BUFFERING, NULL)); |
28 if (!file.IsValid()) | 38 if (!file.IsValid()) |
29 return false; | 39 return false; |
30 | 40 |
31 // Execute in chunks. It could be optimized. We want to do few of these since | 41 // Execute in chunks. It could be optimized. We want to do few of these since |
32 // these opterations will be slow without the cache. | 42 // these opterations will be slow without the cache. |
33 char buffer[128 * 1024]; | 43 char buffer[128 * 1024]; |
(...skipping 17 matching lines...) Expand all Loading... |
51 return false; | 61 return false; |
52 total_bytes += bytes_read; | 62 total_bytes += bytes_read; |
53 | 63 |
54 if (final) { | 64 if (final) { |
55 SetFilePointer(file, total_bytes, 0, FILE_BEGIN); | 65 SetFilePointer(file, total_bytes, 0, FILE_BEGIN); |
56 SetEndOfFile(file); | 66 SetEndOfFile(file); |
57 break; | 67 break; |
58 } | 68 } |
59 } | 69 } |
60 return true; | 70 return true; |
| 71 #elif defined(OS_LINUX) |
| 72 int fd = open(WideToUTF8(std::wstring(name)).c_str(), O_RDONLY); |
| 73 if (fd < 0) |
| 74 return false; |
| 75 if (fdatasync(fd) != 0) |
| 76 return false; |
| 77 if (posix_fadvise(fd, 0, 0, POSIX_FADV_DONTNEED) != 0) |
| 78 return false; |
| 79 close(fd); |
| 80 return true; |
| 81 #else |
| 82 // TODO(port): Mac has its own way to do this. |
| 83 NOTIMPLEMENTED(); |
| 84 return false; |
| 85 #endif |
61 } | 86 } |
62 | 87 |
63 struct TestEntry { | 88 struct TestEntry { |
64 std::string key; | 89 std::string key; |
65 int data_len; | 90 int data_len; |
66 }; | 91 }; |
67 typedef std::vector<TestEntry> TestEntries; | 92 typedef std::vector<TestEntry> TestEntries; |
68 | 93 |
69 const int kMaxSize = 16 * 1024 - 1; | 94 const int kMaxSize = 16 * 1024 - 1; |
70 | 95 |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 | 196 |
172 } // namespace | 197 } // namespace |
173 | 198 |
174 TEST_F(DiskCacheTest, Hash) { | 199 TEST_F(DiskCacheTest, Hash) { |
175 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 200 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
176 srand(seed); | 201 srand(seed); |
177 | 202 |
178 PerfTimeLogger timer("Hash disk cache keys"); | 203 PerfTimeLogger timer("Hash disk cache keys"); |
179 for (int i = 0; i < 300000; i++) { | 204 for (int i = 0; i < 300000; i++) { |
180 std::string key = GenerateKey(true); | 205 std::string key = GenerateKey(true); |
181 uint32 hash = disk_cache::Hash(key); | 206 disk_cache::Hash(key); |
182 } | 207 } |
183 timer.Done(); | 208 timer.Done(); |
184 } | 209 } |
185 | 210 |
186 TEST_F(DiskCacheTest, CacheBackendPerformance) { | 211 TEST_F(DiskCacheTest, CacheBackendPerformance) { |
187 MessageLoopForIO message_loop; | 212 MessageLoopForIO message_loop; |
188 | 213 |
189 std::wstring path = GetCachePath(); | 214 std::wstring path = GetCachePath(); |
190 ASSERT_TRUE(DeleteCache(path.c_str())); | 215 ASSERT_TRUE(DeleteCache(path.c_str())); |
191 disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); | 216 disk_cache::Backend* cache = disk_cache::CreateCacheBackend(path, false, 0); |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
271 if (entry >= kNumEntries) | 296 if (entry >= kNumEntries) |
272 entry = 0; | 297 entry = 0; |
273 | 298 |
274 files.DeleteBlock(address[entry], false); | 299 files.DeleteBlock(address[entry], false); |
275 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 300 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
276 &address[entry])); | 301 &address[entry])); |
277 } | 302 } |
278 | 303 |
279 timer2.Done(); | 304 timer2.Done(); |
280 } | 305 } |
OLD | NEW |