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/bind.h" | 8 #include "base/bind.h" |
9 #include "base/bind_helpers.h" | 9 #include "base/bind_helpers.h" |
10 #include "base/hash.h" | 10 #include "base/hash.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 #include "base/test/perftimer.h" | 12 #include "base/test/perf_time_logger.h" |
13 #include "base/test/test_file_util.h" | 13 #include "base/test/test_file_util.h" |
14 #include "base/threading/thread.h" | 14 #include "base/threading/thread.h" |
15 #include "base/timer/timer.h" | 15 #include "base/timer/timer.h" |
16 #include "net/base/cache_type.h" | 16 #include "net/base/cache_type.h" |
17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
19 #include "net/base/test_completion_callback.h" | 19 #include "net/base/test_completion_callback.h" |
20 #include "net/disk_cache/backend_impl.h" | 20 #include "net/disk_cache/backend_impl.h" |
21 #include "net/disk_cache/block_files.h" | 21 #include "net/disk_cache/block_files.h" |
22 #include "net/disk_cache/disk_cache.h" | 22 #include "net/disk_cache/disk_cache.h" |
(...skipping 23 matching lines...) Expand all Loading... |
46 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); | 46 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); |
47 | 47 |
48 CacheTestFillBuffer(buffer1->data(), kSize1, false); | 48 CacheTestFillBuffer(buffer1->data(), kSize1, false); |
49 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); | 49 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); |
50 | 50 |
51 int expected = 0; | 51 int expected = 0; |
52 | 52 |
53 MessageLoopHelper helper; | 53 MessageLoopHelper helper; |
54 CallbackTest callback(&helper, true); | 54 CallbackTest callback(&helper, true); |
55 | 55 |
56 PerfTimeLogger timer("Write disk cache entries"); | 56 base::PerfTimeLogger timer("Write disk cache entries"); |
57 | 57 |
58 for (int i = 0; i < num_entries; i++) { | 58 for (int i = 0; i < num_entries; i++) { |
59 TestEntry entry; | 59 TestEntry entry; |
60 entry.key = GenerateKey(true); | 60 entry.key = GenerateKey(true); |
61 entry.data_len = rand() % kMaxSize; | 61 entry.data_len = rand() % kMaxSize; |
62 entries->push_back(entry); | 62 entries->push_back(entry); |
63 | 63 |
64 disk_cache::Entry* cache_entry; | 64 disk_cache::Entry* cache_entry; |
65 net::TestCompletionCallback cb; | 65 net::TestCompletionCallback cb; |
66 int rv = cache->CreateEntry(entry.key, &cache_entry, cb.callback()); | 66 int rv = cache->CreateEntry(entry.key, &cache_entry, cb.callback()); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 CacheTestFillBuffer(buffer1->data(), kSize1, false); | 100 CacheTestFillBuffer(buffer1->data(), kSize1, false); |
101 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); | 101 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); |
102 | 102 |
103 int expected = 0; | 103 int expected = 0; |
104 | 104 |
105 MessageLoopHelper helper; | 105 MessageLoopHelper helper; |
106 CallbackTest callback(&helper, true); | 106 CallbackTest callback(&helper, true); |
107 | 107 |
108 const char* message = cold ? "Read disk cache entries (cold)" : | 108 const char* message = cold ? "Read disk cache entries (cold)" : |
109 "Read disk cache entries (warm)"; | 109 "Read disk cache entries (warm)"; |
110 PerfTimeLogger timer(message); | 110 base::PerfTimeLogger timer(message); |
111 | 111 |
112 for (int i = 0; i < num_entries; i++) { | 112 for (int i = 0; i < num_entries; i++) { |
113 disk_cache::Entry* cache_entry; | 113 disk_cache::Entry* cache_entry; |
114 net::TestCompletionCallback cb; | 114 net::TestCompletionCallback cb; |
115 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback()); | 115 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback()); |
116 if (net::OK != cb.GetResult(rv)) | 116 if (net::OK != cb.GetResult(rv)) |
117 break; | 117 break; |
118 int ret = cache_entry->ReadData( | 118 int ret = cache_entry->ReadData( |
119 0, 0, buffer1.get(), kSize1, | 119 0, 0, buffer1.get(), kSize1, |
120 base::Bind(&CallbackTest::Run, base::Unretained(&callback))); | 120 base::Bind(&CallbackTest::Run, base::Unretained(&callback))); |
(...skipping 22 matching lines...) Expand all Loading... |
143 // We can use form 1 to 4 blocks. | 143 // We can use form 1 to 4 blocks. |
144 return (rand() & 0x3) + 1; | 144 return (rand() & 0x3) + 1; |
145 } | 145 } |
146 | 146 |
147 } // namespace | 147 } // namespace |
148 | 148 |
149 TEST_F(DiskCacheTest, Hash) { | 149 TEST_F(DiskCacheTest, Hash) { |
150 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 150 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
151 srand(seed); | 151 srand(seed); |
152 | 152 |
153 PerfTimeLogger timer("Hash disk cache keys"); | 153 base::PerfTimeLogger timer("Hash disk cache keys"); |
154 for (int i = 0; i < 300000; i++) { | 154 for (int i = 0; i < 300000; i++) { |
155 std::string key = GenerateKey(true); | 155 std::string key = GenerateKey(true); |
156 base::Hash(key); | 156 base::Hash(key); |
157 } | 157 } |
158 timer.Done(); | 158 timer.Done(); |
159 } | 159 } |
160 | 160 |
161 TEST_F(DiskCacheTest, CacheBackendPerformance) { | 161 TEST_F(DiskCacheTest, CacheBackendPerformance) { |
162 base::Thread cache_thread("CacheThread"); | 162 base::Thread cache_thread("CacheThread"); |
163 ASSERT_TRUE(cache_thread.StartWithOptions( | 163 ASSERT_TRUE(cache_thread.StartWithOptions( |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 216 |
217 disk_cache::BlockFiles files(cache_path_); | 217 disk_cache::BlockFiles files(cache_path_); |
218 ASSERT_TRUE(files.Init(true)); | 218 ASSERT_TRUE(files.Init(true)); |
219 | 219 |
220 int seed = static_cast<int>(Time::Now().ToInternalValue()); | 220 int seed = static_cast<int>(Time::Now().ToInternalValue()); |
221 srand(seed); | 221 srand(seed); |
222 | 222 |
223 const int kNumEntries = 60000; | 223 const int kNumEntries = 60000; |
224 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; | 224 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; |
225 | 225 |
226 PerfTimeLogger timer1("Fill three block-files"); | 226 base::PerfTimeLogger timer1("Fill three block-files"); |
227 | 227 |
228 // Fill up the 32-byte block file (use three files). | 228 // Fill up the 32-byte block file (use three files). |
229 for (int i = 0; i < kNumEntries; i++) { | 229 for (int i = 0; i < kNumEntries; i++) { |
230 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 230 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
231 &address[i])); | 231 &address[i])); |
232 } | 232 } |
233 | 233 |
234 timer1.Done(); | 234 timer1.Done(); |
235 PerfTimeLogger timer2("Create and delete blocks"); | 235 base::PerfTimeLogger timer2("Create and delete blocks"); |
236 | 236 |
237 for (int i = 0; i < 200000; i++) { | 237 for (int i = 0; i < 200000; i++) { |
238 int entry = rand() * (kNumEntries / RAND_MAX + 1); | 238 int entry = rand() * (kNumEntries / RAND_MAX + 1); |
239 if (entry >= kNumEntries) | 239 if (entry >= kNumEntries) |
240 entry = 0; | 240 entry = 0; |
241 | 241 |
242 files.DeleteBlock(address[entry], false); | 242 files.DeleteBlock(address[entry], false); |
243 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), | 243 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), |
244 &address[entry])); | 244 &address[entry])); |
245 } | 245 } |
246 | 246 |
247 timer2.Done(); | 247 timer2.Done(); |
248 base::MessageLoop::current()->RunUntilIdle(); | 248 base::MessageLoop::current()->RunUntilIdle(); |
249 delete[] address; | 249 delete[] address; |
250 } | 250 } |
OLD | NEW |