proxygen
NetworkBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2013-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 <vector>
18 
19 #include <folly/Benchmark.h>
20 #include <folly/io/Cursor.h>
21 #include <folly/io/IOBuf.h>
23 
24 using folly::IOBuf;
25 using std::unique_ptr;
26 using namespace folly::io;
27 using namespace std;
28 
29 size_t buf_size = 0;
30 size_t num_bufs = 0;
31 
32 BENCHMARK(reserveBenchmark, iters) {
33  while (iters--) {
34  unique_ptr<IOBuf> iobuf1(IOBuf::create(buf_size));
35  iobuf1->append(buf_size);
36  for (size_t bufs = num_bufs; bufs > 1; bufs--) {
37  iobuf1->reserve(0, buf_size);
38  iobuf1->append(buf_size);
39  }
40  }
41 }
42 
43 BENCHMARK(chainBenchmark, iters) {
44  while (iters--) {
45  unique_ptr<IOBuf> iobuf1(IOBuf::create(buf_size));
46  iobuf1->append(buf_size);
47  for (size_t bufs = num_bufs; bufs > 1; bufs--) {
48  unique_ptr<IOBuf> iobufNext(IOBuf::create(buf_size));
49  iobuf1->prependChain(std::move(iobufNext));
50  }
51  }
52 }
53 
54 vector<unique_ptr<IOBuf>> bufPool;
55 inline unique_ptr<IOBuf> poolGetIOBuf() {
56  if (bufPool.size() > 0) {
57  unique_ptr<IOBuf> ret = std::move(bufPool.back());
58  bufPool.pop_back();
59  return ret;
60  } else {
61  unique_ptr<IOBuf> iobuf(IOBuf::create(buf_size));
62  iobuf->append(buf_size);
63  return iobuf;
64  }
65 }
66 
67 inline void poolPutIOBuf(unique_ptr<IOBuf>&& buf) {
68  unique_ptr<IOBuf> head = std::move(buf);
69  while (head) {
70  unique_ptr<IOBuf> next = head->pop();
71  bufPool.push_back(std::move(head));
72  head = std::move(next);
73  }
74 }
75 
76 BENCHMARK(poolBenchmark, iters) {
77  while (iters--) {
78  unique_ptr<IOBuf> head = poolGetIOBuf();
79  for (size_t bufs = num_bufs; bufs > 1; bufs--) {
80  unique_ptr<IOBuf> iobufNext = poolGetIOBuf();
81  head->prependChain(std::move(iobufNext));
82  }
83  // cleanup
84  poolPutIOBuf(std::move(head));
85  }
86 }
87 
88 void setNumbers(size_t size, size_t num) {
89  buf_size = size;
90  num_bufs = num;
91  bufPool.clear();
92 
93  printf("\nBuffer size: %zu, number of buffers: %zu\n\n", size, num);
94 }
95 
96 /*
97 ------------------------------------------------------------------------------
98 reserveBenchmark 100000 9.186 ms 91.86 ns 10.38 M
99 chainBenchmark 100000 59.44 ms 594.4 ns 1.604 M
100 poolBenchmark 100000 15.87 ms 158.7 ns 6.01 M
101 
102 Buffer size: 100, number of buffers: 10
103 
104 Benchmark Iters Total t t/iter iter/sec
105 ------------------------------------------------------------------------------
106 reserveBenchmark 100000 62 ms 620 ns 1.538 M
107 chainBenchmark 100000 59.48 ms 594.8 ns 1.603 M
108 poolBenchmark 100000 16.07 ms 160.7 ns 5.933 M
109 
110 Buffer size: 2048, number of buffers: 10
111 
112 Benchmark Iters Total t t/iter iter/sec
113 ------------------------------------------------------------------------------
114 reserveBenchmark 100000 148.4 ms 1.484 us 658.2 k
115 chainBenchmark 100000 140.9 ms 1.409 us 693 k
116 poolBenchmark 100000 16.73 ms 167.3 ns 5.7 M
117 
118 Buffer size: 10000, number of buffers: 10
119 
120 Benchmark Iters Total t t/iter iter/sec
121 ------------------------------------------------------------------------------
122 reserveBenchmark 100000 234 ms 2.34 us 417.3 k
123 chainBenchmark 100000 142.3 ms 1.423 us 686.1 k
124 poolBenchmark 100000 16.78 ms 167.8 ns 5.684 M
125 
126 Buffer size: 100000, number of buffers: 10
127 
128 Benchmark Iters Total t t/iter iter/sec
129 ------------------------------------------------------------------------------
130 reserveBenchmark 100000 186.5 ms 1.865 us 523.5 k
131 chainBenchmark 100000 360.5 ms 3.605 us 270.9 k
132 poolBenchmark 100000 16.52 ms 165.2 ns 5.772 M
133 
134 Buffer size: 1000000, number of buffers: 10
135 
136 Benchmark Iters Total t t/iter iter/sec
137 ------------------------------------------------------------------------------
138 reserveBenchmark 156 2.084 s 13.36 ms 74.84
139 chainBenchmark 30082 2.001 s 66.5 us 14.68 k
140 poolBenchmark 100000 18.18 ms 181.8 ns 5.244 M
141 
142 
143 Buffer size: 10, number of buffers: 20
144 
145 Benchmark Iters Total t t/iter iter/sec
146 ------------------------------------------------------------------------------
147 reserveBenchmark 100000 12.54 ms 125.4 ns 7.603 M
148 chainBenchmark 100000 118.6 ms 1.186 us 823.2 k
149 poolBenchmark 100000 32.2 ms 322 ns 2.962 M
150 */
151 int main(int argc, char** argv) {
152  gflags::ParseCommandLineFlags(&argc, &argv, true);
153 
154  setNumbers(10, 10);
156  setNumbers(100, 10);
158  setNumbers(2048, 10);
160  setNumbers(10000, 10);
162  setNumbers(100000, 10);
164  setNumbers(1000000, 10);
166 
167  setNumbers(10, 20);
169 
170  return 0;
171 }
static std::unique_ptr< IOBuf > create(std::size_t capacity)
Definition: IOBuf.cpp:229
int main(int argc, char **argv)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
void reserve(std::size_t minHeadroom, std::size_t minTailroom)
Definition: IOBuf.h:741
void runBenchmarks()
Definition: Benchmark.cpp:456
unique_ptr< IOBuf > poolGetIOBuf()
char ** argv
constexpr auto size(C const &c) -> decltype(c.size())
Definition: Access.h:45
std::unique_ptr< IOBuf > pop()
Definition: IOBuf.h:859
vector< unique_ptr< IOBuf > > bufPool
void prependChain(std::unique_ptr< IOBuf > &&iobuf)
Definition: IOBuf.cpp:509
void poolPutIOBuf(unique_ptr< IOBuf > &&buf)
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
size_t num_bufs
void setNumbers(size_t size, size_t num)
size_t buf_size
void append(std::size_t amount)
Definition: IOBuf.h:689
def next(obj)
Definition: ast.py:58