Add toFullyQualifiedAppend() methods
[folly.git] / folly / test / IPAddressBenchmark.cpp
1 /*
2  * Copyright 2017 Facebook, Inc.
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 #include <folly/Conv.h>
18 #include <folly/IPAddress.h>
19
20 #include <glog/logging.h>
21
22 #include <folly/Benchmark.h>
23
24 using namespace folly;
25 using std::string;
26
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};
31
32   while (iters--) {
33     const char* val = inet_ntop(
34       AF_INET,
35       &ip,
36       outputString,
37       sizeof(outputString));
38   }
39 }
40
41 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
42   IPAddressV4 ip("127.0.0.1");
43   while (iters--) {
44     string outputString = ip.toFullyQualified();
45   }
46 }
47
48 BENCHMARK_DRAW_LINE()
49
50 BENCHMARK(ipv4_to_fully_qualified_port, iters) {
51   IPAddressV4 ip("255.255.255.255");
52   while (iters--) {
53     string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535);
54     folly::doNotOptimizeAway(outputString);
55     folly::doNotOptimizeAway(outputString.data());
56   }
57 }
58
59 BENCHMARK_RELATIVE(ipv4_append_to_fully_qualified_port, iters) {
60   IPAddressV4 ip("255.255.255.255");
61   while (iters--) {
62     string outputString;
63     outputString.reserve(IPAddressV4::kMaxToFullyQualifiedSize + 1 + 5);
64     ip.toFullyQualifiedAppend(outputString);
65     outputString += ':';
66     folly::toAppend(65535, &outputString);
67     folly::doNotOptimizeAway(outputString);
68     folly::doNotOptimizeAway(outputString.data());
69   }
70 }
71
72 BENCHMARK_DRAW_LINE()
73
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);
79
80   while (iters--) {
81     const char* val = inet_ntop(
82       AF_INET6,
83       &ip,
84       outputString,
85       sizeof(outputString));
86   }
87 }
88
89 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
90   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
91   string outputString;
92   while (iters--) {
93     outputString = ip.toFullyQualified();
94   }
95 }
96
97 BENCHMARK_DRAW_LINE()
98
99 BENCHMARK(ipv6_to_fully_qualified_port, iters) {
100   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
101   while (iters--) {
102     string outputString = to<std::string>(ip.toFullyQualified(), ':', 65535);
103     folly::doNotOptimizeAway(outputString);
104     folly::doNotOptimizeAway(outputString.data());
105   }
106 }
107
108 BENCHMARK_RELATIVE(ipv6_append_to_fully_qualified_port, iters) {
109   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
110   while (iters--) {
111     string outputString;
112     outputString.reserve(folly::IPAddressV6::kToFullyQualifiedSize + 1 + 5);
113     ip.toFullyQualifiedAppend(outputString);
114     outputString += ':';
115     folly::toAppend(65535, &outputString);
116     folly::doNotOptimizeAway(outputString);
117     folly::doNotOptimizeAway(outputString.data());
118   }
119 }
120
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 // ============================================================================
137
138 int main(int argc, char *argv[]) {
139   gflags::ParseCommandLineFlags(&argc, &argv, true);
140   runBenchmarks();
141   return 0;
142 }