• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
19 /*
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  * 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 
37 #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 
45 #include "NetdClient.h"
46 #include "dns_responder_client_ndk.h"
47 
48 using android::base::StringPrintf;
49 
50 constexpr int MIN_THREADS = 1;
51 constexpr int MAX_THREADS = 32;
52 
53 class DnsFixture : public ::benchmark::Fixture {
54 protected:
55     static constexpr unsigned num_hosts = 1000;
56     DnsResponderClient dns;
57     std::vector<DnsResponderClient::Mapping> mappings;
58     std::vector<std::unique_ptr<test::DNSResponder>> mDns;
59 
60 public:
SetUp(const::benchmark::State & state)61     void SetUp(const ::benchmark::State& state) override {
62         if (state.thread_index == 0) {
63             dns.SetUp();
64 
65             std::vector<std::string> domains = { "example.com" };
66             std::vector<std::string> servers;
67             dns.SetupMappings(num_hosts, domains, &mappings);
68 
69             dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers);
70 
71             const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 1000};
72             dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder);
73         }
74     }
75 
TearDown(const::benchmark::State & state)76     void TearDown(const ::benchmark::State& state) override {
77         if (state.thread_index == 0) {
78             dns.TearDown();
79         }
80     }
81 
getMappings() const82     std::vector<DnsResponderClient::Mapping> const& getMappings() const {
83         return mappings;
84     }
85 
benchmark(benchmark::State & state)86     void benchmark(benchmark::State& state) {
87         while (state.KeepRunning()) {
88             const uint32_t ofs = arc4random_uniform(getMappings().size());
89             const auto& mapping = getMappings()[ofs];
90             addrinfo* result = nullptr;
91             if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) {
92                 state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d",
93                         errno).c_str());
94                 break;
95             }
96             if (result) {
97                 freeaddrinfo(result);
98                 result = nullptr;
99             }
100         }
101     }
102 };
103 
BENCHMARK_DEFINE_F(DnsFixture,getaddrinfo)104 BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo)(benchmark::State& state) {
105     benchmark(state);
106 }
107 BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo)
108     ->ThreadRange(MIN_THREADS, MAX_THREADS)
109     ->UseRealTime();
110