connect_benchmark.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 "connect_benchmark"
  17. /*
  18. * See README.md for general notes.
  19. *
  20. * This set of benchmarks measures the throughput of connect() calls on a single thread for IPv4 and
  21. * IPv6.
  22. *
  23. * Realtime timed tests
  24. * ====================
  25. *
  26. * The tests named *_high_load record the following useful information:
  27. *
  28. * - real_time: the mean roundtrip time for one connect() call under load
  29. *
  30. * - iterations: the number of times the test was run within the timelimit --- approximately
  31. * MinTime / real_time
  32. *
  33. * Manually timed tests
  34. * ====================
  35. *
  36. * All other sets of tests apart from *_high_load run with manual timing. The purpose of these is to
  37. * measure 90th-percentile latency for connect() calls compared to mean latency.
  38. *
  39. * (TODO: ideally this should be against median latency, but google-benchmark only supports one
  40. * custom 'label' output for graphing. Stddev isn't appropriate because the latency
  41. * distribution is usually spiky, not in a nice neat normal-like distribution.)
  42. *
  43. * The manually timed tests record the following useful information:
  44. *
  45. * - real_time: the average time taken to complete a test run. Unlike the real_time used in high
  46. * load tests, this is calculated from before-and-after values of the realtime clock
  47. * over many iterations so may be less accurate than the under-load times.
  48. *
  49. * - iterations: the number of times the test was run within the timelimit --- approximately
  50. * MinTime / real_time, although as explained, may not be as meaningful because of
  51. * overhead from timing.
  52. *
  53. * - label: a manually-recorded time giving the 90th-percentile value of real_time over all
  54. * individual runs. Should be compared to real_time.
  55. *
  56. */
  57. #include <arpa/inet.h>
  58. #include <cutils/sockets.h>
  59. #include <errno.h>
  60. #include <netinet/in.h>
  61. #include <time.h>
  62. #include <map>
  63. #include <functional>
  64. #include <thread>
  65. #include <android-base/stringprintf.h>
  66. #include <benchmark/benchmark.h>
  67. #include <log/log.h>
  68. #include <netdutils/Stopwatch.h>
  69. #include <utils/StrongPointer.h>
  70. #include "FwmarkClient.h"
  71. #include "SockDiag.h"
  72. using android::base::StringPrintf;
  73. using android::netdutils::Stopwatch;
  74. static int bindAndListen(int s) {
  75. sockaddr_in6 sin6 = { .sin6_family = AF_INET6 };
  76. if (bind(s, (sockaddr*) &sin6, sizeof(sin6)) == 0) {
  77. if (listen(s, 1)) {
  78. return -1;
  79. }
  80. sockaddr_in sin = {};
  81. socklen_t len = sizeof(sin);
  82. if (getsockname(s, (sockaddr*) &sin, &len)) {
  83. return -1;
  84. }
  85. return ntohs(sin.sin_port);
  86. } else {
  87. return -1;
  88. }
  89. }
  90. static void ipv4_loopback(benchmark::State& state, const bool waitBetweenRuns) {
  91. const int listensocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
  92. const int port = bindAndListen(listensocket);
  93. if (port == -1) {
  94. state.SkipWithError("Unable to bind server socket");
  95. return;
  96. }
  97. // ALOGW("Listening on port = %d", port);
  98. std::vector<uint64_t> latencies(state.max_iterations);
  99. uint64_t iterations = 0;
  100. while (state.KeepRunning()) {
  101. int sock = socket(AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  102. if (sock < 0) {
  103. state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str());
  104. break;
  105. }
  106. const Stopwatch stopwatch;
  107. sockaddr_in server = { .sin_family = AF_INET, .sin_port = htons(port) };
  108. if (connect(sock, (sockaddr*) &server, sizeof(server))) {
  109. state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str());
  110. close(sock);
  111. break;
  112. }
  113. if (waitBetweenRuns) {
  114. latencies[iterations] = stopwatch.timeTaken() * 1e6L;
  115. state.SetIterationTime(latencies[iterations] / 1e9L);
  116. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  117. ++iterations;
  118. }
  119. sockaddr_in6 client;
  120. socklen_t clientlen = sizeof(client);
  121. int accepted = accept4(listensocket, (sockaddr*) &client, &clientlen, SOCK_CLOEXEC);
  122. if (accepted < 0) {
  123. state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str());
  124. close(sock);
  125. break;
  126. }
  127. close(accepted);
  128. close(sock);
  129. }
  130. close(listensocket);
  131. // ALOGI("Finished test on port = %d", port);
  132. if (iterations > 0) {
  133. latencies.resize(iterations);
  134. sort(latencies.begin(), latencies.end());
  135. state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10]));
  136. }
  137. }
  138. static void ipv6_loopback(benchmark::State& state, const bool waitBetweenRuns) {
  139. const int listensocket = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
  140. const int port = bindAndListen(listensocket);
  141. if (port == -1) {
  142. state.SkipWithError("Unable to bind server socket");
  143. return;
  144. }
  145. // ALOGW("Listening on port = %d", port);
  146. std::vector<uint64_t> latencies(state.max_iterations);
  147. uint64_t iterations = 0;
  148. while (state.KeepRunning()) {
  149. int sock = socket(AF_INET6, SOCK_STREAM | SOCK_CLOEXEC, 0);
  150. if (sock < 0) {
  151. state.SkipWithError(StringPrintf("socket() failed with errno=%d", errno).c_str());
  152. break;
  153. }
  154. const Stopwatch stopwatch;
  155. sockaddr_in6 server = { .sin6_family = AF_INET6, .sin6_port = htons(port) };
  156. if (connect(sock, (sockaddr*) &server, sizeof(server))) {
  157. state.SkipWithError(StringPrintf("connect() failed with errno=%d", errno).c_str());
  158. close(sock);
  159. break;
  160. }
  161. if (waitBetweenRuns) {
  162. latencies[iterations] = stopwatch.timeTaken() * 1e6L;
  163. state.SetIterationTime(latencies[iterations] / 1e9L);
  164. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  165. ++iterations;
  166. }
  167. sockaddr_in6 client;
  168. socklen_t clientlen = sizeof(client);
  169. int accepted = accept4(listensocket, (sockaddr*) &client, &clientlen, SOCK_CLOEXEC);
  170. if (accepted < 0) {
  171. state.SkipWithError(StringPrintf("accept() failed with errno=%d", errno).c_str());
  172. close(sock);
  173. break;
  174. }
  175. close(accepted);
  176. close(sock);
  177. }
  178. close(listensocket);
  179. // ALOGI("Finished test on port = %d", port);
  180. if (iterations > 0) {
  181. latencies.resize(iterations);
  182. sort(latencies.begin(), latencies.end());
  183. state.SetLabel(StringPrintf("%lld", (long long) latencies[iterations * 9 / 10]));
  184. }
  185. }
  186. static void run(decltype(ipv4_loopback) benchmarkFunction, ::benchmark::State& state,
  187. const bool waitBetweenRuns) {
  188. benchmarkFunction(state, waitBetweenRuns);
  189. }
  190. constexpr int MIN_THREADS = 1;
  191. constexpr int MAX_THREADS = 1;
  192. constexpr double MIN_TIME = 0.5 /* seconds */;
  193. // IPv4 benchmarks under no load
  194. static void ipv4_no_load(::benchmark::State& state) {
  195. run(ipv4_loopback, state, true);
  196. }
  197. BENCHMARK(ipv4_no_load)->MinTime(MIN_TIME)->UseManualTime();
  198. // IPv4 benchmarks under high load
  199. static void ipv4_high_load(::benchmark::State& state) {
  200. run(ipv4_loopback, state, false);
  201. }
  202. BENCHMARK(ipv4_high_load)->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();
  203. // IPv6 raw connect() without using fwmark
  204. static void ipv6_no_load(::benchmark::State& state) {
  205. run(ipv6_loopback, state, true);
  206. }
  207. BENCHMARK(ipv6_no_load)->MinTime(MIN_TIME)->UseManualTime();
  208. // IPv6 benchmarks under high load
  209. static void ipv6_high_load(::benchmark::State& state) {
  210. run(ipv6_loopback, state, false);
  211. }
  212. BENCHMARK(ipv6_high_load)->ThreadRange(MIN_THREADS, MAX_THREADS)->MinTime(MIN_TIME)->UseRealTime();