LCOV - code coverage report
Current view: top level - src/test - cuckoocache_tests.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 200 200 100.0 %
Date: 2022-04-21 14:51:19 Functions: 12 12 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 68 68 100.0 %

           Branch data     Line data    Source code
#       1                 :            : // Copyright (c) 2012-2021 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                 :            : #include <cuckoocache.h>
#       5                 :            : #include <random.h>
#       6                 :            : #include <script/sigcache.h>
#       7                 :            : #include <test/util/setup_common.h>
#       8                 :            : 
#       9                 :            : #include <boost/test/unit_test.hpp>
#      10                 :            : 
#      11                 :            : #include <deque>
#      12                 :            : #include <mutex>
#      13                 :            : #include <shared_mutex>
#      14                 :            : #include <thread>
#      15                 :            : #include <vector>
#      16                 :            : 
#      17                 :            : /** Test Suite for CuckooCache
#      18                 :            :  *
#      19                 :            :  *  1. All tests should have a deterministic result (using insecure rand
#      20                 :            :  *  with deterministic seeds)
#      21                 :            :  *  2. Some test methods are templated to allow for easier testing
#      22                 :            :  *  against new versions / comparing
#      23                 :            :  *  3. Results should be treated as a regression test, i.e., did the behavior
#      24                 :            :  *  change significantly from what was expected. This can be OK, depending on
#      25                 :            :  *  the nature of the change, but requires updating the tests to reflect the new
#      26                 :            :  *  expected behavior. For example improving the hit rate may cause some tests
#      27                 :            :  *  using BOOST_CHECK_CLOSE to fail.
#      28                 :            :  *
#      29                 :            :  */
#      30                 :            : BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
#      31                 :            : 
#      32                 :            : /* Test that no values not inserted into the cache are read out of it.
#      33                 :            :  *
#      34                 :            :  * There are no repeats in the first 200000 insecure_GetRandHash calls
#      35                 :            :  */
#      36                 :            : BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
#      37                 :          2 : {
#      38                 :          2 :     SeedInsecureRand(SeedRand::ZEROS);
#      39                 :          2 :     CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
#      40                 :          2 :     size_t megabytes = 4;
#      41                 :          2 :     cc.setup_bytes(megabytes << 20);
#      42         [ +  + ]:     200002 :     for (int x = 0; x < 100000; ++x) {
#      43                 :     200000 :         cc.insert(InsecureRand256());
#      44                 :     200000 :     }
#      45         [ +  + ]:     200002 :     for (int x = 0; x < 100000; ++x) {
#      46                 :     200000 :         BOOST_CHECK(!cc.contains(InsecureRand256(), false));
#      47                 :     200000 :     }
#      48                 :          2 : };
#      49                 :            : 
#      50                 :            : /** This helper returns the hit rate when megabytes*load worth of entries are
#      51                 :            :  * inserted into a megabytes sized cache
#      52                 :            :  */
#      53                 :            : template <typename Cache>
#      54                 :            : static double test_cache(size_t megabytes, double load)
#      55                 :         10 : {
#      56                 :         10 :     SeedInsecureRand(SeedRand::ZEROS);
#      57                 :         10 :     std::vector<uint256> hashes;
#      58                 :         10 :     Cache set{};
#      59                 :         10 :     size_t bytes = megabytes * (1 << 20);
#      60                 :         10 :     set.setup_bytes(bytes);
#      61                 :         10 :     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
#      62                 :         10 :     hashes.resize(n_insert);
#      63         [ +  + ]:     812652 :     for (uint32_t i = 0; i < n_insert; ++i) {
#      64                 :     812642 :         uint32_t* ptr = (uint32_t*)hashes[i].begin();
#      65         [ +  + ]:    7313778 :         for (uint8_t j = 0; j < 8; ++j)
#      66                 :    6501136 :             *(ptr++) = InsecureRand32();
#      67                 :     812642 :     }
#      68                 :            :     /** We make a copy of the hashes because future optimizations of the
#      69                 :            :      * cuckoocache may overwrite the inserted element, so the test is
#      70                 :            :      * "future proofed".
#      71                 :            :      */
#      72                 :         10 :     std::vector<uint256> hashes_insert_copy = hashes;
#      73                 :            :     /** Do the insert */
#      74         [ +  + ]:         10 :     for (const uint256& h : hashes_insert_copy)
#      75                 :     812642 :         set.insert(h);
#      76                 :            :     /** Count the hits */
#      77                 :         10 :     uint32_t count = 0;
#      78         [ +  + ]:         10 :     for (const uint256& h : hashes)
#      79                 :     812642 :         count += set.contains(h, false);
#      80                 :         10 :     double hit_rate = ((double)count) / ((double)n_insert);
#      81                 :         10 :     return hit_rate;
#      82                 :         10 : }
#      83                 :            : 
#      84                 :            : /** The normalized hit rate for a given load.
#      85                 :            :  *
#      86                 :            :  * The semantics are a little confusing, so please see the below
#      87                 :            :  * explanation.
#      88                 :            :  *
#      89                 :            :  * Examples:
#      90                 :            :  *
#      91                 :            :  * 1. at load 0.5, we expect a perfect hit rate, so we multiply by
#      92                 :            :  * 1.0
#      93                 :            :  * 2. at load 2.0, we expect to see half the entries, so a perfect hit rate
#      94                 :            :  * would be 0.5. Therefore, if we see a hit rate of 0.4, 0.4*2.0 = 0.8 is the
#      95                 :            :  * normalized hit rate.
#      96                 :            :  *
#      97                 :            :  * This is basically the right semantics, but has a bit of a glitch depending on
#      98                 :            :  * how you measure around load 1.0 as after load 1.0 your normalized hit rate
#      99                 :            :  * becomes effectively perfect, ignoring freshness.
#     100                 :            :  */
#     101                 :            : static double normalize_hit_rate(double hits, double load)
#     102                 :         10 : {
#     103                 :         10 :     return hits * std::max(load, 1.0);
#     104                 :         10 : }
#     105                 :            : 
#     106                 :            : /** Check the hit rate on loads ranging from 0.1 to 1.6 */
#     107                 :            : BOOST_AUTO_TEST_CASE(cuckoocache_hit_rate_ok)
#     108                 :          2 : {
#     109                 :            :     /** Arbitrarily selected Hit Rate threshold that happens to work for this test
#     110                 :            :      * as a lower bound on performance.
#     111                 :            :      */
#     112                 :          2 :     double HitRateThresh = 0.98;
#     113                 :          2 :     size_t megabytes = 4;
#     114         [ +  + ]:         12 :     for (double load = 0.1; load < 2; load *= 2) {
#     115                 :         10 :         double hits = test_cache<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes, load);
#     116                 :         10 :         BOOST_CHECK(normalize_hit_rate(hits, load) > HitRateThresh);
#     117                 :         10 :     }
#     118                 :          2 : }
#     119                 :            : 
#     120                 :            : 
#     121                 :            : /** This helper checks that erased elements are preferentially inserted onto and
#     122                 :            :  * that the hit rate of "fresher" keys is reasonable*/
#     123                 :            : template <typename Cache>
#     124                 :            : static void test_cache_erase(size_t megabytes)
#     125                 :          2 : {
#     126                 :          2 :     double load = 1;
#     127                 :          2 :     SeedInsecureRand(SeedRand::ZEROS);
#     128                 :          2 :     std::vector<uint256> hashes;
#     129                 :          2 :     Cache set{};
#     130                 :          2 :     size_t bytes = megabytes * (1 << 20);
#     131                 :          2 :     set.setup_bytes(bytes);
#     132                 :          2 :     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
#     133                 :          2 :     hashes.resize(n_insert);
#     134         [ +  + ]:     262146 :     for (uint32_t i = 0; i < n_insert; ++i) {
#     135                 :     262144 :         uint32_t* ptr = (uint32_t*)hashes[i].begin();
#     136         [ +  + ]:    2359296 :         for (uint8_t j = 0; j < 8; ++j)
#     137                 :    2097152 :             *(ptr++) = InsecureRand32();
#     138                 :     262144 :     }
#     139                 :            :     /** We make a copy of the hashes because future optimizations of the
#     140                 :            :      * cuckoocache may overwrite the inserted element, so the test is
#     141                 :            :      * "future proofed".
#     142                 :            :      */
#     143                 :          2 :     std::vector<uint256> hashes_insert_copy = hashes;
#     144                 :            : 
#     145                 :            :     /** Insert the first half */
#     146         [ +  + ]:     131074 :     for (uint32_t i = 0; i < (n_insert / 2); ++i)
#     147                 :     131072 :         set.insert(hashes_insert_copy[i]);
#     148                 :            :     /** Erase the first quarter */
#     149         [ +  + ]:      65538 :     for (uint32_t i = 0; i < (n_insert / 4); ++i)
#     150                 :      65536 :         BOOST_CHECK(set.contains(hashes[i], true));
#     151                 :            :     /** Insert the second half */
#     152         [ +  + ]:     131074 :     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
#     153                 :     131072 :         set.insert(hashes_insert_copy[i]);
#     154                 :            : 
#     155                 :            :     /** elements that we marked as erased but are still there */
#     156                 :          2 :     size_t count_erased_but_contained = 0;
#     157                 :            :     /** elements that we did not erase but are older */
#     158                 :          2 :     size_t count_stale = 0;
#     159                 :            :     /** elements that were most recently inserted */
#     160                 :          2 :     size_t count_fresh = 0;
#     161                 :            : 
#     162         [ +  + ]:      65538 :     for (uint32_t i = 0; i < (n_insert / 4); ++i)
#     163                 :      65536 :         count_erased_but_contained += set.contains(hashes[i], false);
#     164         [ +  + ]:      65538 :     for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
#     165                 :      65536 :         count_stale += set.contains(hashes[i], false);
#     166         [ +  + ]:     131074 :     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
#     167                 :     131072 :         count_fresh += set.contains(hashes[i], false);
#     168                 :            : 
#     169                 :          2 :     double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
#     170                 :          2 :     double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
#     171                 :          2 :     double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
#     172                 :            : 
#     173                 :            :     // Check that our hit_rate_fresh is perfect
#     174                 :          2 :     BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
#     175                 :            :     // Check that we have a more than 2x better hit rate on stale elements than
#     176                 :            :     // erased elements.
#     177                 :          2 :     BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
#     178                 :          2 : }
#     179                 :            : 
#     180                 :            : BOOST_AUTO_TEST_CASE(cuckoocache_erase_ok)
#     181                 :          2 : {
#     182                 :          2 :     size_t megabytes = 4;
#     183                 :          2 :     test_cache_erase<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
#     184                 :          2 : }
#     185                 :            : 
#     186                 :            : template <typename Cache>
#     187                 :            : static void test_cache_erase_parallel(size_t megabytes)
#     188                 :          2 : {
#     189                 :          2 :     double load = 1;
#     190                 :          2 :     SeedInsecureRand(SeedRand::ZEROS);
#     191                 :          2 :     std::vector<uint256> hashes;
#     192                 :          2 :     Cache set{};
#     193                 :          2 :     size_t bytes = megabytes * (1 << 20);
#     194                 :          2 :     set.setup_bytes(bytes);
#     195                 :          2 :     uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
#     196                 :          2 :     hashes.resize(n_insert);
#     197         [ +  + ]:     262146 :     for (uint32_t i = 0; i < n_insert; ++i) {
#     198                 :     262144 :         uint32_t* ptr = (uint32_t*)hashes[i].begin();
#     199         [ +  + ]:    2359296 :         for (uint8_t j = 0; j < 8; ++j)
#     200                 :    2097152 :             *(ptr++) = InsecureRand32();
#     201                 :     262144 :     }
#     202                 :            :     /** We make a copy of the hashes because future optimizations of the
#     203                 :            :      * cuckoocache may overwrite the inserted element, so the test is
#     204                 :            :      * "future proofed".
#     205                 :            :      */
#     206                 :          2 :     std::vector<uint256> hashes_insert_copy = hashes;
#     207                 :          2 :     std::shared_mutex mtx;
#     208                 :            : 
#     209                 :          2 :     {
#     210                 :            :         /** Grab lock to make sure we release inserts */
#     211                 :          2 :         std::unique_lock<std::shared_mutex> l(mtx);
#     212                 :            :         /** Insert the first half */
#     213         [ +  + ]:     131074 :         for (uint32_t i = 0; i < (n_insert / 2); ++i)
#     214                 :     131072 :             set.insert(hashes_insert_copy[i]);
#     215                 :          2 :     }
#     216                 :            : 
#     217                 :            :     /** Spin up 3 threads to run contains with erase.
#     218                 :            :      */
#     219                 :          2 :     std::vector<std::thread> threads;
#     220                 :            :     /** Erase the first quarter */
#     221         [ +  + ]:          8 :     for (uint32_t x = 0; x < 3; ++x)
#     222                 :            :         /** Each thread is emplaced with x copy-by-value
#     223                 :            :         */
#     224                 :          6 :         threads.emplace_back([&, x] {
#     225                 :          6 :             std::shared_lock<std::shared_mutex> l(mtx);
#     226                 :          6 :             size_t ntodo = (n_insert/4)/3;
#     227                 :          6 :             size_t start = ntodo*x;
#     228                 :          6 :             size_t end = ntodo*(x+1);
#     229         [ +  + ]:      65194 :             for (uint32_t i = start; i < end; ++i) {
#     230                 :      65188 :                 bool contains = set.contains(hashes[i], true);
#     231                 :      65188 :                 assert(contains);
#     232                 :      65188 :             }
#     233                 :          6 :         });
#     234                 :            : 
#     235                 :            :     /** Wait for all threads to finish
#     236                 :            :      */
#     237         [ +  + ]:          2 :     for (std::thread& t : threads)
#     238                 :          6 :         t.join();
#     239                 :            :     /** Grab lock to make sure we observe erases */
#     240                 :          2 :     std::unique_lock<std::shared_mutex> l(mtx);
#     241                 :            :     /** Insert the second half */
#     242         [ +  + ]:     131074 :     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
#     243                 :     131072 :         set.insert(hashes_insert_copy[i]);
#     244                 :            : 
#     245                 :            :     /** elements that we marked erased but that are still there */
#     246                 :          2 :     size_t count_erased_but_contained = 0;
#     247                 :            :     /** elements that we did not erase but are older */
#     248                 :          2 :     size_t count_stale = 0;
#     249                 :            :     /** elements that were most recently inserted */
#     250                 :          2 :     size_t count_fresh = 0;
#     251                 :            : 
#     252         [ +  + ]:      65538 :     for (uint32_t i = 0; i < (n_insert / 4); ++i)
#     253                 :      65536 :         count_erased_but_contained += set.contains(hashes[i], false);
#     254         [ +  + ]:      65538 :     for (uint32_t i = (n_insert / 4); i < (n_insert / 2); ++i)
#     255                 :      65536 :         count_stale += set.contains(hashes[i], false);
#     256         [ +  + ]:     131074 :     for (uint32_t i = (n_insert / 2); i < n_insert; ++i)
#     257                 :     131072 :         count_fresh += set.contains(hashes[i], false);
#     258                 :            : 
#     259                 :          2 :     double hit_rate_erased_but_contained = double(count_erased_but_contained) / (double(n_insert) / 4.0);
#     260                 :          2 :     double hit_rate_stale = double(count_stale) / (double(n_insert) / 4.0);
#     261                 :          2 :     double hit_rate_fresh = double(count_fresh) / (double(n_insert) / 2.0);
#     262                 :            : 
#     263                 :            :     // Check that our hit_rate_fresh is perfect
#     264                 :          2 :     BOOST_CHECK_EQUAL(hit_rate_fresh, 1.0);
#     265                 :            :     // Check that we have a more than 2x better hit rate on stale elements than
#     266                 :            :     // erased elements.
#     267                 :          2 :     BOOST_CHECK(hit_rate_stale > 2 * hit_rate_erased_but_contained);
#     268                 :          2 : }
#     269                 :            : BOOST_AUTO_TEST_CASE(cuckoocache_erase_parallel_ok)
#     270                 :          2 : {
#     271                 :          2 :     size_t megabytes = 4;
#     272                 :          2 :     test_cache_erase_parallel<CuckooCache::cache<uint256, SignatureCacheHasher>>(megabytes);
#     273                 :          2 : }
#     274                 :            : 
#     275                 :            : 
#     276                 :            : template <typename Cache>
#     277                 :            : static void test_cache_generations()
#     278                 :          2 : {
#     279                 :            :     // This test checks that for a simulation of network activity, the fresh hit
#     280                 :            :     // rate is never below 99%, and the number of times that it is worse than
#     281                 :            :     // 99.9% are less than 1% of the time.
#     282                 :          2 :     double min_hit_rate = 0.99;
#     283                 :          2 :     double tight_hit_rate = 0.999;
#     284                 :          2 :     double max_rate_less_than_tight_hit_rate = 0.01;
#     285                 :            :     // A cache that meets this specification is therefore shown to have a hit
#     286                 :            :     // rate of at least tight_hit_rate * (1 - max_rate_less_than_tight_hit_rate) +
#     287                 :            :     // min_hit_rate*max_rate_less_than_tight_hit_rate = 0.999*99%+0.99*1% == 99.89%
#     288                 :            :     // hit rate with low variance.
#     289                 :            : 
#     290                 :            :     // We use deterministic values, but this test has also passed on many
#     291                 :            :     // iterations with non-deterministic values, so it isn't "overfit" to the
#     292                 :            :     // specific entropy in FastRandomContext(true) and implementation of the
#     293                 :            :     // cache.
#     294                 :          2 :     SeedInsecureRand(SeedRand::ZEROS);
#     295                 :            : 
#     296                 :            :     // block_activity models a chunk of network activity. n_insert elements are
#     297                 :            :     // added to the cache. The first and last n/4 are stored for removal later
#     298                 :            :     // and the middle n/2 are not stored. This models a network which uses half
#     299                 :            :     // the signatures of recently (since the last block) added transactions
#     300                 :            :     // immediately and never uses the other half.
#     301                 :          2 :     struct block_activity {
#     302                 :          2 :         std::vector<uint256> reads;
#     303                 :          2 :         block_activity(uint32_t n_insert, Cache& c) : reads()
#     304                 :       2620 :         {
#     305                 :       2620 :             std::vector<uint256> inserts;
#     306                 :       2620 :             inserts.resize(n_insert);
#     307                 :       2620 :             reads.reserve(n_insert / 2);
#     308         [ +  + ]:    2622620 :             for (uint32_t i = 0; i < n_insert; ++i) {
#     309                 :    2620000 :                 uint32_t* ptr = (uint32_t*)inserts[i].begin();
#     310         [ +  + ]:   23580000 :                 for (uint8_t j = 0; j < 8; ++j)
#     311                 :   20960000 :                     *(ptr++) = InsecureRand32();
#     312                 :    2620000 :             }
#     313         [ +  + ]:     657620 :             for (uint32_t i = 0; i < n_insert / 4; ++i)
#     314                 :     655000 :                 reads.push_back(inserts[i]);
#     315         [ +  + ]:     657620 :             for (uint32_t i = n_insert - (n_insert / 4); i < n_insert; ++i)
#     316                 :     655000 :                 reads.push_back(inserts[i]);
#     317         [ +  + ]:       2620 :             for (const auto& h : inserts)
#     318                 :    2620000 :                 c.insert(h);
#     319                 :       2620 :         }
#     320                 :          2 :     };
#     321                 :            : 
#     322                 :          2 :     const uint32_t BLOCK_SIZE = 1000;
#     323                 :            :     // We expect window size 60 to perform reasonably given that each epoch
#     324                 :            :     // stores 45% of the cache size (~472k).
#     325                 :          2 :     const uint32_t WINDOW_SIZE = 60;
#     326                 :          2 :     const uint32_t POP_AMOUNT = (BLOCK_SIZE / WINDOW_SIZE) / 2;
#     327                 :          2 :     const double load = 10;
#     328                 :          2 :     const size_t megabytes = 4;
#     329                 :          2 :     const size_t bytes = megabytes * (1 << 20);
#     330                 :          2 :     const uint32_t n_insert = static_cast<uint32_t>(load * (bytes / sizeof(uint256)));
#     331                 :            : 
#     332                 :          2 :     std::vector<block_activity> hashes;
#     333                 :          2 :     Cache set{};
#     334                 :          2 :     set.setup_bytes(bytes);
#     335                 :          2 :     hashes.reserve(n_insert / BLOCK_SIZE);
#     336                 :          2 :     std::deque<block_activity> last_few;
#     337                 :          2 :     uint32_t out_of_tight_tolerance = 0;
#     338                 :          2 :     uint32_t total = n_insert / BLOCK_SIZE;
#     339                 :            :     // we use the deque last_few to model a sliding window of blocks. at each
#     340                 :            :     // step, each of the last WINDOW_SIZE block_activities checks the cache for
#     341                 :            :     // POP_AMOUNT of the hashes that they inserted, and marks these erased.
#     342         [ +  + ]:       2622 :     for (uint32_t i = 0; i < total; ++i) {
#     343         [ +  + ]:       2620 :         if (last_few.size() == WINDOW_SIZE)
#     344                 :       2500 :             last_few.pop_front();
#     345                 :       2620 :         last_few.emplace_back(BLOCK_SIZE, set);
#     346                 :       2620 :         uint32_t count = 0;
#     347         [ +  + ]:       2620 :         for (auto& act : last_few)
#     348         [ +  + ]:    1382940 :             for (uint32_t k = 0; k < POP_AMOUNT; ++k) {
#     349                 :    1229280 :                 count += set.contains(act.reads.back(), true);
#     350                 :    1229280 :                 act.reads.pop_back();
#     351                 :    1229280 :             }
#     352                 :            :         // We use last_few.size() rather than WINDOW_SIZE for the correct
#     353                 :            :         // behavior on the first WINDOW_SIZE iterations where the deque is not
#     354                 :            :         // full yet.
#     355                 :       2620 :         double hit = (double(count)) / (last_few.size() * POP_AMOUNT);
#     356                 :            :         // Loose Check that hit rate is above min_hit_rate
#     357                 :       2620 :         BOOST_CHECK(hit > min_hit_rate);
#     358                 :            :         // Tighter check, count number of times we are less than tight_hit_rate
#     359                 :            :         // (and implicitly, greater than min_hit_rate)
#     360                 :       2620 :         out_of_tight_tolerance += hit < tight_hit_rate;
#     361                 :       2620 :     }
#     362                 :            :     // Check that being out of tolerance happens less than
#     363                 :            :     // max_rate_less_than_tight_hit_rate of the time
#     364                 :          2 :     BOOST_CHECK(double(out_of_tight_tolerance) / double(total) < max_rate_less_than_tight_hit_rate);
#     365                 :          2 : }
#     366                 :            : BOOST_AUTO_TEST_CASE(cuckoocache_generations)
#     367                 :          2 : {
#     368                 :          2 :     test_cache_generations<CuckooCache::cache<uint256, SignatureCacheHasher>>();
#     369                 :          2 : }
#     370                 :            : 
#     371                 :            : BOOST_AUTO_TEST_SUITE_END();

Generated by: LCOV version 0-eol-96201-ge66f56f4af6a