Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 <stdint.h> | 5 #include <stdint.h> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/files/file_util.h" | 8 #include "base/files/file_util.h" |
| 9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 82 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween. | 82 // will be filled with times, used by DoomEntriesSince and DoomEntriesBetween. |
| 83 // There are 4 entries after doomed_start and 2 after doomed_end. | 83 // There are 4 entries after doomed_start and 2 after doomed_end. |
| 84 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end); | 84 void InitSparseCache(base::Time* doomed_start, base::Time* doomed_end); |
| 85 | 85 |
| 86 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); | 86 bool CreateSetOfRandomEntries(std::set<std::string>* key_pool); |
| 87 bool EnumerateAndMatchKeys(int max_to_open, | 87 bool EnumerateAndMatchKeys(int max_to_open, |
| 88 TestIterator* iter, | 88 TestIterator* iter, |
| 89 std::set<std::string>* keys_to_match, | 89 std::set<std::string>* keys_to_match, |
| 90 size_t* count); | 90 size_t* count); |
| 91 | 91 |
| 92 // Computes the expected size of entry metadata, i.e. the total size without | |
| 93 // the actual data stored. This depends only on the entry's |key| size. | |
| 94 int GetEntryMetadataSize(std::string key); | |
| 95 | |
| 92 // Actual tests: | 96 // Actual tests: |
| 93 void BackendBasics(); | 97 void BackendBasics(); |
| 94 void BackendKeying(); | 98 void BackendKeying(); |
| 95 void BackendShutdownWithPendingFileIO(bool fast); | 99 void BackendShutdownWithPendingFileIO(bool fast); |
| 96 void BackendShutdownWithPendingIO(bool fast); | 100 void BackendShutdownWithPendingIO(bool fast); |
| 97 void BackendShutdownWithPendingCreate(bool fast); | 101 void BackendShutdownWithPendingCreate(bool fast); |
| 98 void BackendSetSize(); | 102 void BackendSetSize(); |
| 99 void BackendLoad(); | 103 void BackendLoad(); |
| 100 void BackendChain(); | 104 void BackendChain(); |
| 101 void BackendValidEntry(); | 105 void BackendValidEntry(); |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 275 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey())); | 279 EXPECT_EQ(1U, keys_to_match->erase(entry->GetKey())); |
| 276 entry->Close(); | 280 entry->Close(); |
| 277 ++(*count); | 281 ++(*count); |
| 278 if (max_to_open >= 0 && static_cast<int>(*count) >= max_to_open) | 282 if (max_to_open >= 0 && static_cast<int>(*count) >= max_to_open) |
| 279 break; | 283 break; |
| 280 }; | 284 }; |
| 281 | 285 |
| 282 return true; | 286 return true; |
| 283 } | 287 } |
| 284 | 288 |
| 289 int DiskCacheBackendTest::GetEntryMetadataSize(std::string key) { | |
| 290 // For blockfile and memory backends, it is just the key size. | |
| 291 if (!simple_cache_mode_) | |
| 292 return key.size(); | |
| 293 | |
| 294 // For the simple cache, we must add the file header and EOF, and that for | |
| 295 // every stream. | |
| 296 return disk_cache::kSimpleEntryStreamCount * | |
| 297 (sizeof(disk_cache::SimpleFileHeader) + | |
| 298 sizeof(disk_cache::SimpleFileEOF) + key.size()); | |
| 299 } | |
| 300 | |
| 285 void DiskCacheBackendTest::BackendBasics() { | 301 void DiskCacheBackendTest::BackendBasics() { |
| 286 InitCache(); | 302 InitCache(); |
| 287 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; | 303 disk_cache::Entry *entry1 = NULL, *entry2 = NULL; |
| 288 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); | 304 EXPECT_NE(net::OK, OpenEntry("the first key", &entry1)); |
| 289 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); | 305 ASSERT_EQ(net::OK, CreateEntry("the first key", &entry1)); |
| 290 ASSERT_TRUE(NULL != entry1); | 306 ASSERT_TRUE(NULL != entry1); |
| 291 entry1->Close(); | 307 entry1->Close(); |
| 292 entry1 = NULL; | 308 entry1 = NULL; |
| 293 | 309 |
| 294 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); | 310 ASSERT_EQ(net::OK, OpenEntry("the first key", &entry1)); |
| (...skipping 1377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1672 start = end; | 1688 start = end; |
| 1673 end = base::Time::Now(); | 1689 end = base::Time::Now(); |
| 1674 DoomEntriesBetween(start, end); | 1690 DoomEntriesBetween(start, end); |
| 1675 EXPECT_EQ(3, cache_->GetEntryCount()); | 1691 EXPECT_EQ(3, cache_->GetEntryCount()); |
| 1676 } | 1692 } |
| 1677 | 1693 |
| 1678 void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() { | 1694 void DiskCacheBackendTest::BackendCalculateSizeOfAllEntries() { |
| 1679 InitCache(); | 1695 InitCache(); |
| 1680 | 1696 |
| 1681 // The cache is initially empty. | 1697 // The cache is initially empty. |
| 1682 if (memory_only_ || simple_cache_mode_) { | 1698 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 1683 // TODO(msramek): Implement. | |
| 1684 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries()); | |
| 1685 } else { | |
| 1686 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | |
| 1687 } | |
| 1688 | 1699 |
| 1689 // Generate random entries and populate them with data of respective | 1700 // Generate random entries and populate them with data of respective |
| 1690 // sizes 0, 1, ..., count - 1 bytes. | 1701 // sizes 0, 1, ..., count - 1 bytes. |
| 1691 std::set<std::string> key_pool; | 1702 std::set<std::string> key_pool; |
| 1692 CreateSetOfRandomEntries(&key_pool); | 1703 CreateSetOfRandomEntries(&key_pool); |
| 1693 | 1704 |
| 1694 int count = 0; | 1705 int count = 0; |
| 1695 for (std::string key : key_pool) { | 1706 for (std::string key : key_pool) { |
| 1696 std::string data(count, ' '); | 1707 std::string data(count, ' '); |
| 1697 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1708 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 1698 | 1709 |
| 1699 // Alternate between writing to the first and second stream to test that | 1710 // Write only to the stream 0. This is because writing to the stream 1 |
|
pasko
2015/10/15 11:55:54
I would prefer if we did not regress in our test c
msramek
2015/10/16 13:32:37
I spent some more time trying to find out if I can
pasko
2015/10/16 14:03:32
yay! Thanks a lot! I could not really find a ratio
pasko
2015/10/16 14:24:04
tiny correction: we actually measured that it's he
msramek
2015/10/16 14:35:15
Ok, then I can revert to Patchset 1 and just add S
pasko
2015/10/16 14:43:59
you would not loose any important coverage in size
| |
| 1700 // we are not taking just the first stream into account. | 1711 // in simple cache triggers a write to the stream 0 as well. This will |
| 1712 // happen asynchronously, because |DiskCacheTestWithCache::WriteData| | |
| 1713 // only waits for the callback from the first write operation, and possibly | |
| 1714 // later than our call to |CalculateSizeOfAllEntries|. | |
| 1715 // TODO(msramek): Investigate how to write synchronously to other streams. | |
| 1701 disk_cache::Entry* entry; | 1716 disk_cache::Entry* entry; |
| 1702 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); | 1717 ASSERT_EQ(net::OK, OpenEntry(key, &entry)); |
| 1703 ASSERT_EQ(count, WriteData(entry, count % 2, 0, buffer.get(), count, true)); | 1718 ASSERT_EQ(count, WriteData(entry, 0, 0, buffer.get(), count, true)); |
| 1704 entry->Close(); | 1719 entry->Close(); |
| 1705 | 1720 |
| 1706 ++count; | 1721 ++count; |
| 1707 } | 1722 } |
| 1708 | 1723 |
| 1709 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. | 1724 // The resulting size should be (0 + 1 + ... + count - 1) plus keys. |
| 1710 int result = CalculateSizeOfAllEntries(); | 1725 int result = CalculateSizeOfAllEntries(); |
| 1711 if (memory_only_ || simple_cache_mode_) { | 1726 int total_metadata_size = 0; |
| 1712 // TODO(msramek): Implement. | 1727 for (std::string key : key_pool) |
| 1713 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, result); | 1728 total_metadata_size += GetEntryMetadataSize(key); |
| 1714 } else { | 1729 EXPECT_EQ((count - 1) * count / 2 + total_metadata_size, result); |
| 1715 int total_key_size = 0; | |
| 1716 for (std::string key : key_pool) | |
| 1717 total_key_size += key.size(); | |
| 1718 | |
| 1719 EXPECT_EQ((count - 1) * count / 2 + total_key_size, result); | |
| 1720 } | |
| 1721 | 1730 |
| 1722 // Add another entry and test if the size is updated. Then remove it and test | 1731 // Add another entry and test if the size is updated. Then remove it and test |
| 1723 // if the size is back to original value. | 1732 // if the size is back to original value. |
| 1724 { | 1733 { |
| 1725 const int last_entry_size = 47; | 1734 const int last_entry_size = 47; |
| 1726 std::string data(last_entry_size, ' '); | 1735 std::string data(last_entry_size, ' '); |
| 1727 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); | 1736 scoped_refptr<net::StringIOBuffer> buffer = new net::StringIOBuffer(data); |
| 1728 | 1737 |
| 1729 disk_cache::Entry* entry; | 1738 disk_cache::Entry* entry; |
| 1730 std::string key = GenerateKey(true); | 1739 std::string key = GenerateKey(true); |
| 1731 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); | 1740 ASSERT_EQ(net::OK, CreateEntry(key, &entry)); |
| 1732 ASSERT_EQ(last_entry_size, | 1741 ASSERT_EQ(last_entry_size, |
| 1733 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); | 1742 WriteData(entry, 0, 0, buffer.get(), last_entry_size, true)); |
| 1734 entry->Close(); | 1743 entry->Close(); |
| 1735 | 1744 |
| 1736 int new_result = CalculateSizeOfAllEntries(); | 1745 int new_result = CalculateSizeOfAllEntries(); |
| 1737 if (memory_only_ || simple_cache_mode_) { | 1746 EXPECT_EQ(result + last_entry_size + GetEntryMetadataSize(key), new_result); |
| 1738 // TODO(msramek): Implement. | |
| 1739 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result); | |
| 1740 } else { | |
| 1741 EXPECT_EQ(result + last_entry_size + static_cast<int>(key.size()), | |
| 1742 new_result); | |
| 1743 } | |
| 1744 | 1747 |
| 1745 DoomEntry(key); | 1748 DoomEntry(key); |
| 1746 new_result = CalculateSizeOfAllEntries(); | 1749 new_result = CalculateSizeOfAllEntries(); |
| 1747 if (memory_only_ || simple_cache_mode_) { | 1750 EXPECT_EQ(result, new_result); |
| 1748 // TODO(msramek): Implement. | |
| 1749 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, new_result); | |
| 1750 } else { | |
| 1751 EXPECT_EQ(result, new_result); | |
| 1752 } | |
| 1753 } | 1751 } |
| 1754 | 1752 |
| 1755 // After dooming the entries, the size should be back to zero. | 1753 // After dooming the entries, the size should be back to zero. |
| 1756 ASSERT_EQ(net::OK, DoomAllEntries()); | 1754 ASSERT_EQ(net::OK, DoomAllEntries()); |
| 1757 if (memory_only_ || simple_cache_mode_) { | 1755 EXPECT_EQ(0, CalculateSizeOfAllEntries()); |
| 1758 // TODO(msramek): Implement. | |
| 1759 EXPECT_EQ(net::ERR_NOT_IMPLEMENTED, CalculateSizeOfAllEntries()); | |
| 1760 } else { | |
| 1761 EXPECT_EQ(0, CalculateSizeOfAllEntries()); | |
| 1762 } | |
| 1763 } | 1756 } |
| 1764 | 1757 |
| 1765 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) { | 1758 TEST_F(DiskCacheBackendTest, CalculateSizeOfAllEntries) { |
| 1766 BackendCalculateSizeOfAllEntries(); | 1759 BackendCalculateSizeOfAllEntries(); |
| 1767 } | 1760 } |
| 1768 | 1761 |
| 1769 TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) { | 1762 TEST_F(DiskCacheBackendTest, MemoryOnlyCalculateSizeOfAllEntries) { |
| 1770 SetMemoryOnlyMode(); | 1763 SetMemoryOnlyMode(); |
| 1771 BackendCalculateSizeOfAllEntries(); | 1764 BackendCalculateSizeOfAllEntries(); |
| 1772 } | 1765 } |
| (...skipping 1852 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3625 // after closing. | 3618 // after closing. |
| 3626 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 | 3619 // NOTE: IF THIS TEST IS FLAKY THEN IT IS FAILING. See https://crbug.com/416940 |
| 3627 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { | 3620 TEST_F(DiskCacheBackendTest, SimpleCacheDeleteQuickly) { |
| 3628 SetSimpleCacheMode(); | 3621 SetSimpleCacheMode(); |
| 3629 for (int i = 0; i < 100; ++i) { | 3622 for (int i = 0; i < 100; ++i) { |
| 3630 InitCache(); | 3623 InitCache(); |
| 3631 cache_.reset(); | 3624 cache_.reset(); |
| 3632 EXPECT_TRUE(CleanupCacheDir()); | 3625 EXPECT_TRUE(CleanupCacheDir()); |
| 3633 } | 3626 } |
| 3634 } | 3627 } |
| OLD | NEW |