Fix SimpleBarrier
[folly.git] / folly / test / IPAddressBenchmark.cpp
1 /*
2  * Copyright 2016 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/IPAddress.h>
18
19 #include <glog/logging.h>
20
21 #include <folly/Benchmark.h>
22
23 using namespace folly;
24 using std::string;
25
26 BENCHMARK(ipv4_to_string_inet_ntop, iters) {
27   folly::IPAddressV4 ipv4Addr("127.0.0.1");
28   in_addr ip = ipv4Addr.toAddr();
29   char outputString[INET_ADDRSTRLEN] = {0};
30
31   while (iters--) {
32     const char* val = inet_ntop(
33       AF_INET,
34       &ip,
35       outputString,
36       sizeof(outputString));
37   }
38 }
39
40 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
41   IPAddressV4 ip("127.0.0.1");
42   while (iters--) {
43     string outputString = ip.toFullyQualified();
44   }
45 }
46
47 BENCHMARK_DRAW_LINE()
48
49 BENCHMARK(ipv6_to_string_inet_ntop, iters) {
50   IPAddressV6 ipv6Addr("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
51   in6_addr ip = ipv6Addr.toAddr();
52   char outputString[INET6_ADDRSTRLEN] = {0};
53   bool checkResult = (iters == 1);
54
55   while (iters--) {
56     const char* val = inet_ntop(
57       AF_INET6,
58       &ip,
59       outputString,
60       sizeof(outputString));
61   }
62 }
63
64 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
65   IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
66   string outputString;
67   while (iters--) {
68     outputString = ip.toFullyQualified();
69   }
70 }
71
72 // Benchmark results on Intel Xeon CPU E5-2660 @ 2.20GHz
73 // ============================================================================
74 // folly/test/IPAddressBenchmark.cpp               relative  time/iter  iters/s
75 // ============================================================================
76 // ipv4_to_string_inet_ntop                                   237.87ns    4.20M
77 // ipv4_to_fully_qualified                          362.31%    65.65ns   15.23M
78 // ----------------------------------------------------------------------------
79 // ipv6_to_string_inet_ntop                                   768.60ns    1.30M
80 // ipv6_to_fully_qualified                          821.81%    93.53ns   10.69M
81 // ============================================================================
82
83 int main(int argc, char *argv[]) {
84   gflags::ParseCommandLineFlags(&argc, &argv, true);
85   runBenchmarks();
86   return 0;
87 }