blob: ba6e28b3127ee9e52fe696fb21e9533f80e80df7 [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 *
Robin Lee711237c2016-11-07 19:55:00 +000025 * Useful measurements
26 * ===================
27 *
28 * - real_time: the average time taken to make a single getaddrinfo lookup on a local DNS resolver
29 * run by DnsFixture. This will usually be higher on multithreaded tests as threads
30 * block on DNS lookups and Binder connections.
31 *
32 * - iterations: total number of runs finished within the time limit. Higher is better. This is
33 * roughly proportional to MinTime * nThreads / real_time.
34 *
35 */
36
Robin Lee2cf56172016-09-13 18:55:42 +090037#include <netdb.h>
38#include <netinet/in.h>
39#include <sys/types.h>
40#include <sys/socket.h>
41
42#include <android-base/stringprintf.h>
43#include <benchmark/benchmark.h>
44#include <utils/String16.h>
45#include <utils/StrongPointer.h>
46
Robin Lee2cf56172016-09-13 18:55:42 +090047#include "NetdClient.h"
Michal Karpinskid46aa712016-10-13 10:04:36 +010048#include "android/net/metrics/INetdEventListener.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090049#include "dns_responder_client.h"
50#include "netd_resolv/params.h" // MAXNS
Robin Lee2cf56172016-09-13 18:55:42 +090051
52using android::base::StringPrintf;
Michal Karpinskid46aa712016-10-13 10:04:36 +010053using android::net::metrics::INetdEventListener;
Robin Lee2cf56172016-09-13 18:55:42 +090054
55constexpr int MIN_THREADS = 1;
56constexpr int MAX_THREADS = 32;
57
58class DnsFixture : public ::benchmark::Fixture {
59protected:
60 static constexpr unsigned num_hosts = 1000;
61 DnsResponderClient dns;
62 std::vector<DnsResponderClient::Mapping> mappings;
63 std::vector<std::unique_ptr<test::DNSResponder>> mDns;
64
65public:
66 void SetUp(const ::benchmark::State& state) override {
67 if (state.thread_index == 0) {
68 dns.SetUp();
69
70 std::vector<std::string> domains = { "example.com" };
71 std::vector<std::string> servers;
72 dns.SetupMappings(num_hosts, domains, &mappings);
73
74 dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers);
75
Bernie Innocenticc93c372018-08-06 15:18:59 +090076 const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 100};
Robin Lee2cf56172016-09-13 18:55:42 +090077 dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder);
78 }
79 }
80
81 void TearDown(const ::benchmark::State& state) override {
82 if (state.thread_index == 0) {
83 dns.ShutdownDNSServers(&mDns);
84 dns.TearDown();
85 }
86 }
87
88 std::vector<DnsResponderClient::Mapping> const& getMappings() const {
89 return mappings;
90 }
91
92 android::sp<android::net::INetd> getNetd() const {
93 return dns.mNetdSrv;
94 }
95
Bernie Innocenticd257642018-12-20 15:56:40 +090096 void benchmark(benchmark::State& state) {
Robin Lee2cf56172016-09-13 18:55:42 +090097 while (state.KeepRunning()) {
98 const uint32_t ofs = arc4random_uniform(getMappings().size());
99 const auto& mapping = getMappings()[ofs];
100 addrinfo* result = nullptr;
101 if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) {
102 state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d",
103 errno).c_str());
104 break;
105 }
106 if (result) {
107 freeaddrinfo(result);
108 result = nullptr;
109 }
110 }
111 }
Robin Lee2cf56172016-09-13 18:55:42 +0900112};
113
Bernie Innocenticd257642018-12-20 15:56:40 +0900114BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo)(benchmark::State& state) {
115 benchmark(state);
Robin Lee2cf56172016-09-13 18:55:42 +0900116}
Bernie Innocenticd257642018-12-20 15:56:40 +0900117BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo)
Robin Lee2cf56172016-09-13 18:55:42 +0900118 ->ThreadRange(MIN_THREADS, MAX_THREADS)
119 ->UseRealTime();