PIVX Core  5.6.99
P2P Digital Currency
cuckoocache_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2012-2016 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include "cuckoocache.h"
6 #include "script/sigcache.h"
7 #include "test/test_pivx.h"
8 #include "random.h"
9 
10 #include <thread>
11 
12 #include <boost/test/unit_test.hpp>
13 
28 BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
29 
30 /* Test that no values not inserted into the cache are read out of it.
31  *
32  * There are no repeats in the first 200000 insecure_GetRandHash calls
33  */
34 BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
35 {
36  SeedInsecureRand(SeedRand::ZEROS);
38  size_t megabytes = 4;
39  cc.setup_bytes(megabytes << 20);
40  uint256 v;
41  for (int x = 0; x < 100000; ++x) {
42  cc.insert(InsecureRand256());
43  }
44  for (int x = 0; x < 100000; ++x) {
45  BOOST_CHECK(!cc.contains(InsecureRand256(), false));
46  }
47 };
48 
52 template <typename Cache>
53 double test_cache(size_t megabytes, double load)
54 {
55  SeedInsecureRand(SeedRand::ZEROS);
56  std::vector<uint256> hashes;
57  Cache set{};
58  size_t bytes = megabytes * (1 << 20);
59  set.setup_bytes(bytes);
60  uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
61  hashes.resize(n_insert);
62  for (uint32_t i = 0; i < n_insert; ++i) {
63  uint32_t* ptr = (uint32_t*)hashes[i].begin();
64  for (uint8_t j = 0; j < 8; ++j)
65  *(ptr++) = InsecureRand32();
66  }
71  std::vector<uint256> hashes_insert_copy = hashes;
73  for (uint256& h : hashes_insert_copy)
74  set.insert(h);
76  uint32_t count = 0;
77  for (uint256& h : hashes)
78  count += set.contains(h, false);
79  double hit_rate = ((double)count) / ((double)n_insert);
80  return hit_rate;
81 }
82 
100 double normalize_hit_rate(double hits, double load)
101 {
102  return hits * std::max(load, 1.0);
103 }
104 
106 BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
107 {
111  double HitRateThresh = 0.98;
112  size_t megabytes = 4;
113  for (double load = 0.1; load < 2; load *= 2) {
114  double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
115  BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
116  }
117 }
118 
119 
122 template <typename Cache>
123 void test_cache_erase(size_t megabytes)
124 {
125  double load = 1;
126  SeedInsecureRand(SeedRand::ZEROS);
127  std::vector<uint256> hashes;
128  Cache set{};
129  size_t bytes = megabytes * (1 << 20);
130  set.setup_bytes(bytes);
131  uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
132  hashes.resize(n_insert);
133  for (uint32_t i = 0; i < n_insert; ++i) {
134  uint32_t* ptr = (uint32_t*)hashes[i].begin();
135  for (uint8_t j = 0; j < 8; ++j)
136  *(ptr++) = InsecureRand32();
137  }
142  std::vector<uint256> hashes_insert_copy = hashes;
143 
145  for (uint32_t i = 0; i < (n_insert / 2); ++i)
146  set.insert(hashes_insert_copy[i]);
148  for (uint32_t i = 0; i < (n_insert / 4); ++i)
149  set.contains(hashes[i], true);
151  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
152  set.insert(hashes_insert_copy[i]);
153 
155  size_t count_erased_but_contained = 0;
157  size_t count_stale = 0;
159  size_t count_fresh = 0;
160 
161  for (uint32_t i = 0; i < (n_insert / 4); ++i)
162  count_erased_but_contained += set.contains(hashes[i], false);
163  for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
164  count_stale += set.contains(hashes[i], false);
165  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
166  count_fresh += set.contains(hashes[i], false);
167 
168  double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
169  double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
170  double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
171 
172  // Check that our hit_rate_fresh is perfect
173  BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
174  // Check that we have a more than 2x better hit rate on stale elements than
175  // erased elements.
176  BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
177 }
178 
179 BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
180 {
181  size_t megabytes = 4;
182  test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
183 }
184 
185 template <typename Cache>
186 void test_cache_erase_parallel(size_t megabytes)
187 {
188  double load = 1;
189  SeedInsecureRand(SeedRand::ZEROS);
190  std::vector<uint256> hashes;
191  Cache set{};
192  size_t bytes = megabytes * (1 << 20);
193  set.setup_bytes(bytes);
194  uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
195  hashes.resize(n_insert);
196  for (uint32_t i = 0; i < n_insert; ++i) {
197  uint32_t* ptr = (uint32_t*)hashes[i].begin();
198  for (uint8_t j = 0; j < 8; ++j)
199  *(ptr++) = InsecureRand32();
200  }
205  std::vector<uint256> hashes_insert_copy = hashes;
206  boost::shared_mutex mtx;
207 
208  {
210  boost::unique_lock<boost::shared_mutex> l(mtx);
212  for (uint32_t i = 0; i < (n_insert / 2); ++i)
213  set.insert(hashes_insert_copy[i]);
214  }
215 
218  std::vector<std::thread> threads;
220  for (uint32_t x = 0; x < 3; ++x)
223  threads.emplace_back([&, x] {
224  boost::shared_lock<boost::shared_mutex> l(mtx);
225  size_t ntodo = (n_insert/4)/3;
226  size_t start = ntodo*x;
227  size_t end = ntodo*(x+1);
228  for (uint32_t i = start; i < end; ++i)
229  set.contains(hashes[i], true);
230  });
231 
234  for (std::thread& t : threads)
235  t.join();
237  boost::unique_lock<boost::shared_mutex> l(mtx);
239  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
240  set.insert(hashes_insert_copy[i]);
241 
243  size_t count_erased_but_contained = 0;
245  size_t count_stale = 0;
247  size_t count_fresh = 0;
248 
249  for (uint32_t i = 0; i < (n_insert / 4); ++i)
250  count_erased_but_contained += set.contains(hashes[i], false);
251  for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
252  count_stale += set.contains(hashes[i], false);
253  for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
254  count_fresh += set.contains(hashes[i], false);
255 
256  double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
257  double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
258  double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
259 
260  // Check that our hit_rate_fresh is perfect
261  BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
262  // Check that we have a more than 2x better hit rate on stale elements than
263  // erased elements.
264  BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
265 }
266 BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
267 {
268  size_t megabytes = 4;
269  test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
270 }
271 
272 
273 template <typename Cache>
275 {
276  // This test checks that for a simulation of network activity, the fresh hit
277  // rate is never below 99%, and the number of times that it is worse than
278  // 99.9% are less than 1% of the time.
279  double min_hit_rate = 0.99;
280  double tight_hit_rate = 0.999;
281  double max_rate_less_than_tight_hit_rate = 0.01;
282  // A cache that meets this specification is therefore shown to have a hit
283  // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) +
284  // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89%
285  // hit rate with low variance.
286 
287  // We use deterministic values, but this test has also passed on many
288  // iterations with non-deterministic values, so it isn't "overfit" to the
289  // specific entropy in FastRandomContext(true) and implementation of the
290  // cache.
291  SeedInsecureRand(SeedRand::ZEROS);
292 
293  // block_activity models a chunk of network activity. n_insert elements are
294  // adde to the cache. The first and last n/4 are stored for removal later
295  // and the middle n/2 are not stored. This models a network which uses half
296  // the signatures of recently (since the last block) added transactions
297  // immediately and never uses the other half.
298  struct block_activity {
299  std::vector<uint256> reads;
300  block_activity(uint32_t n_insert, Cache& c) : reads()
301  {
302  std::vector<uint256> inserts;
303  inserts.resize(n_insert);
304  reads.reserve(n_insert / 2);
305  for (uint32_t i = 0; i < n_insert; ++i) {
306  uint32_t* ptr = (uint32_t*)inserts[i].begin();
307  for (uint8_t j = 0; j < 8; ++j)
308  *(ptr++) = InsecureRand32();
309  }
310  for (uint32_t i = 0; i < n_insert / 4; ++i)
311  reads.push_back(inserts[i]);
312  for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
313  reads.push_back(inserts[i]);
314  for (auto h : inserts)
315  c.insert(h);
316  }
317  };
318 
319  const uint32_t BLOCK_SIZE = 1000;
320  // We expect window size 60 to perform reasonably given that each epoch
321  // stores 45% of the cache size (~472k).
322  const uint32_t WINDOW_SIZE = 60;
323  const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
324  const double load = 10;
325  const size_t megabytes = 4;
326  const size_t bytes = megabytes * (1 << 20);
327  const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
328 
329  std::vector<block_activity> hashes;
330  Cache set{};
331  set.setup_bytes(bytes);
332  hashes.reserve(n_insert / BLOCK_SIZE);
333  std::deque<block_activity> last_few;
334  uint32_t out_of_tight_tolerance = 0;
335  uint32_t total = n_insert / BLOCK_SIZE;
336  // we use the deque last_few to model a sliding window of blocks. at each
337  // step, each of the last WINDOW_SIZE block_activities checks the cache for
338  // POP_AMOUNT of the hashes that they inserted, and marks these erased.
339  for (uint32_t i = 0; i < total; ++i) {
340  if (last_few.size() == WINDOW_SIZE)
341  last_few.pop_front();
342  last_few.emplace_back(BLOCK_SIZE, set);
343  uint32_t count = 0;
344  for (auto& act : last_few)
345  for (uint32_t k = 0; k < POP_AMOUNT; ++k) {
346  count += set.contains(act.reads.back(), true);
347  act.reads.pop_back();
348  }
349  // We use last_few.size() rather than WINDOW_SIZE for the correct
350  // behavior on the first WINDOW_SIZE iterations where the deque is not
351  // full yet.
352  double hit = (double(count)) / (last_few.size() * POP_AMOUNT);
353  // Loose Check that hit rate is above min_hit_rate
354  BOOST_CHECK(hit > min_hit_rate);
355  // Tighter check, count number of times we are less than tight_hit_rate
356  // (and implicityly, greater than min_hit_rate)
357  out_of_tight_tolerance += hit < tight_hit_rate;
358  }
359  // Check that being out of tolerance happens less than
360  // max_rate_less_than_tight_hit_rate of the time
361  BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
362 }
363 BOOST_AUTO_TEST_CASE(cuckoocache_generations)
364 {
365  test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
366 }
367 
cache implements a cache with properties similar to a cuckoo-set
Definition: cuckoocache.h:161
uint32_t setup_bytes(size_t bytes)
setup_bytes is a convenience function which accounts for internal memory usage when deciding how many...
Definition: cuckoocache.h:343
256-bit opaque blob.
Definition: uint256.h:138
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
double normalize_hit_rate(double hits, double load)
The normalized hit rate for a given load.
double test_cache(size_t megabytes, double load)
This helper returns the hit rate when megabytes*load worth of entries are inserted into a megabytes s...
void test_cache_erase(size_t megabytes)
This helper checks that erased elements are preferentially inserted onto and that the hit rate of "fr...
void test_cache_erase_parallel(size_t megabytes)
BOOST_AUTO_TEST_SUITE(cuckoocache_tests)
Test Suite for CuckooCache.
void test_cache_generations()
BOOST_AUTO_TEST_SUITE_END()
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
@ ZEROS
Seed with a compile time constant of zeros.