2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <folly/Conv.h>
18 #include <folly/IPAddress.h>
20 #include <glog/logging.h>
22 #include <folly/Benchmark.h>
24 using namespace folly;
27 BENCHMARK(ipv4_to_string_inet_ntop, iters) {
28 folly::IPAddressV4 ipv4Addr("127.0.0.1");
29 in_addr ip = ipv4Addr.toAddr();
30 char outputString[INET_ADDRSTRLEN] = {0};
33 const char* val = inet_ntop(
37 sizeof(outputString));
41 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
42 IPAddressV4 ip("127.0.0.1");
44 string outputString = ip.toFullyQualified();
50 BENCHMARK(ipv4_to_fully_qualified_port, iters) {
51 IPAddressV4 ip("255.255.255.255");
53 string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535);
54 folly::doNotOptimizeAway(outputString);
55 folly::doNotOptimizeAway(outputString.data());
59 BENCHMARK_RELATIVE(ipv4_append_to_fully_qualified_port, iters) {
60 IPAddressV4 ip("255.255.255.255");
63 outputString.reserve(IPAddressV4::kMaxToFullyQualifiedSize + 1 + 5);
64 ip.toFullyQualifiedAppend(outputString);
66 folly::toAppend(65535, &outputString);
67 folly::doNotOptimizeAway(outputString);
68 folly::doNotOptimizeAway(outputString.data());
74 BENCHMARK(ipv6_to_string_inet_ntop, iters) {
75 IPAddressV6 ipv6Addr("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
76 in6_addr ip = ipv6Addr.toAddr();
77 char outputString[INET6_ADDRSTRLEN] = {0};
78 bool checkResult = (iters == 1);
81 const char* val = inet_ntop(
85 sizeof(outputString));
89 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
90 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
93 outputString = ip.toFullyQualified();
99 BENCHMARK(ipv6_to_fully_qualified_port, iters) {
100 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
102 string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535);
103 folly::doNotOptimizeAway(outputString);
104 folly::doNotOptimizeAway(outputString.data());
108 BENCHMARK_RELATIVE(ipv6_append_to_fully_qualified_port, iters) {
109 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
112 outputString.reserve(folly::IPAddressV6::kToFullyQualifiedSize + 1 + 5);
113 ip.toFullyQualifiedAppend(outputString);
115 folly::toAppend(65535, &outputString);
116 folly::doNotOptimizeAway(outputString);
117 folly::doNotOptimizeAway(outputString.data());
121 // Benchmark results on Intel Xeon CPU E5-2660 @ 2.20GHz
122 // ============================================================================
123 // folly/test/IPAddressBenchmark.cpp relative time/iter iters/s
124 // ============================================================================
125 // ipv4_to_string_inet_ntop 227.13ns 4.40M
126 // ipv4_to_fully_qualified 1418.95% 16.01ns 62.47M
127 // ----------------------------------------------------------------------------
128 // ipv4_to_fully_qualified_port 77.51ns 12.90M
129 // ipv4_append_to_fully_qualified_port 133.72% 57.96ns 17.25M
130 // ----------------------------------------------------------------------------
131 // ipv6_to_string_inet_ntop 750.53ns 1.33M
132 // ipv6_to_fully_qualified 608.68% 123.30ns 8.11M
133 // ----------------------------------------------------------------------------
134 // ipv6_to_fully_qualified_port 150.76ns 6.63M
135 // ipv6_append_to_fully_qualified_port 178.73% 84.35ns 11.86M
136 // ============================================================================
138 int main(int argc, char *argv[]) {
139 gflags::ParseCommandLineFlags(&argc, &argv, true);