Skip to content
Bitcoin Roots

v31.1-roots.1 · Commit 7: [PATCH 07/17] feat(rpc): preserve Roots operator interfaces on v31

src/stats/test/stats_tests.cpp

Before: /dev/null · After: src/stats/test/stats_tests.cpp

File metadata
new file mode 100644
index 0000000000000000000000000000000000000000..87c1db502b9b158244990e6954a7a1f02c615523

Download original patch · Release notes

Diff rows 1–83 of 83. Line numbers refer to the original before/after files.

+ Added− RemovedUnmarked lines are context.

Scroll the code area horizontally to read long lines. Before is on the left; after is on the right. Unified view fits narrow screens more easily.

src/stats/test/stats_tests.cpp ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ // Copyright (c) 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 <stats/stats.h>
6
+
7
+ #include <test/util/setup_common.h>
8
+ #include <util/time.h>
9
+
10
+ #include <boost/test/unit_test.hpp>
11
+
12
+ BOOST_FIXTURE_TEST_SUITE(stats_tests, BasicTestingSetup)
13
+
14
+ BOOST_AUTO_TEST_CASE(stats)
15
+ {
16
+ BOOST_CHECK(!CStats::m_stats_enabled);
17
+ CStats::DefaultStats()->setMaxMemoryUsageTarget(CStats::DEFAULT_MAX_STATS_MEMORY);
18
+
19
+ uint64_t start = GetTime();
20
+ SetMockTime(start);
21
+
22
+ CStats::DefaultStats()->addMempoolSample(0, 1, 1);
23
+ SetMockTime(start + 1);
24
+ CStats::DefaultStats()->addMempoolSample(0, 2, 2); //1second should be to short
25
+ SetMockTime(start + 5);
26
+ CStats::DefaultStats()->addMempoolSample(3, 4, 3);
27
+
28
+ uint64_t queryFromTime = start;
29
+ uint64_t queryToTime = start + 3600;
30
+ mempoolSamples_t samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
31
+
32
+ BOOST_CHECK_EQUAL(samples[0].m_time_delta, 0U);
33
+ BOOST_CHECK_EQUAL(samples[1].m_time_delta, 5U);
34
+ BOOST_CHECK_EQUAL(samples[1].m_tx_count, 3);
35
+ BOOST_CHECK_EQUAL(samples[1].m_dyn_mem_usage, 4);
36
+ BOOST_CHECK_EQUAL(samples[1].m_min_fee_per_k, 3);
37
+ BOOST_CHECK_EQUAL(queryFromTime, start);
38
+ BOOST_CHECK_EQUAL(queryToTime, start + 5);
39
+
40
+ // check retrieving a subset of the available samples
41
+ queryFromTime = start;
42
+ queryToTime = start;
43
+ samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
44
+ BOOST_CHECK_EQUAL(samples.size(), 1U);
45
+
46
+ // add some samples
47
+ for (int i = 0; i < 10000; i++) {
48
+ SetMockTime(start + 10 + i * 5);
49
+ CStats::DefaultStats()->addMempoolSample(i, i + 1, i + 2);
50
+ }
51
+
52
+ queryFromTime = start + 3600;
53
+ queryToTime = start + 3600;
54
+ samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
55
+ BOOST_CHECK_EQUAL(samples.size(), 1U); //get a single sample
56
+
57
+ queryFromTime = start;
58
+ queryToTime = start + 3600;
59
+ samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
60
+ BOOST_CHECK(samples.size() >= 3600 / 5);
61
+
62
+ // reduce max memory and add 100 samples to ensure it triggers the cleanup
63
+ CStats::DefaultStats()->setMaxMemoryUsageTarget(10 * 1024);
64
+ for (int i = 10000; i < 10100; i++) {
65
+ SetMockTime(start + 10 + i * 5);
66
+ CStats::DefaultStats()->addMempoolSample(i, i + 1, i + 2);
67
+ }
68
+
69
+ queryFromTime = start;
70
+ queryToTime = start + 100;
71
+ samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
72
+ BOOST_CHECK_EQUAL(samples.size(), 1U);
73
+
74
+ queryFromTime = 0; // no range limits
75
+ queryToTime = 0; // no range limits
76
+ samples = CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime);
77
+ BOOST_CHECK_EQUAL(samples.size() < 1000U, true);
78
+ CStats::DefaultStats()->setMaxMemoryUsageTarget(0);
79
+ BOOST_CHECK(!CStats::m_stats_enabled);
80
+ BOOST_CHECK(CStats::DefaultStats()->mempoolGetValuesInRange(queryFromTime, queryToTime).empty());
81
+ }
82
+
83
+ BOOST_AUTO_TEST_SUITE_END()