blob: bc1e3ddec18409cbdb3fc56d48ec726324b6f53f [file] [log] [blame]
Robin Lee2cf56172016-09-13 18:55:42 +09001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "dns_benchmark"
18
Robin Lee711237c2016-11-07 19:55:00 +000019/*
20 * See README.md for general notes.
21 *
22 * This set of benchmarks measures the throughput of getaddrinfo() on between 1 and 32 threads for
23 * the purpose of keeping track of the maximum load that netd can reasonably handle.
24 *
25 * The benchmark fixture runs in 3 different modes:
26 *
27 * - getaddrinfo_log_nothing
28 *
29 * The control case. Switches all kinds of DNS events reporting off and runs getaddrinfo() in a
30 * loop until the timer expires.
31 *
32 * This was the default and only mode in all versions before 7.0.
33 *
34 * - getaddrinfo_log_metrics
35 *
36 * DNS Logging Liteā„¢ includes staple favourites such as event type (getaddrinfo/gethostbyname),
37 * return code, and latency, but misses out in-depth information such as resolved IP addresses.
38 *
39 * It is expected that this is a little slower than getaddrinfo_log_nothing because of the
40 * overhead, but not particularly worse, since it is a oneway binder call without too much data
41 * being sent per event.
42 *
43 * This was the default mode between versions 7.0 and 7.1 inclusive.
44 *
45 * - getaddrinfo_log_everything
46 *
47 * DNS Logging, in full HD, includes extra non-metrics fields such as hostname, a truncated
48 * list of resolved addresses, total resolved address count, and originating UID.
49 *
50 * Useful measurements
51 * ===================
52 *
53 * - real_time: the average time taken to make a single getaddrinfo lookup on a local DNS resolver
54 * run by DnsFixture. This will usually be higher on multithreaded tests as threads
55 * block on DNS lookups and Binder connections.
56 *
57 * - iterations: total number of runs finished within the time limit. Higher is better. This is
58 * roughly proportional to MinTime * nThreads / real_time.
59 *
60 */
61
Robin Lee2cf56172016-09-13 18:55:42 +090062#include <netdb.h>
63#include <netinet/in.h>
64#include <sys/types.h>
65#include <sys/socket.h>
66
67#include <android-base/stringprintf.h>
68#include <benchmark/benchmark.h>
69#include <utils/String16.h>
70#include <utils/StrongPointer.h>
71
Robin Lee2cf56172016-09-13 18:55:42 +090072#include "NetdClient.h"
Michal Karpinskid46aa712016-10-13 10:04:36 +010073#include "android/net/metrics/INetdEventListener.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090074#include "dns_responder_client.h"
75#include "netd_resolv/params.h" // MAXNS
Robin Lee2cf56172016-09-13 18:55:42 +090076
77using android::base::StringPrintf;
Michal Karpinskid46aa712016-10-13 10:04:36 +010078using android::net::metrics::INetdEventListener;
Robin Lee2cf56172016-09-13 18:55:42 +090079
80constexpr int MIN_THREADS = 1;
81constexpr int MAX_THREADS = 32;
82
83class DnsFixture : public ::benchmark::Fixture {
84protected:
85 static constexpr unsigned num_hosts = 1000;
86 DnsResponderClient dns;
87 std::vector<DnsResponderClient::Mapping> mappings;
88 std::vector<std::unique_ptr<test::DNSResponder>> mDns;
89
90public:
91 void SetUp(const ::benchmark::State& state) override {
92 if (state.thread_index == 0) {
93 dns.SetUp();
94
95 std::vector<std::string> domains = { "example.com" };
96 std::vector<std::string> servers;
97 dns.SetupMappings(num_hosts, domains, &mappings);
98
99 dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers);
100
Bernie Innocenticc93c372018-08-06 15:18:59 +0900101 const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 100};
Robin Lee2cf56172016-09-13 18:55:42 +0900102 dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder);
103 }
104 }
105
106 void TearDown(const ::benchmark::State& state) override {
107 if (state.thread_index == 0) {
108 dns.ShutdownDNSServers(&mDns);
109 dns.TearDown();
110 }
111 }
112
113 std::vector<DnsResponderClient::Mapping> const& getMappings() const {
114 return mappings;
115 }
116
117 android::sp<android::net::INetd> getNetd() const {
118 return dns.mNetdSrv;
119 }
120
121 void getaddrinfo_until_done(benchmark::State &state) {
122 while (state.KeepRunning()) {
123 const uint32_t ofs = arc4random_uniform(getMappings().size());
124 const auto& mapping = getMappings()[ofs];
125 addrinfo* result = nullptr;
126 if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) {
127 state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d",
128 errno).c_str());
129 break;
130 }
131 if (result) {
132 freeaddrinfo(result);
133 result = nullptr;
134 }
135 }
136 }
137
138 void benchmark_at_reporting_level(benchmark::State &state, int metricsLevel) {
139 const bool isMaster = (state.thread_index == 0);
140 int oldMetricsLevel;
141
142 // SETUP
143 if (isMaster) {
144 auto rv = getNetd()->getMetricsReportingLevel(&oldMetricsLevel);
145 if (!rv.isOk()) {
146 state.SkipWithError(StringPrintf("Failed saving metrics reporting level: %s",
147 rv.toString8().string()).c_str());
148 return;
149 }
150 rv = getNetd()->setMetricsReportingLevel(metricsLevel);
151 if (!rv.isOk()) {
152 state.SkipWithError(StringPrintf("Failed changing metrics reporting: %s",
153 rv.toString8().string()).c_str());
154 return;
155 }
156 }
157
158 // TEST
159 getaddrinfo_until_done(state);
160
161 // TEARDOWN
162 if (isMaster) {
163 auto rv = getNetd()->setMetricsReportingLevel(oldMetricsLevel);
164 if (!rv.isOk()) {
165 state.SkipWithError(StringPrintf("Failed restoring metrics reporting level: %s",
166 rv.toString8().string()).c_str());
167 return;
168 }
169 }
170 }
171};
172
173// DNS calls without any metrics logged or sent.
174BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_nothing)(benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100175 benchmark_at_reporting_level(state, INetdEventListener::REPORTING_LEVEL_NONE);
Robin Lee2cf56172016-09-13 18:55:42 +0900176}
177BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_nothing)
178 ->ThreadRange(MIN_THREADS, MAX_THREADS)
179 ->UseRealTime();
180
181// DNS calls with metrics only (netId, latency, return code) sent to the system server.
182BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_metrics)(benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100183 benchmark_at_reporting_level(state, INetdEventListener::REPORTING_LEVEL_METRICS);
Robin Lee2cf56172016-09-13 18:55:42 +0900184}
185BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_metrics)
186 ->ThreadRange(MIN_THREADS, MAX_THREADS)
187 ->UseRealTime();
188
189// DNS calls with all information logged and sent to the system server.
190BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_everything)(benchmark::State& state) {
Michal Karpinskid46aa712016-10-13 10:04:36 +0100191 benchmark_at_reporting_level(state, INetdEventListener::REPORTING_LEVEL_FULL);
Robin Lee2cf56172016-09-13 18:55:42 +0900192}
193BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_everything)
194 ->ThreadRange(MIN_THREADS, MAX_THREADS)
195 ->UseRealTime();