Skip to content

Commit b0010c8

Browse files
bench: test select for a new table with only one address
the addrman select function will demonstrate it's worst case performance when it is almost empty, because it might have to linearly search several buckets. add a bench test to cover this case Co-authored-by: Martin Zumsande <mzumsande@gmail.com>
1 parent 9b91aae commit b0010c8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/bench/addrman.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ static CNetAddr ResolveIP(const std::string& ip)
7979
return addr;
8080
}
8181

82+
static CService ResolveService(const std::string& ip, uint16_t port = 0)
83+
{
84+
CService serv;
85+
Lookup(ip, serv, port, false);
86+
return serv;
87+
}
88+
8289
/* Benchmarks */
8390

8491
static void AddrManAdd(benchmark::Bench& bench)
@@ -103,6 +110,22 @@ static void AddrManSelect(benchmark::Bench& bench)
103110
});
104111
}
105112

113+
// The worst case performance of the Select() function is when there is only
114+
// one address on the table, because it linearly searches every position of
115+
// several buckets before identifying the correct bucket
116+
static void AddrManSelectFromAlmostEmpty(benchmark::Bench& bench)
117+
{
118+
AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO};
119+
120+
// Add one address to the new table
121+
CService addr = ResolveService("250.3.1.1", 8333);
122+
addrman.Add({CAddress(addr, NODE_NONE)}, ResolveService("250.3.1.1", 8333));
123+
124+
bench.run([&] {
125+
(void)addrman.Select();
126+
});
127+
}
128+
106129
static void AddrManSelectByNetwork(benchmark::Bench& bench)
107130
{
108131
AddrMan addrman{EMPTY_NETGROUPMAN, /*deterministic=*/false, ADDRMAN_CONSISTENCY_CHECK_RATIO};
@@ -162,6 +185,7 @@ static void AddrManAddThenGood(benchmark::Bench& bench)
162185

163186
BENCHMARK(AddrManAdd, benchmark::PriorityLevel::HIGH);
164187
BENCHMARK(AddrManSelect, benchmark::PriorityLevel::HIGH);
188+
BENCHMARK(AddrManSelectFromAlmostEmpty, benchmark::PriorityLevel::HIGH);
165189
BENCHMARK(AddrManSelectByNetwork, benchmark::PriorityLevel::HIGH);
166190
BENCHMARK(AddrManGetAddr, benchmark::PriorityLevel::HIGH);
167191
BENCHMARK(AddrManAddThenGood, benchmark::PriorityLevel::HIGH);

0 commit comments

Comments
 (0)