2 * Copyright 2016 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/IPAddress.h>
19 #include <glog/logging.h>
21 #include <folly/Benchmark.h>
23 using namespace folly;
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};
32 const char* val = inet_ntop(
36 sizeof(outputString));
40 BENCHMARK_RELATIVE(ipv4_to_fully_qualified, iters) {
41 IPAddressV4 ip("127.0.0.1");
43 string outputString = ip.toFullyQualified();
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);
56 const char* val = inet_ntop(
60 sizeof(outputString));
64 BENCHMARK_RELATIVE(ipv6_to_fully_qualified, iters) {
65 IPAddressV6 ip("F1E0:0ACE:FB94:7ADF:22E8:6DE6:9672:3725");
68 outputString = ip.toFullyQualified();
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 // ============================================================================
83 int main(int argc, char *argv[]) {
84 gflags::ParseCommandLineFlags(&argc, &argv, true);