proxygen
IOBufBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-present 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);
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);
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 
105 int main(int argc, char** argv) {
106  gflags::ParseCommandLineFlags(&argc, &argv, true);
108  return 0;
109 }
BENCHMARK(cloneOneBenchmark, iters)
ByteRange coalesce()
Definition: IOBuf.h:1095
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void cloneOneInto(IOBuf &other) const
Definition: IOBuf.h:1221
std::unique_ptr< IOBuf > clone() const
Definition: IOBuf.cpp:527
void runBenchmarks()
Definition: Benchmark.cpp:456
void cloneInto(IOBuf &other) const
Definition: IOBuf.h:1213
std::size_t capacity() const
Definition: IOBuf.h:593
constexpr std::decay< T >::type copy(T &&value) noexcept(noexcept(typename std::decay< T >::type(std::forward< T >(value))))
Definition: Utility.h:72
char ** argv
BENCHMARK_RELATIVE(cloneCoalescedBenchmark, iters)
IOBuf cloneAsValue() const
Definition: IOBuf.cpp:546
std::unique_ptr< IOBuf > cloneOne() const
Definition: IOBuf.cpp:531
int main(int argc, char **argv)
IOBuf cloneCoalescedAsValue() const
Definition: IOBuf.cpp:570
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258