dns_benchmark.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #define LOG_TAG "dns_benchmark"
  17. /*
  18. * See README.md for general notes.
  19. *
  20. * This set of benchmarks measures the throughput of getaddrinfo() on between 1 and 32 threads for
  21. * the purpose of keeping track of the maximum load that netd can reasonably handle.
  22. *
  23. * Useful measurements
  24. * ===================
  25. *
  26. * - real_time: the average time taken to make a single getaddrinfo lookup on a local DNS resolver
  27. * run by DnsFixture. This will usually be higher on multithreaded tests as threads
  28. * block on DNS lookups and Binder connections.
  29. *
  30. * - iterations: total number of runs finished within the time limit. Higher is better. This is
  31. * roughly proportional to MinTime * nThreads / real_time.
  32. *
  33. */
  34. #include <netdb.h>
  35. #include <netinet/in.h>
  36. #include <sys/types.h>
  37. #include <sys/socket.h>
  38. #include <android-base/stringprintf.h>
  39. #include <benchmark/benchmark.h>
  40. #include "NetdClient.h"
  41. #include "dns_responder_client.h"
  42. #include "netd_resolv/params.h" // MAXNS
  43. using android::base::StringPrintf;
  44. constexpr int MIN_THREADS = 1;
  45. constexpr int MAX_THREADS = 32;
  46. class DnsFixture : public ::benchmark::Fixture {
  47. protected:
  48. static constexpr unsigned num_hosts = 1000;
  49. DnsResponderClient dns;
  50. std::vector<DnsResponderClient::Mapping> mappings;
  51. std::vector<std::unique_ptr<test::DNSResponder>> mDns;
  52. public:
  53. void SetUp(const ::benchmark::State& state) override {
  54. if (state.thread_index == 0) {
  55. dns.SetUp();
  56. std::vector<std::string> domains = { "example.com" };
  57. std::vector<std::string> servers;
  58. dns.SetupMappings(num_hosts, domains, &mappings);
  59. dns.SetupDNSServers(MAXNS, mappings, &mDns, &servers);
  60. const std::vector<int> mDefaultParams_Binder = {300, 25, 8, 8, 1000};
  61. dns.SetResolversForNetwork(servers, domains, mDefaultParams_Binder);
  62. }
  63. }
  64. void TearDown(const ::benchmark::State& state) override {
  65. if (state.thread_index == 0) {
  66. dns.TearDown();
  67. }
  68. }
  69. std::vector<DnsResponderClient::Mapping> const& getMappings() const {
  70. return mappings;
  71. }
  72. void benchmark(benchmark::State& state) {
  73. while (state.KeepRunning()) {
  74. const uint32_t ofs = arc4random_uniform(getMappings().size());
  75. const auto& mapping = getMappings()[ofs];
  76. addrinfo* result = nullptr;
  77. if (getaddrinfo(mapping.host.c_str(), nullptr, nullptr, &result)) {
  78. state.SkipWithError(StringPrintf("getaddrinfo failed with errno=%d",
  79. errno).c_str());
  80. break;
  81. }
  82. if (result) {
  83. freeaddrinfo(result);
  84. result = nullptr;
  85. }
  86. }
  87. }
  88. };
  89. BENCHMARK_DEFINE_F(DnsFixture, getaddrinfo)(benchmark::State& state) {
  90. benchmark(state);
  91. }
  92. BENCHMARK_REGISTER_F(DnsFixture, getaddrinfo)
  93. ->ThreadRange(MIN_THREADS, MAX_THREADS)
  94. ->UseRealTime();