Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: net/disk_cache/blockfile/disk_cache_perftest.cc

Issue 127083002: **STILLBAKING** Decouple disk cache tests from backends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: upstream rebase Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « net/disk_cache/blockfile/backend_impl.cc ('k') | net/disk_cache/cache_creator.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/perf_time_logger.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/blockfile/backend_impl.h" 20 #include "net/disk_cache/blockfile/backend_impl.h"
21 #include "net/disk_cache/blockfile/block_files.h" 21 #include "net/disk_cache/blockfile/block_files.h"
22 #include "net/disk_cache/disk_cache.h" 22 #include "net/disk_cache/disk_cache.h"
23 #include "net/disk_cache/disk_cache_test_base.h" 23 #include "net/disk_cache/disk_cache_test.h"
24 #include "net/disk_cache/disk_cache_test_util.h" 24 #include "net/disk_cache/disk_cache_test_util.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 #include "testing/platform_test.h" 26 #include "testing/platform_test.h"
27 27
28 using base::Time; 28 using base::Time;
29 29
30 namespace disk_cache {
31
30 namespace { 32 namespace {
31 33
32 struct TestEntry { 34 struct TestEntry {
33 std::string key; 35 std::string key;
34 int data_len; 36 int data_len;
35 }; 37 };
36 typedef std::vector<TestEntry> TestEntries; 38 typedef std::vector<TestEntry> TestEntries;
37 39
38 const int kMaxSize = 16 * 1024 - 1; 40 const int kMaxSize = 16 * 1024 - 1;
39 41
40 // Creates num_entries on the cache, and writes 200 bytes of metadata and up 42 // Creates num_entries on the cache, and writes 200 bytes of metadata and up
41 // to kMaxSize of data to each entry. 43 // to kMaxSize of data to each entry.
42 bool TimeWrite(int num_entries, disk_cache::Backend* cache, 44 bool TimeWrite(int num_entries, Backend* cache,
43 TestEntries* entries) { 45 TestEntries* entries) {
44 const int kSize1 = 200; 46 const int kSize1 = 200;
45 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 47 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
46 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); 48 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize));
47 49
48 CacheTestFillBuffer(buffer1->data(), kSize1, false); 50 CacheTestFillBuffer(buffer1->data(), kSize1, false);
49 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); 51 CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
50 52
51 int expected = 0; 53 int expected = 0;
52 54
53 MessageLoopHelper helper; 55 MessageLoopHelper helper;
54 CallbackTest callback(&helper, true); 56 CallbackTest callback(&helper, true);
55 57
56 base::PerfTimeLogger timer("Write disk cache entries"); 58 base::PerfTimeLogger timer("Write disk cache entries");
57 59
58 for (int i = 0; i < num_entries; i++) { 60 for (int i = 0; i < num_entries; i++) {
59 TestEntry entry; 61 TestEntry entry;
60 entry.key = GenerateKey(true); 62 entry.key = GenerateKey(true);
61 entry.data_len = rand() % kMaxSize; 63 entry.data_len = rand() % kMaxSize;
62 entries->push_back(entry); 64 entries->push_back(entry);
63 65
64 disk_cache::Entry* cache_entry; 66 Entry* cache_entry;
65 net::TestCompletionCallback cb; 67 net::TestCompletionCallback cb;
66 int rv = cache->CreateEntry(entry.key, &cache_entry, cb.callback()); 68 int rv = cache->CreateEntry(entry.key, &cache_entry, cb.callback());
67 if (net::OK != cb.GetResult(rv)) 69 if (net::OK != cb.GetResult(rv))
68 break; 70 break;
69 int ret = cache_entry->WriteData( 71 int ret = cache_entry->WriteData(
70 0, 0, buffer1.get(), kSize1, 72 0, 0, buffer1.get(), kSize1,
71 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false); 73 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false);
72 if (net::ERR_IO_PENDING == ret) 74 if (net::ERR_IO_PENDING == ret)
73 expected++; 75 expected++;
74 else if (kSize1 != ret) 76 else if (kSize1 != ret)
75 break; 77 break;
76 78
77 ret = cache_entry->WriteData( 79 ret = cache_entry->WriteData(
78 1, 0, buffer2.get(), entry.data_len, 80 1, 0, buffer2.get(), entry.data_len,
79 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false); 81 base::Bind(&CallbackTest::Run, base::Unretained(&callback)), false);
80 if (net::ERR_IO_PENDING == ret) 82 if (net::ERR_IO_PENDING == ret)
81 expected++; 83 expected++;
82 else if (entry.data_len != ret) 84 else if (entry.data_len != ret)
83 break; 85 break;
84 cache_entry->Close(); 86 cache_entry->Close();
85 } 87 }
86 88
87 helper.WaitUntilCacheIoFinished(expected); 89 helper.WaitUntilCacheIoFinished(expected);
88 timer.Done(); 90 timer.Done();
89 91
90 return (expected == helper.callbacks_called()); 92 return (expected == helper.callbacks_called());
91 } 93 }
92 94
93 // Reads the data and metadata from each entry listed on |entries|. 95 // Reads the data and metadata from each entry listed on |entries|.
94 bool TimeRead(int num_entries, disk_cache::Backend* cache, 96 bool TimeRead(int num_entries, Backend* cache,
95 const TestEntries& entries, bool cold) { 97 const TestEntries& entries, bool cold) {
96 const int kSize1 = 200; 98 const int kSize1 = 200;
97 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1)); 99 scoped_refptr<net::IOBuffer> buffer1(new net::IOBuffer(kSize1));
98 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize)); 100 scoped_refptr<net::IOBuffer> buffer2(new net::IOBuffer(kMaxSize));
99 101
100 CacheTestFillBuffer(buffer1->data(), kSize1, false); 102 CacheTestFillBuffer(buffer1->data(), kSize1, false);
101 CacheTestFillBuffer(buffer2->data(), kMaxSize, false); 103 CacheTestFillBuffer(buffer2->data(), kMaxSize, false);
102 104
103 int expected = 0; 105 int expected = 0;
104 106
105 MessageLoopHelper helper; 107 MessageLoopHelper helper;
106 CallbackTest callback(&helper, true); 108 CallbackTest callback(&helper, true);
107 109
108 const char* message = cold ? "Read disk cache entries (cold)" : 110 const char* message = cold ? "Read disk cache entries (cold)" :
109 "Read disk cache entries (warm)"; 111 "Read disk cache entries (warm)";
110 base::PerfTimeLogger timer(message); 112 base::PerfTimeLogger timer(message);
111 113
112 for (int i = 0; i < num_entries; i++) { 114 for (int i = 0; i < num_entries; i++) {
113 disk_cache::Entry* cache_entry; 115 Entry* cache_entry;
114 net::TestCompletionCallback cb; 116 net::TestCompletionCallback cb;
115 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback()); 117 int rv = cache->OpenEntry(entries[i].key, &cache_entry, cb.callback());
116 if (net::OK != cb.GetResult(rv)) 118 if (net::OK != cb.GetResult(rv))
117 break; 119 break;
118 int ret = cache_entry->ReadData( 120 int ret = cache_entry->ReadData(
119 0, 0, buffer1.get(), kSize1, 121 0, 0, buffer1.get(), kSize1,
120 base::Bind(&CallbackTest::Run, base::Unretained(&callback))); 122 base::Bind(&CallbackTest::Run, base::Unretained(&callback)));
121 if (net::ERR_IO_PENDING == ret) 123 if (net::ERR_IO_PENDING == ret)
122 expected++; 124 expected++;
123 else if (kSize1 != ret) 125 else if (kSize1 != ret)
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 timer.Done(); 160 timer.Done();
159 } 161 }
160 162
161 TEST_F(DiskCacheTest, CacheBackendPerformance) { 163 TEST_F(DiskCacheTest, CacheBackendPerformance) {
162 base::Thread cache_thread("CacheThread"); 164 base::Thread cache_thread("CacheThread");
163 ASSERT_TRUE(cache_thread.StartWithOptions( 165 ASSERT_TRUE(cache_thread.StartWithOptions(
164 base::Thread::Options(base::MessageLoop::TYPE_IO, 0))); 166 base::Thread::Options(base::MessageLoop::TYPE_IO, 0)));
165 167
166 ASSERT_TRUE(CleanupCacheDir()); 168 ASSERT_TRUE(CleanupCacheDir());
167 net::TestCompletionCallback cb; 169 net::TestCompletionCallback cb;
168 scoped_ptr<disk_cache::Backend> cache; 170 scoped_ptr<Backend> cache;
169 int rv = disk_cache::CreateCacheBackend( 171 int rv = CreateCacheBackend(
170 net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path_, 0, false, 172 net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path(), 0, false,
171 cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback()); 173 cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback());
172 174
173 ASSERT_EQ(net::OK, cb.GetResult(rv)); 175 ASSERT_EQ(net::OK, cb.GetResult(rv));
174 176
175 int seed = static_cast<int>(Time::Now().ToInternalValue()); 177 int seed = static_cast<int>(Time::Now().ToInternalValue());
176 srand(seed); 178 srand(seed);
177 179
178 TestEntries entries; 180 TestEntries entries;
179 int num_entries = 1000; 181 int num_entries = 1000;
180 182
181 EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries)); 183 EXPECT_TRUE(TimeWrite(num_entries, cache.get(), &entries));
182 184
183 base::MessageLoop::current()->RunUntilIdle(); 185 base::MessageLoop::current()->RunUntilIdle();
184 cache.reset(); 186 cache.reset();
185 187
186 ASSERT_TRUE(file_util::EvictFileFromSystemCache( 188 ASSERT_TRUE(file_util::EvictFileFromSystemCache(
187 cache_path_.AppendASCII("index"))); 189 cache_path().AppendASCII("index")));
188 ASSERT_TRUE(file_util::EvictFileFromSystemCache( 190 ASSERT_TRUE(file_util::EvictFileFromSystemCache(
189 cache_path_.AppendASCII("data_0"))); 191 cache_path().AppendASCII("data_0")));
190 ASSERT_TRUE(file_util::EvictFileFromSystemCache( 192 ASSERT_TRUE(file_util::EvictFileFromSystemCache(
191 cache_path_.AppendASCII("data_1"))); 193 cache_path().AppendASCII("data_1")));
192 ASSERT_TRUE(file_util::EvictFileFromSystemCache( 194 ASSERT_TRUE(file_util::EvictFileFromSystemCache(
193 cache_path_.AppendASCII("data_2"))); 195 cache_path().AppendASCII("data_2")));
194 ASSERT_TRUE(file_util::EvictFileFromSystemCache( 196 ASSERT_TRUE(file_util::EvictFileFromSystemCache(
195 cache_path_.AppendASCII("data_3"))); 197 cache_path().AppendASCII("data_3")));
196 198
197 rv = disk_cache::CreateCacheBackend( 199 rv = CreateCacheBackend(
198 net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path_, 0, false, 200 net::DISK_CACHE, net::CACHE_BACKEND_BLOCKFILE, cache_path(), 0, false,
199 cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback()); 201 cache_thread.message_loop_proxy().get(), NULL, &cache, cb.callback());
200 ASSERT_EQ(net::OK, cb.GetResult(rv)); 202 ASSERT_EQ(net::OK, cb.GetResult(rv));
201 203
202 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true)); 204 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, true));
203 205
204 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false)); 206 EXPECT_TRUE(TimeRead(num_entries, cache.get(), entries, false));
205 207
206 base::MessageLoop::current()->RunUntilIdle(); 208 base::MessageLoop::current()->RunUntilIdle();
207 } 209 }
208 210
209 // Creating and deleting "entries" on a block-file is something quite frequent 211 // Creating and deleting "entries" on a block-file is something quite frequent
210 // (after all, almost everything is stored on block files). The operation is 212 // (after all, almost everything is stored on block files). The operation is
211 // almost free when the file is empty, but can be expensive if the file gets 213 // almost free when the file is empty, but can be expensive if the file gets
212 // fragmented, or if we have multiple files. This test measures that scenario, 214 // fragmented, or if we have multiple files. This test measures that scenario,
213 // by using multiple, highly fragmented files. 215 // by using multiple, highly fragmented files.
214 TEST_F(DiskCacheTest, BlockFilesPerformance) { 216 TEST_F(DiskCacheTest, BlockFilesPerformance) {
215 ASSERT_TRUE(CleanupCacheDir()); 217 ASSERT_TRUE(CleanupCacheDir());
216 218
217 disk_cache::BlockFiles files(cache_path_); 219 BlockFiles files(cache_path());
218 ASSERT_TRUE(files.Init(true)); 220 ASSERT_TRUE(files.Init(true));
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 const int kNumEntries = 60000; 225 const int kNumEntries = 60000;
224 disk_cache::Addr* address = new disk_cache::Addr[kNumEntries]; 226 Addr* address = new Addr[kNumEntries];
225 227
226 base::PerfTimeLogger timer1("Fill three block-files"); 228 base::PerfTimeLogger timer1("Fill three block-files");
227 229
228 // Fill up the 32-byte block file (use three files). 230 // Fill up the 32-byte block file (use three files).
229 for (int i = 0; i < kNumEntries; i++) { 231 for (int i = 0; i < kNumEntries; i++) {
230 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), 232 EXPECT_TRUE(files.CreateBlock(RANKINGS, BlockSize(),
231 &address[i])); 233 &address[i]));
232 } 234 }
233 235
234 timer1.Done(); 236 timer1.Done();
235 base::PerfTimeLogger timer2("Create and delete blocks"); 237 base::PerfTimeLogger timer2("Create and delete blocks");
236 238
237 for (int i = 0; i < 200000; i++) { 239 for (int i = 0; i < 200000; i++) {
238 int entry = rand() * (kNumEntries / RAND_MAX + 1); 240 int entry = rand() * (kNumEntries / RAND_MAX + 1);
239 if (entry >= kNumEntries) 241 if (entry >= kNumEntries)
240 entry = 0; 242 entry = 0;
241 243
242 files.DeleteBlock(address[entry], false); 244 files.DeleteBlock(address[entry], false);
243 EXPECT_TRUE(files.CreateBlock(disk_cache::RANKINGS, BlockSize(), 245 EXPECT_TRUE(files.CreateBlock(RANKINGS, BlockSize(),
244 &address[entry])); 246 &address[entry]));
245 } 247 }
246 248
247 timer2.Done(); 249 timer2.Done();
248 base::MessageLoop::current()->RunUntilIdle(); 250 base::MessageLoop::current()->RunUntilIdle();
249 delete[] address; 251 delete[] address;
250 } 252 }
253
254 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/blockfile/backend_impl.cc ('k') | net/disk_cache/cache_creator.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698