|
| 1 | +// Copyright (c) 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 | + |
| 5 | +#include <bench/bench.h> |
| 6 | +#include <net.h> |
| 7 | +#include <netaddress.h> |
| 8 | +#include <random.h> |
| 9 | +#include <test/util/net.h> |
| 10 | +#include <test/util/setup_common.h> |
| 11 | + |
| 12 | +#include <algorithm> |
| 13 | +#include <functional> |
| 14 | +#include <vector> |
| 15 | + |
| 16 | +static void EvictionProtectionCommon( |
| 17 | + benchmark::Bench& bench, |
| 18 | + int num_candidates, |
| 19 | + std::function<void(NodeEvictionCandidate&)> candidate_setup_fn) |
| 20 | +{ |
| 21 | + using Candidates = std::vector<NodeEvictionCandidate>; |
| 22 | + FastRandomContext random_context{true}; |
| 23 | + bench.warmup(100).epochIterations(1100); |
| 24 | + |
| 25 | + Candidates candidates{GetRandomNodeEvictionCandidates(num_candidates, random_context)}; |
| 26 | + for (auto& c : candidates) { |
| 27 | + candidate_setup_fn(c); |
| 28 | + } |
| 29 | + |
| 30 | + std::vector<Candidates> copies{bench.epochs() * bench.epochIterations(), candidates}; |
| 31 | + size_t i{0}; |
| 32 | + bench.run([&] { |
| 33 | + ProtectEvictionCandidatesByRatio(copies.at(i)); |
| 34 | + ++i; |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +/* Benchmarks */ |
| 39 | + |
| 40 | +static void EvictionProtection0Networks250Candidates(benchmark::Bench& bench) |
| 41 | +{ |
| 42 | + EvictionProtectionCommon( |
| 43 | + bench, |
| 44 | + 250 /* num_candidates */, |
| 45 | + [](NodeEvictionCandidate& c) { |
| 46 | + c.nTimeConnected = c.id; |
| 47 | + c.m_network = NET_IPV4; |
| 48 | + }); |
| 49 | +} |
| 50 | + |
| 51 | +static void EvictionProtection1Networks250Candidates(benchmark::Bench& bench) |
| 52 | +{ |
| 53 | + EvictionProtectionCommon( |
| 54 | + bench, |
| 55 | + 250 /* num_candidates */, |
| 56 | + [](NodeEvictionCandidate& c) { |
| 57 | + c.nTimeConnected = c.id; |
| 58 | + c.m_is_local = false; |
| 59 | + if (c.id >= 130 && c.id < 240) { // 110 Tor |
| 60 | + c.m_network = NET_ONION; |
| 61 | + } else { |
| 62 | + c.m_network = NET_IPV4; |
| 63 | + } |
| 64 | + }); |
| 65 | +} |
| 66 | + |
| 67 | +static void EvictionProtection2Networks250Candidates(benchmark::Bench& bench) |
| 68 | +{ |
| 69 | + EvictionProtectionCommon( |
| 70 | + bench, |
| 71 | + 250 /* num_candidates */, |
| 72 | + [](NodeEvictionCandidate& c) { |
| 73 | + c.nTimeConnected = c.id; |
| 74 | + c.m_is_local = false; |
| 75 | + if (c.id >= 90 && c.id < 160) { // 70 Tor |
| 76 | + c.m_network = NET_ONION; |
| 77 | + } else if (c.id >= 170 && c.id < 250) { // 80 I2P |
| 78 | + c.m_network = NET_I2P; |
| 79 | + } else { |
| 80 | + c.m_network = NET_IPV4; |
| 81 | + } |
| 82 | + }); |
| 83 | +} |
| 84 | + |
| 85 | +static void EvictionProtection3Networks050Candidates(benchmark::Bench& bench) |
| 86 | +{ |
| 87 | + EvictionProtectionCommon( |
| 88 | + bench, |
| 89 | + 50 /* num_candidates */, |
| 90 | + [](NodeEvictionCandidate& c) { |
| 91 | + c.nTimeConnected = c.id; |
| 92 | + c.m_is_local = (c.id == 28 || c.id == 47); // 2 localhost |
| 93 | + if (c.id >= 30 && c.id < 47) { // 17 I2P |
| 94 | + c.m_network = NET_I2P; |
| 95 | + } else if (c.id >= 24 && c.id < 28) { // 4 Tor |
| 96 | + c.m_network = NET_ONION; |
| 97 | + } else { |
| 98 | + c.m_network = NET_IPV4; |
| 99 | + } |
| 100 | + }); |
| 101 | +} |
| 102 | + |
| 103 | +static void EvictionProtection3Networks100Candidates(benchmark::Bench& bench) |
| 104 | +{ |
| 105 | + EvictionProtectionCommon( |
| 106 | + bench, |
| 107 | + 100 /* num_candidates */, |
| 108 | + [](NodeEvictionCandidate& c) { |
| 109 | + c.nTimeConnected = c.id; |
| 110 | + c.m_is_local = (c.id >= 55 && c.id < 60); // 5 localhost |
| 111 | + if (c.id >= 70 && c.id < 80) { // 10 I2P |
| 112 | + c.m_network = NET_I2P; |
| 113 | + } else if (c.id >= 80 && c.id < 96) { // 16 Tor |
| 114 | + c.m_network = NET_ONION; |
| 115 | + } else { |
| 116 | + c.m_network = NET_IPV4; |
| 117 | + } |
| 118 | + }); |
| 119 | +} |
| 120 | + |
| 121 | +static void EvictionProtection3Networks250Candidates(benchmark::Bench& bench) |
| 122 | +{ |
| 123 | + EvictionProtectionCommon( |
| 124 | + bench, |
| 125 | + 250 /* num_candidates */, |
| 126 | + [](NodeEvictionCandidate& c) { |
| 127 | + c.nTimeConnected = c.id; |
| 128 | + c.m_is_local = (c.id >= 140 && c.id < 160); // 20 localhost |
| 129 | + if (c.id >= 170 && c.id < 180) { // 10 I2P |
| 130 | + c.m_network = NET_I2P; |
| 131 | + } else if (c.id >= 190 && c.id < 240) { // 50 Tor |
| 132 | + c.m_network = NET_ONION; |
| 133 | + } else { |
| 134 | + c.m_network = NET_IPV4; |
| 135 | + } |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +// Candidate numbers used for the benchmarks: |
| 140 | +// - 50 candidates simulates a possible use of -maxconnections |
| 141 | +// - 100 candidates approximates an average node with default settings |
| 142 | +// - 250 candidates is the number of peers reported by operators of busy nodes |
| 143 | + |
| 144 | +// No disadvantaged networks, with 250 eviction candidates. |
| 145 | +BENCHMARK(EvictionProtection0Networks250Candidates); |
| 146 | + |
| 147 | +// 1 disadvantaged network (Tor) with 250 eviction candidates. |
| 148 | +BENCHMARK(EvictionProtection1Networks250Candidates); |
| 149 | + |
| 150 | +// 2 disadvantaged networks (I2P, Tor) with 250 eviction candidates. |
| 151 | +BENCHMARK(EvictionProtection2Networks250Candidates); |
| 152 | + |
| 153 | +// 3 disadvantaged networks (I2P/localhost/Tor) with 50/100/250 eviction candidates. |
| 154 | +BENCHMARK(EvictionProtection3Networks050Candidates); |
| 155 | +BENCHMARK(EvictionProtection3Networks100Candidates); |
| 156 | +BENCHMARK(EvictionProtection3Networks250Candidates); |
0 commit comments