caches_tests.cpp raw
1 // Copyright (c) The Limenka developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or https://opensource.org/license/mit.
4
5 #include <node/dbcache.h>
6 #include <util/byte_units.h>
7
8 #include <boost/test/unit_test.hpp>
9
10 #include <array>
11 #include <cstdint>
12 #include <limits>
13 #include <utility>
14
15 using namespace node;
16
17 namespace {
18 void CheckDbCacheWarnThreshold(uint64_t threshold, uint64_t total_ram)
19 {
20 BOOST_CHECK(!ShouldWarnOversizedDbCache(threshold, total_ram));
21 BOOST_CHECK( ShouldWarnOversizedDbCache(threshold + 1, total_ram));
22 }
23 } // namespace
24
25 BOOST_AUTO_TEST_SUITE(caches_tests)
26
27 BOOST_AUTO_TEST_CASE(default_dbcache_formula_by_total_ram)
28 {
29 BOOST_CHECK(FALLBACK_RAM_BYTES >= 1_GiB);
30 for (const auto& [total_ram, expected] : std::array<std::pair<uint64_t, uint64_t>, 4>{{
31 {512_MiB, MIN_DEFAULT_DBCACHE},
32 {1_GiB, MIN_DEFAULT_DBCACHE},
33 {RESERVED_RAM - 1, MIN_DEFAULT_DBCACHE},
34 {RESERVED_RAM, MIN_DEFAULT_DBCACHE}
35 }}) {
36 BOOST_CHECK_EQUAL(GetDefaultDBCache(total_ram), expected);
37 }
38
39 BOOST_CHECK_EQUAL(GetDefaultDBCache(3_GiB), 256_MiB);
40
41 if constexpr (SIZE_MAX > UINT32_MAX) {
42 for (const auto& [total_ram_64, expected] : std::array<std::pair<uint64_t, uint64_t>, 3>{{
43 {8_GiB, 1536_MiB},
44 {16_GiB, MAX_DEFAULT_DBCACHE},
45 {32_GiB, MAX_DEFAULT_DBCACHE}
46 }}) {
47 BOOST_CHECK_EQUAL(GetDefaultDBCache(total_ram_64), expected);
48 }
49 }
50 }
51
52 BOOST_AUTO_TEST_CASE(default_dbcache_uses_current_total_ram)
53 {
54 BOOST_CHECK_EQUAL(GetDefaultDBCache(), GetDefaultDBCache(GetTotalRam()));
55 }
56
57 BOOST_AUTO_TEST_CASE(oversized_dbcache_warning)
58 {
59 BOOST_CHECK(!ShouldWarnOversizedDbCache(MIN_DBCACHE_BYTES, 1_GiB));
60
61 // Below RESERVED_RAM the auto default dominates (headroom is zero).
62 CheckDbCacheWarnThreshold(GetDefaultDBCache(1_GiB), 1_GiB);
63 CheckDbCacheWarnThreshold(GetDefaultDBCache(RESERVED_RAM), RESERVED_RAM);
64
65 // Above RESERVED_RAM the warning fires at 75% of the headroom.
66 CheckDbCacheWarnThreshold(((3_GiB - RESERVED_RAM) / 4) * 3, 3_GiB);
67
68 for (const auto total_ram : {8_GiB, 16_GiB, 32_GiB}) {
69 CheckDbCacheWarnThreshold(((total_ram - RESERVED_RAM) / 4) * 3, total_ram);
70 }
71 }
72
73 BOOST_AUTO_TEST_CASE(default_dbcache_never_warns)
74 {
75 for (const auto total_ram : {1_GiB, 2_GiB, 3_GiB}) {
76 BOOST_CHECK(!ShouldWarnOversizedDbCache(GetDefaultDBCache(total_ram), total_ram));
77 }
78
79 for (const auto total_ram : {4_GiB, 8_GiB, 16_GiB, 32_GiB}) {
80 BOOST_CHECK(!ShouldWarnOversizedDbCache(GetDefaultDBCache(total_ram), total_ram));
81 }
82 }
83
84 BOOST_AUTO_TEST_SUITE_END()
85