Add IOBuf::cloneCoalesced()
[folly.git] / folly / io / test / IOBufBenchmark.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/Benchmark.h>
18 #include <folly/io/IOBuf.h>
19
20 using folly::IOBuf;
21
22 BENCHMARK(cloneOneBenchmark, iters) {
23   IOBuf buf(IOBuf::CREATE, 10);
24   while (iters--) {
25     auto copy = buf.cloneOne();
26     folly::doNotOptimizeAway(copy->capacity());
27   }
28 }
29
30 BENCHMARK(cloneOneIntoBenchmark, iters) {
31   IOBuf buf(IOBuf::CREATE, 10);
32   IOBuf copy;
33   while (iters--) {
34     buf.cloneOneInto(copy);
35     folly::doNotOptimizeAway(copy.capacity());
36   }
37 }
38
39 BENCHMARK(cloneBenchmark, iters) {
40   IOBuf buf(IOBuf::CREATE, 10);
41   while (iters--) {
42     auto copy = buf.clone();
43     folly::doNotOptimizeAway(copy->capacity());
44   }
45 }
46
47 BENCHMARK(cloneIntoBenchmark, iters) {
48   IOBuf buf(IOBuf::CREATE, 10);
49   IOBuf copy;
50   while (iters--) {
51     buf.cloneInto(copy);
52     folly::doNotOptimizeAway(copy.capacity());
53   }
54 }
55
56 BENCHMARK(moveBenchmark, iters) {
57   IOBuf buf(IOBuf::CREATE, 10);
58   while (iters--) {
59     auto tmp = std::move(buf);
60     folly::doNotOptimizeAway(tmp.capacity());
61     buf = std::move(tmp);
62   }
63 }
64
65 BENCHMARK(copyBenchmark, iters) {
66   IOBuf buf(IOBuf::CREATE, 10);
67   while (iters--) {
68     auto copy = buf;
69     folly::doNotOptimizeAway(copy.capacity());
70   }
71 }
72
73 BENCHMARK(cloneCoalescedBaseline, iters) {
74   std::unique_ptr<IOBuf> buf = IOBuf::createChain(100, 10);
75   while (iters--) {
76     auto clone = buf->cloneAsValue();
77     clone.coalesce();
78     folly::doNotOptimizeAway(clone.capacity());
79   }
80 }
81
82 BENCHMARK_RELATIVE(cloneCoalescedBenchmark, iters) {
83   std::unique_ptr<IOBuf> buf = IOBuf::createChain(100, 10);
84   while (iters--) {
85     auto copy = buf->cloneCoalescedAsValue();
86     folly::doNotOptimizeAway(copy.capacity());
87   }
88 }
89
90 /**
91  * ============================================================================
92  * folly/io/test/IOBufBenchmark.cpp                relative  time/iter  iters/s
93  * ============================================================================
94  * cloneOneBenchmark                                           49.03ns   20.39M
95  * cloneOneIntoBenchmark                                       26.36ns   37.93M
96  * cloneBenchmark                                              49.43ns   20.23M
97  * cloneIntoBenchmark                                          30.03ns   33.30M
98  * moveBenchmark                                               15.35ns   65.14M
99  * copyBenchmark                                               33.63ns   29.73M
100  * cloneCoalescedBaseline                                     344.33ns    2.90M
101  * cloneCoalescedBenchmark                          605.62%    56.86ns   17.59M
102  * ============================================================================
103  */
104
105 int main(int argc, char** argv) {
106   gflags::ParseCommandLineFlags(&argc, &argv, true);
107   folly::runBenchmarks();
108   return 0;
109 }