Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 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 | |
Robin Lee | 711237c | 2016-11-07 19:55:00 +0000 | [diff] [blame] | 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 | * |
Robin Lee | 711237c | 2016-11-07 19:55:00 +0000 | [diff] [blame] | 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 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 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 | #include <utils/String16.h> |
| 45 | #include <utils/StrongPointer.h> |
| 46 | |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 47 | #include "NetdClient.h" |
Michal Karpinski | d46aa71 | 2016-10-13 10:04:36 +0100 | [diff] [blame] | 48 | #include "android/net/metrics/INetdEventListener.h" |
Bernie Innocenti | 189eb50 | 2018-10-01 23:10:18 +0900 | [diff] [blame] | 49 | #include "dns_responder_client.h" |
| 50 | #include "netd_resolv/params.h" // MAXNS |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 51 | |
| 52 | using android::base::StringPrintf; |
Michal Karpinski | d46aa71 | 2016-10-13 10:04:36 +0100 | [diff] [blame] | 53 | using android::net::metrics::INetdEventListener; |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 54 | |
| 55 | constexpr int MIN_THREADS = 1; |
| 56 | constexpr int MAX_THREADS = 32; |
| 57 | |
| 58 | class DnsFixture : public ::benchmark::Fixture { |
| 59 | protected: |
| 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 | |
| 65 | public: |
| 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 Innocenti | cc93c37 | 2018-08-06 15:18:59 +0900 | [diff] [blame] | 76 | const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 100}; |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 77 | 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 Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame^] | 96 | void benchmark(benchmark::State& state) { |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 97 | 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 Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 112 | }; |
| 113 | |
Bernie Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame^] | 114 | BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo)(benchmark::State& state) { |
| 115 | benchmark(state); |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 116 | } |
Bernie Innocenti | cd25764 | 2018-12-20 15:56:40 +0900 | [diff] [blame^] | 117 | BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo) |
Robin Lee | 2cf5617 | 2016-09-13 18:55:42 +0900 | [diff] [blame] | 118 | ->ThreadRange(MIN_THREADS, MAX_THREADS) |
| 119 | ->UseRealTime(); |