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/Format.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};
34 inet_ntop(AF_INET, &ip, outputString, sizeof(outputString));
38 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
39 IPAddressV4 ip("127.0.0.1");
41 string outputString = ip.toFullyQualified();
47 BENCHMARK(ipv4_to_fully_qualified_port, iters) {
48 IPAddressV4 ip("255.255.255.255");
50 string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
51 folly::doNotOptimizeAway(outputString);
52 folly::doNotOptimizeAway(outputString.data());
56 BENCHMARK_RELATIVE(ipv4_append_to_fully_qualified_port, iters) {
57 IPAddressV4 ip("255.255.255.255");
60 outputString.reserve(IPAddressV4::kMaxToFullyQualifiedSize + 1 + 5);
61 ip.toFullyQualifiedAppend(outputString);
63 folly::toAppend(65535, &outputString);
64 folly::doNotOptimizeAway(outputString);
65 folly::doNotOptimizeAway(outputString.data());
71 BENCHMARK(ipv6_to_string_inet_ntop, iters) {
72 IPAddressV6 ipv6Addr("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
73 in6_addr ip = ipv6Addr.toAddr();
74 char outputString[INET6_ADDRSTRLEN] = {0};
75 bool checkResult = (iters == 1);
79 inet_ntop(AF_INET6, &ip, outputString, sizeof(outputString));
83 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
84 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
87 outputString = ip.toFullyQualified();
93 BENCHMARK(ipv6_to_fully_qualified_port, iters) {
94 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
96 string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
97 folly::doNotOptimizeAway(outputString);
98 folly::doNotOptimizeAway(outputString.data());
102 BENCHMARK_RELATIVE(ipv6_append_to_fully_qualified_port, iters) {
103 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
106 outputString.reserve(folly::IPAddressV6::kToFullyQualifiedSize + 1 + 5);
107 ip.toFullyQualifiedAppend(outputString);
109 folly::toAppend(65535, &outputString);
110 folly::doNotOptimizeAway(outputString);
111 folly::doNotOptimizeAway(outputString.data());
115 // Benchmark results on Intel Xeon CPU E5-2660 @ 2.20GHz
116 // ============================================================================
117 // folly/test/IPAddressBenchmark.cpp relative time/iter iters/s
118 // ============================================================================
119 // ipv4_to_string_inet_ntop 227.13ns 4.40M
120 // ipv4_to_fully_qualified 1418.95% 16.01ns 62.47M
121 // ----------------------------------------------------------------------------
122 // ipv4_to_fully_qualified_port 77.51ns 12.90M
123 // ipv4_append_to_fully_qualified_port 133.72% 57.96ns 17.25M
124 // ----------------------------------------------------------------------------
125 // ipv6_to_string_inet_ntop 750.53ns 1.33M
126 // ipv6_to_fully_qualified 608.68% 123.30ns 8.11M
127 // ----------------------------------------------------------------------------
128 // ipv6_to_fully_qualified_port 150.76ns 6.63M
129 // ipv6_append_to_fully_qualified_port 178.73% 84.35ns 11.86M
130 // ============================================================================
132 int main(int argc, char* argv[]) {
133 gflags::ParseCommandLineFlags(&argc, &argv, true);