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