OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/estimate_memory_usage.h" |
| 6 |
| 7 #include <stdlib.h> |
| 8 |
| 9 #include "base/memory/ptr_util.h" |
| 10 #include "base/strings/stringprintf.h" |
| 11 #include "base/test/scoped_memory_usage.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 |
| 14 namespace base { |
| 15 namespace trace_event { |
| 16 |
| 17 namespace { |
| 18 |
| 19 // Guaranteed (if at all possible) to hit short-string-optimization. |
| 20 std::string GetShortString() { |
| 21 // All known SSO implementations can accommodate 6-char strings. |
| 22 return std::string("small"); |
| 23 } |
| 24 |
| 25 // Guaranteed to defeat short-string-optimization and allocate on the heap. |
| 26 std::string GetLongString(int seed = 0) { |
| 27 // Definitely won't fit into std::string. |
| 28 constexpr size_t kLongLength = sizeof(std::string) + 1; |
| 29 std::string string(kLongLength, '?'); |
| 30 int random = seed; |
| 31 for (auto& ch: string) { |
| 32 random = random * 1103515245 + 12345; |
| 33 ch = 'a' + abs(random % 26); |
| 34 } |
| 35 return string; |
| 36 } |
| 37 |
| 38 // Compilers will happily optimize allocation / deallocation |
| 39 // completely if you don't use your pointer. This "uses" a |
| 40 // pointer and prevents optimizations. |
| 41 template <class T> |
| 42 void UseAllocatedPointer(T* pointer) { |
| 43 EXPECT_NE(1u, reinterpret_cast<uintptr_t>(pointer)); |
| 44 } |
| 45 |
| 46 } // namespace |
| 47 |
| 48 class EstimateMemoryUsageTest : public testing::Test { |
| 49 public: |
| 50 void SetUp() override { |
| 51 test::ScopedMemoryUsage::Initialize(); |
| 52 } |
| 53 }; |
| 54 |
| 55 #define BEGIN_MEMORY_USAGE_SCOPE \ |
| 56 test::ScopedMemoryUsage scoped_memory_usage |
| 57 |
| 58 // TODO(dskiba): introduce concept of "accuracy", i.e. allow |estimated_usage| |
| 59 // to be off by some % of the real usage. |
| 60 #define EXPECT_MEMORY_USAGE(estimated_usage) \ |
| 61 EXPECT_EQ(scoped_memory_usage.Usage(), estimated_usage) |
| 62 |
| 63 TEST_F(EstimateMemoryUsageTest, String) { |
| 64 // Short |
| 65 { |
| 66 BEGIN_MEMORY_USAGE_SCOPE; |
| 67 std::string string = GetShortString(); |
| 68 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(string)); |
| 69 } |
| 70 |
| 71 // Long |
| 72 { |
| 73 BEGIN_MEMORY_USAGE_SCOPE; |
| 74 std::string string = GetLongString(); |
| 75 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(string)); |
| 76 } |
| 77 |
| 78 // Capacity |
| 79 { |
| 80 BEGIN_MEMORY_USAGE_SCOPE; |
| 81 std::string string; |
| 82 string.reserve(777); |
| 83 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(string)); |
| 84 } |
| 85 } |
| 86 |
| 87 TEST_F(EstimateMemoryUsageTest, Arrays) { |
| 88 // std::array |
| 89 { |
| 90 BEGIN_MEMORY_USAGE_SCOPE; |
| 91 std::array<std::string, 10> array { {GetLongString()} }; |
| 92 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(array)); |
| 93 } |
| 94 |
| 95 // T[N] |
| 96 { |
| 97 BEGIN_MEMORY_USAGE_SCOPE; |
| 98 struct Item { |
| 99 std::unique_ptr<std::string> data; |
| 100 |
| 101 Item(): data(new std::string(GetLongString())) { |
| 102 } |
| 103 size_t EstimateMemoryUsage() const { |
| 104 using base::trace_event::EstimateMemoryUsage; |
| 105 return EstimateMemoryUsage(data); |
| 106 } |
| 107 }; |
| 108 Item array[10]; |
| 109 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(array)); |
| 110 } |
| 111 |
| 112 // C array |
| 113 { |
| 114 BEGIN_MEMORY_USAGE_SCOPE; |
| 115 struct Item { char payload[10]; }; |
| 116 Item* array = new Item[7]; |
| 117 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(array, 7)); |
| 118 UseAllocatedPointer(array); |
| 119 delete[] array; |
| 120 } |
| 121 } |
| 122 |
| 123 TEST_F(EstimateMemoryUsageTest, UniquePtr) { |
| 124 // Empty |
| 125 { |
| 126 BEGIN_MEMORY_USAGE_SCOPE; |
| 127 std::unique_ptr<std::string> ptr; |
| 128 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(ptr)); |
| 129 } |
| 130 |
| 131 // Not empty |
| 132 { |
| 133 BEGIN_MEMORY_USAGE_SCOPE; |
| 134 std::unique_ptr<std::string> ptr(new std::string(GetLongString())); |
| 135 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(ptr)); |
| 136 } |
| 137 |
| 138 // With a pointer |
| 139 { |
| 140 BEGIN_MEMORY_USAGE_SCOPE; |
| 141 std::unique_ptr<std::string*> ptr(new std::string*()); |
| 142 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(ptr)); |
| 143 UseAllocatedPointer(ptr.get()); |
| 144 } |
| 145 |
| 146 // With an array |
| 147 { |
| 148 BEGIN_MEMORY_USAGE_SCOPE; |
| 149 struct Item { int payload[10]; }; |
| 150 std::unique_ptr<Item[]> ptr(new Item[7]); |
| 151 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(ptr, 7)); |
| 152 UseAllocatedPointer(ptr.get()); |
| 153 } |
| 154 } |
| 155 |
| 156 TEST_F(EstimateMemoryUsageTest, Vector) { |
| 157 { |
| 158 BEGIN_MEMORY_USAGE_SCOPE; |
| 159 std::vector<std::string> vector; |
| 160 for (int i = 0; i != 1000; ++i) { |
| 161 vector.push_back(GetLongString(i)); |
| 162 } |
| 163 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(vector)); |
| 164 } |
| 165 |
| 166 // Capacity |
| 167 { |
| 168 BEGIN_MEMORY_USAGE_SCOPE; |
| 169 struct POD { short data; }; |
| 170 std::vector<POD> vector; |
| 171 vector.reserve(1000); |
| 172 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(vector)); |
| 173 } |
| 174 } |
| 175 |
| 176 TEST_F(EstimateMemoryUsageTest, List) { |
| 177 BEGIN_MEMORY_USAGE_SCOPE; |
| 178 struct POD { short data; }; |
| 179 std::list<POD> list; |
| 180 for (int i = 0; i != 1000; ++i) { |
| 181 list.push_back(POD()); |
| 182 } |
| 183 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(list)); |
| 184 } |
| 185 |
| 186 TEST_F(EstimateMemoryUsageTest, Set) { |
| 187 BEGIN_MEMORY_USAGE_SCOPE; |
| 188 std::set<std::pair<int, std::string>> set; |
| 189 for (int i = 0; i != 1000; ++i) { |
| 190 set.insert({i, GetLongString(i)}); |
| 191 } |
| 192 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(set)); |
| 193 } |
| 194 |
| 195 TEST_F(EstimateMemoryUsageTest, MultiSet) { |
| 196 BEGIN_MEMORY_USAGE_SCOPE; |
| 197 std::multiset<bool> set; |
| 198 for (int i = 0; i != 1000; ++i) { |
| 199 set.insert((i & 1) != 0); |
| 200 } |
| 201 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(set)); |
| 202 } |
| 203 |
| 204 TEST_F(EstimateMemoryUsageTest, Map) { |
| 205 BEGIN_MEMORY_USAGE_SCOPE; |
| 206 std::map<std::string, int> map; |
| 207 for (int i = 0; i != 1000; ++i) { |
| 208 map.insert({GetLongString(i), i}); |
| 209 } |
| 210 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(map)); |
| 211 } |
| 212 |
| 213 TEST_F(EstimateMemoryUsageTest, MultiMap) { |
| 214 BEGIN_MEMORY_USAGE_SCOPE; |
| 215 std::multimap<char, std::string> map; |
| 216 for (int i = 0; i != 1000; ++i) { |
| 217 map.insert({static_cast<char>(i), GetLongString(i)}); |
| 218 } |
| 219 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(map)); |
| 220 } |
| 221 |
| 222 TEST_F(EstimateMemoryUsageTest, UnorderedSet) { |
| 223 BEGIN_MEMORY_USAGE_SCOPE; |
| 224 std::unordered_set<std::string> set; |
| 225 for (int i = 0; i != 1000; ++i) { |
| 226 set.insert(GetLongString(i)); |
| 227 } |
| 228 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(set)); |
| 229 } |
| 230 |
| 231 TEST_F(EstimateMemoryUsageTest, UnorderedMultiSet) { |
| 232 BEGIN_MEMORY_USAGE_SCOPE; |
| 233 std::unordered_multiset<short> set; |
| 234 for (int i = 0; i != 1000; ++i) { |
| 235 set.insert(static_cast<short>(i)); |
| 236 } |
| 237 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(set)); |
| 238 } |
| 239 |
| 240 TEST_F(EstimateMemoryUsageTest, UnorderedMap) { |
| 241 BEGIN_MEMORY_USAGE_SCOPE; |
| 242 std::unordered_map<std::string, void*> map; |
| 243 for (int i = 0; i != 1000; ++i) { |
| 244 map.insert({GetLongString(i), reinterpret_cast<void*>(i)}); |
| 245 } |
| 246 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(map)); |
| 247 } |
| 248 |
| 249 TEST_F(EstimateMemoryUsageTest, UnorderedMultiMap) { |
| 250 BEGIN_MEMORY_USAGE_SCOPE; |
| 251 std::unordered_multimap<char, short> map; |
| 252 for (int i = 0; i != 1000; ++i) { |
| 253 map.insert({static_cast<char>(i), 777}); |
| 254 } |
| 255 EXPECT_MEMORY_USAGE(EstimateMemoryUsage(map)); |
| 256 } |
| 257 |
| 258 } // namespace trace_event |
| 259 } // namespace base |
OLD | NEW |