|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
| 5 | +#include <memusage.h> |
5 | 6 | #include <support/allocators/pool.h>
|
6 | 7 | #include <test/util/poolresourcetester.h>
|
7 | 8 | #include <test/util/random.h>
|
@@ -153,4 +154,37 @@ BOOST_AUTO_TEST_CASE(random_allocations)
|
153 | 154 | PoolResourceTester::CheckAllDataAccountedFor(resource);
|
154 | 155 | }
|
155 | 156 |
|
| 157 | +BOOST_AUTO_TEST_CASE(memusage_test) |
| 158 | +{ |
| 159 | + auto std_map = std::unordered_map<int, int>{}; |
| 160 | + |
| 161 | + using Map = std::unordered_map<int, |
| 162 | + int, |
| 163 | + std::hash<int>, |
| 164 | + std::equal_to<int>, |
| 165 | + PoolAllocator<std::pair<const int, int>, |
| 166 | + sizeof(std::pair<const int, int>) + sizeof(void*) * 4, |
| 167 | + alignof(void*)>>; |
| 168 | + auto resource = Map::allocator_type::ResourceType(1024); |
| 169 | + |
| 170 | + PoolResourceTester::CheckAllDataAccountedFor(resource); |
| 171 | + |
| 172 | + { |
| 173 | + auto resource_map = Map{0, std::hash<int>{}, std::equal_to<int>{}, &resource}; |
| 174 | + |
| 175 | + // can't have the same resource usage |
| 176 | + BOOST_TEST(memusage::DynamicUsage(std_map) != memusage::DynamicUsage(resource_map)); |
| 177 | + |
| 178 | + for (size_t i = 0; i < 10000; ++i) { |
| 179 | + std_map[i]; |
| 180 | + resource_map[i]; |
| 181 | + } |
| 182 | + |
| 183 | + // Eventually the resource_map should have a much lower memory usage because it has less malloc overhead |
| 184 | + BOOST_TEST(memusage::DynamicUsage(resource_map) <= memusage::DynamicUsage(std_map) * 90 / 100); |
| 185 | + } |
| 186 | + |
| 187 | + PoolResourceTester::CheckAllDataAccountedFor(resource); |
| 188 | +} |
| 189 | + |
156 | 190 | BOOST_AUTO_TEST_SUITE_END()
|
0 commit comments