465691c0dc71503645ec4e266e9a9c56fc808f79
[folly.git] / folly / io / test / IOBufCursorBenchmark.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/io/IOBuf.h>
18
19 #include <folly/Benchmark.h>
20 #include <folly/Format.h>
21 #include <folly/Range.h>
22 #include <folly/io/Cursor.h>
23
24 DECLARE_bool(benchmark);
25
26 using folly::ByteRange;
27 using folly::format;
28 using folly::IOBuf;
29 using folly::StringPiece;
30 using std::unique_ptr;
31 using namespace folly::io;
32
33 int benchmark_size = 1000;
34 unique_ptr<IOBuf> iobuf_benchmark;
35
36 unique_ptr<IOBuf> iobuf_read_benchmark;
37
38 template <class CursClass>
39 void runBenchmark() {
40   CursClass c(iobuf_benchmark.get());
41
42   for (int i = 0; i < benchmark_size; i++) {
43     c.write((uint8_t)0);
44   }
45 }
46
47 BENCHMARK(rwPrivateCursorBenchmark, iters) {
48   while (iters--) {
49     runBenchmark<RWPrivateCursor>();
50   }
51 }
52
53 BENCHMARK(rwUnshareCursorBenchmark, iters) {
54   while (iters--) {
55     runBenchmark<RWUnshareCursor>();
56   }
57 }
58
59 BENCHMARK(cursorBenchmark, iters) {
60   while (iters--) {
61     Cursor c(iobuf_read_benchmark.get());
62     for (int i = 0; i < benchmark_size; i++) {
63       c.read<uint8_t>();
64     }
65   }
66 }
67
68 BENCHMARK(skipBenchmark, iters) {
69   while (iters--) {
70     Cursor c(iobuf_read_benchmark.get());
71     for (int i = 0; i < benchmark_size; i++) {
72       c.peekBytes();
73       c.skip(1);
74     }
75   }
76 }
77
78 BENCHMARK(cloneBenchmark, iters) {
79   folly::IOBuf out;
80   while (iters--) {
81     Cursor c(iobuf_read_benchmark.get());
82     for (int i = 0; i < benchmark_size; ++i) {
83       c.clone(out, 1);
84     }
85   }
86 }
87
88 // fbmake opt
89 // _bin/folly/experimental/io/test/iobuf_cursor_test -benchmark
90 //
91 // Benchmark                               Iters   Total t    t/iter iter/sec
92 // ---------------------------------------------------------------------------
93 // rwPrivateCursorBenchmark               100000  142.9 ms  1.429 us  683.5 k
94 // rwUnshareCursorBenchmark               100000  309.3 ms  3.093 us  315.7 k
95 // cursorBenchmark                        100000  741.4 ms  7.414 us  131.7 k
96 // skipBenchmark                          100000  738.9 ms  7.389 us  132.2 k
97 //
98 // uname -a:
99 //
100 // Linux dev2159.snc6.facebook.com 2.6.33-7_fbk15_104e4d0 #1 SMP
101 // Tue Oct 19 22:40:30 PDT 2010 x86_64 x86_64 x86_64 GNU/Linux
102 //
103 // 72GB RAM, 2 CPUs (Intel(R) Xeon(R) CPU L5630  @ 2.13GHz)
104 // hyperthreading disabled
105
106 int main(int argc, char** argv) {
107   gflags::ParseCommandLineFlags(&argc, &argv, true);
108   iobuf_benchmark = IOBuf::create(benchmark_size);
109   iobuf_benchmark->append(benchmark_size);
110
111   iobuf_read_benchmark = IOBuf::create(1);
112   for (int i = 0; i < benchmark_size; i++) {
113     unique_ptr<IOBuf> iobuf2(IOBuf::create(1));
114     iobuf2->append(1);
115     iobuf_read_benchmark->prependChain(std::move(iobuf2));
116   }
117
118   folly::runBenchmarks();
119   return 0;
120 }