blob: 067d252b3efbb7e00b174fb187590d2a7b6cd297 [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>
Robin Lee2cf56172016-09-13 18:55:42 +090044
Robin Lee2cf56172016-09-13 18:55:42 +090045#include "NetdClient.h"
Bernie Innocenti189eb502018-10-01 23:10:18 +090046#include "dns_responder_client.h"
47#include "netd_resolv/params.h" // MAXNS
Robin Lee2cf56172016-09-13 18:55:42 +090048
49using android::base::StringPrintf;
50
51constexpr int MIN_THREADS = 1;
52constexpr int MAX_THREADS = 32;
53
54class DnsFixture : public ::benchmark::Fixture {
55protected:
56 static constexpr unsigned num_hosts = 1000;
57 DnsResponderClient dns;
58 std::vector<DnsResponderClient::Mapping> mappings;
59 std::vector<std::unique_ptr<test::DNSResponder>> mDns;
60
61public:
62 void SetUp(const ::benchmark::State& state) override {
63 if (state.thread_index == 0) {
64 dns.SetUp();
65
66 std::vector<std::string> domains = { "example.com" };
67 std::vector<std::string> servers;
68 dns.SetupMappings(num_hosts, domains, &mappings);
69
70 dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers);
71
waynema2371eab2019-01-18 14:02:31 +080072 const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 1000};
Robin Lee2cf56172016-09-13 18:55:42 +090073 dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder);
74 }
75 }
76
77 void TearDown(const ::benchmark::State& state) override {
78 if (state.thread_index == 0) {
79 dns.ShutdownDNSServers(&mDns);
80 dns.TearDown();
81 }
82 }
83
84 std::vector<DnsResponderClient::Mapping> const& getMappings() const {
85 return mappings;
86 }
87
Bernie Innocenticd257642018-12-20 15:56:40 +090088 void benchmark(benchmark::State& state) {
Robin Lee2cf56172016-09-13 18:55:42 +090089 while (state.KeepRunning()) {
90 const uint32_t ofs = arc4random_uniform(getMappings().size());
91 const auto& mapping = getMappings()[ofs];
92 addrinfo* result = nullptr;
93 if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) {
94 state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d",
95 errno).c_str());
96 break;
97 }
98 if (result) {
99 freeaddrinfo(result);
100 result = nullptr;
101 }
102 }
103 }
Robin Lee2cf56172016-09-13 18:55:42 +0900104};
105
Bernie Innocenticd257642018-12-20 15:56:40 +0900106BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo)(benchmark::State& state) {
107 benchmark(state);
Robin Lee2cf56172016-09-13 18:55:42 +0900108}
Bernie Innocenticd257642018-12-20 15:56:40 +0900109BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo)
Robin Lee2cf56172016-09-13 18:55:42 +0900110 ->ThreadRange(MIN_THREADS, MAX_THREADS)
111 ->UseRealTime();