2 * Copyright 2017 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/Benchmark.h>
18 #include <folly/io/IOBuf.h>
22 BENCHMARK(cloneOneBenchmark, iters) {
23 IOBuf buf(IOBuf::CREATE, 10);
25 auto copy = buf.cloneOne();
26 folly::doNotOptimizeAway(copy->capacity());
30 BENCHMARK(cloneOneIntoBenchmark, iters) {
31 IOBuf buf(IOBuf::CREATE, 10);
34 buf.cloneOneInto(copy);
35 folly::doNotOptimizeAway(copy.capacity());
39 BENCHMARK(cloneBenchmark, iters) {
40 IOBuf buf(IOBuf::CREATE, 10);
42 auto copy = buf.clone();
43 folly::doNotOptimizeAway(copy->capacity());
47 BENCHMARK(cloneIntoBenchmark, iters) {
48 IOBuf buf(IOBuf::CREATE, 10);
52 folly::doNotOptimizeAway(copy.capacity());
56 BENCHMARK(moveBenchmark, iters) {
57 IOBuf buf(IOBuf::CREATE, 10);
59 auto tmp = std::move(buf);
60 folly::doNotOptimizeAway(tmp.capacity());
65 BENCHMARK(copyBenchmark, iters) {
66 IOBuf buf(IOBuf::CREATE, 10);
69 folly::doNotOptimizeAway(copy.capacity());
73 BENCHMARK(cloneCoalescedBaseline, iters) {
74 std::unique_ptr<IOBuf> buf = IOBuf::createChain(100, 10);
76 auto clone = buf->cloneAsValue();
78 folly::doNotOptimizeAway(clone.capacity());
82 BENCHMARK_RELATIVE(cloneCoalescedBenchmark, iters) {
83 std::unique_ptr<IOBuf> buf = IOBuf::createChain(100, 10);
85 auto copy = buf->cloneCoalescedAsValue();
86 folly::doNotOptimizeAway(copy.capacity());
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 * ============================================================================
105 int main(int argc, char** argv) {
106 gflags::ParseCommandLineFlags(&argc, &argv, true);
107 folly::runBenchmarks();