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 | |
| 19 | #include <netdb.h> |
| 20 | #include <netinet/in.h> |
| 21 | #include <sys/types.h> |
| 22 | #include <sys/socket.h> |
| 23 | |
| 24 | #include <android-base/stringprintf.h> |
| 25 | #include <benchmark/benchmark.h> |
| 26 | #include <utils/String16.h> |
| 27 | #include <utils/StrongPointer.h> |
| 28 | |
| 29 | #include "dns_responder_client.h" |
| 30 | #include "NetdClient.h" |
| 31 | |
| 32 | using android::base::StringPrintf; |
| 33 | |
| 34 | constexpr int MIN_THREADS = 1; |
| 35 | constexpr int MAX_THREADS = 32; |
| 36 | |
| 37 | class DnsFixture : public ::benchmark::Fixture { |
| 38 | protected: |
| 39 | static constexpr unsigned num_hosts = 1000; |
| 40 | DnsResponderClient dns; |
| 41 | std::vector<DnsResponderClient::Mapping> mappings; |
| 42 | std::vector<std::unique_ptr<test::DNSResponder>> mDns; |
| 43 | |
| 44 | public: |
| 45 | void SetUp(const ::benchmark::State& state) override { |
| 46 | if (state.thread_index == 0) { |
| 47 | dns.SetUp(); |
| 48 | |
| 49 | std::vector<std::string> domains = { "example.com" }; |
| 50 | std::vector<std::string> servers; |
| 51 | dns.SetupMappings(num_hosts, domains, &mappings); |
| 52 | |
| 53 | dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers); |
| 54 | |
| 55 | const std::vector<int> mDefaultParams_Binder = { 300, 25, 8, 8 }; |
| 56 | dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void TearDown(const ::benchmark::State& state) override { |
| 61 | if (state.thread_index == 0) { |
| 62 | dns.ShutdownDNSServers(&mDns); |
| 63 | dns.TearDown(); |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | std::vector<DnsResponderClient::Mapping> const& getMappings() const { |
| 68 | return mappings; |
| 69 | } |
| 70 | |
| 71 | android::sp<android::net::INetd> getNetd() const { |
| 72 | return dns.mNetdSrv; |
| 73 | } |
| 74 | |
| 75 | void getaddrinfo_until_done(benchmark::State &state) { |
| 76 | while (state.KeepRunning()) { |
| 77 | const uint32_t ofs = arc4random_uniform(getMappings().size()); |
| 78 | const auto& mapping = getMappings()[ofs]; |
| 79 | addrinfo* result = nullptr; |
| 80 | if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) { |
| 81 | state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d", |
| 82 | errno).c_str()); |
| 83 | break; |
| 84 | } |
| 85 | if (result) { |
| 86 | freeaddrinfo(result); |
| 87 | result = nullptr; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | void benchmark_at_reporting_level(benchmark::State &state, int metricsLevel) { |
| 93 | const bool isMaster = (state.thread_index == 0); |
| 94 | int oldMetricsLevel; |
| 95 | |
| 96 | // SETUP |
| 97 | if (isMaster) { |
| 98 | auto rv = getNetd()->getMetricsReportingLevel(&oldMetricsLevel); |
| 99 | if (!rv.isOk()) { |
| 100 | state.SkipWithError(StringPrintf("Failed saving metrics reporting level: %s", |
| 101 | rv.toString8().string()).c_str()); |
| 102 | return; |
| 103 | } |
| 104 | rv = getNetd()->setMetricsReportingLevel(metricsLevel); |
| 105 | if (!rv.isOk()) { |
| 106 | state.SkipWithError(StringPrintf("Failed changing metrics reporting: %s", |
| 107 | rv.toString8().string()).c_str()); |
| 108 | return; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | // TEST |
| 113 | getaddrinfo_until_done(state); |
| 114 | |
| 115 | // TEARDOWN |
| 116 | if (isMaster) { |
| 117 | auto rv = getNetd()->setMetricsReportingLevel(oldMetricsLevel); |
| 118 | if (!rv.isOk()) { |
| 119 | state.SkipWithError(StringPrintf("Failed restoring metrics reporting level: %s", |
| 120 | rv.toString8().string()).c_str()); |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | // DNS calls without any metrics logged or sent. |
| 128 | BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_nothing)(benchmark::State& state) { |
| 129 | benchmark_at_reporting_level(state, 0); |
| 130 | } |
| 131 | BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_nothing) |
| 132 | ->ThreadRange(MIN_THREADS, MAX_THREADS) |
| 133 | ->UseRealTime(); |
| 134 | |
| 135 | // DNS calls with metrics only (netId, latency, return code) sent to the system server. |
| 136 | BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_metrics)(benchmark::State& state) { |
| 137 | benchmark_at_reporting_level(state, 1); |
| 138 | } |
| 139 | BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_metrics) |
| 140 | ->ThreadRange(MIN_THREADS, MAX_THREADS) |
| 141 | ->UseRealTime(); |
| 142 | |
| 143 | // DNS calls with all information logged and sent to the system server. |
| 144 | BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo_log_everything)(benchmark::State& state) { |
| 145 | benchmark_at_reporting_level(state, 2); |
| 146 | } |
| 147 | BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo_log_everything) |
| 148 | ->ThreadRange(MIN_THREADS, MAX_THREADS) |
| 149 | ->UseRealTime(); |