Extract non-portability constexpr math functions to new header
[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/Format.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 =
34         inet_ntop(AF_INET, &ip, outputString, sizeof(outputString));
35   }
36 }
37
38 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
39   IPAddressV4 ip("127.0.0.1");
40   while (iters--) {
41     string outputString = ip.toFullyQualified();
42   }
43 }
44
45 BENCHMARK_DRAW_LINE()
46
47 BENCHMARK(ipv4_to_fully_qualified_port, iters) {
48   IPAddressV4 ip("255.255.255.255");
49   while (iters--) {
50     string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
51     folly::doNotOptimizeAway(outputString);
52     folly::doNotOptimizeAway(outputString.data());
53   }
54 }
55
56 BENCHMARK_RELATIVE(ipv4_append_to_fully_qualified_port, iters) {
57   IPAddressV4 ip("255.255.255.255");
58   while (iters--) {
59     string outputString;
60     outputString.reserve(IPAddressV4::kMaxToFullyQualifiedSize + 1 + 5);
61     ip.toFullyQualifiedAppend(outputString);
62     outputString += ':';
63     folly::toAppend(65535, &outputString);
64     folly::doNotOptimizeAway(outputString);
65     folly::doNotOptimizeAway(outputString.data());
66   }
67 }
68
69 BENCHMARK_DRAW_LINE()
70
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);
76
77   while (iters--) {
78     const char* val =
79         inet_ntop(AF_INET6, &ip, outputString, sizeof(outputString));
80   }
81 }
82
83 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
84   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
85   string outputString;
86   while (iters--) {
87     outputString = ip.toFullyQualified();
88   }
89 }
90
91 BENCHMARK_DRAW_LINE()
92
93 BENCHMARK(ipv6_to_fully_qualified_port, iters) {
94   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
95   while (iters--) {
96     string outputString = folly::sformat("{}:{}", ip.toFullyQualified(), 65535);
97     folly::doNotOptimizeAway(outputString);
98     folly::doNotOptimizeAway(outputString.data());
99   }
100 }
101
102 BENCHMARK_RELATIVE(ipv6_append_to_fully_qualified_port, iters) {
103   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
104   while (iters--) {
105     string outputString;
106     outputString.reserve(folly::IPAddressV6::kToFullyQualifiedSize + 1 + 5);
107     ip.toFullyQualifiedAppend(outputString);
108     outputString += ':';
109     folly::toAppend(65535, &outputString);
110     folly::doNotOptimizeAway(outputString);
111     folly::doNotOptimizeAway(outputString.data());
112   }
113 }
114
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 // ============================================================================
131
132 int main(int argc, char* argv[]) {
133   gflags::ParseCommandLineFlags(&argc, &argv, true);
134   runBenchmarks();
135   return 0;
136 }