proxygen
FibersBenchmark.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 
19 
20 #include <queue>
21 
22 #include <folly/Benchmark.h>
24 #include <folly/init/Init.h>
26 
27 using namespace folly::fibers;
28 
29 static size_t sNumAwaits;
30 
31 void runBenchmark(size_t numAwaits, size_t toSend) {
32  sNumAwaits = numAwaits;
33 
34  FiberManager fiberManager(std::make_unique<SimpleLoopController>());
35  auto& loopController =
36  dynamic_cast<SimpleLoopController&>(fiberManager.loopController());
37 
38  std::queue<Promise<int>> pendingRequests;
39  static const size_t maxOutstanding = 5;
40 
41  auto loop = [&fiberManager, &loopController, &pendingRequests, &toSend]() {
42  if (pendingRequests.size() == maxOutstanding || toSend == 0) {
43  if (pendingRequests.empty()) {
44  return;
45  }
46  pendingRequests.front().setValue(0);
47  pendingRequests.pop();
48  } else {
49  fiberManager.addTask([&pendingRequests]() {
50  for (size_t i = 0; i < sNumAwaits; ++i) {
51  auto result = await([&pendingRequests](Promise<int> promise) {
52  pendingRequests.push(std::move(promise));
53  });
54  DCHECK_EQ(result, 0);
55  }
56  });
57 
58  if (--toSend == 0) {
59  loopController.stop();
60  }
61  }
62  };
63 
64  loopController.loop(std::move(loop));
65 }
66 
67 BENCHMARK(FiberManagerBasicOneAwait, iters) {
68  runBenchmark(1, iters);
69 }
70 
71 BENCHMARK(FiberManagerBasicFiveAwaits, iters) {
72  runBenchmark(5, iters);
73 }
74 
75 BENCHMARK(FiberManagerCreateDestroy, iters) {
76  for (size_t i = 0; i < iters; ++i) {
77  folly::EventBase evb;
78  auto& fm = folly::fibers::getFiberManager(evb);
79  fm.addTask([]() {});
80  evb.loop();
81  }
82 }
83 
84 BENCHMARK(FiberManagerAllocateDeallocatePattern, iters) {
85  static const size_t kNumAllocations = 10000;
86 
88  opts.maxFibersPoolSize = 0;
89 
90  FiberManager fiberManager(std::make_unique<SimpleLoopController>(), opts);
91 
92  for (size_t iter = 0; iter < iters; ++iter) {
93  DCHECK_EQ(0, fiberManager.fibersPoolSize());
94 
95  size_t fibersRun = 0;
96 
97  for (size_t i = 0; i < kNumAllocations; ++i) {
98  fiberManager.addTask([&fibersRun] { ++fibersRun; });
99  fiberManager.loopUntilNoReady();
100  }
101 
102  DCHECK_EQ(10000, fibersRun);
103  DCHECK_EQ(0, fiberManager.fibersPoolSize());
104  }
105 }
106 
107 BENCHMARK(FiberManagerAllocateLargeChunk, iters) {
108  static const size_t kNumAllocations = 10000;
109 
111  opts.maxFibersPoolSize = 0;
112 
113  FiberManager fiberManager(std::make_unique<SimpleLoopController>(), opts);
114 
115  for (size_t iter = 0; iter < iters; ++iter) {
116  DCHECK_EQ(0, fiberManager.fibersPoolSize());
117 
118  size_t fibersRun = 0;
119 
120  for (size_t i = 0; i < kNumAllocations; ++i) {
121  fiberManager.addTask([&fibersRun] { ++fibersRun; });
122  }
123 
124  fiberManager.loopUntilNoReady();
125 
126  DCHECK_EQ(10000, fibersRun);
127  DCHECK_EQ(0, fiberManager.fibersPoolSize());
128  }
129 }
130 
131 int main(int argc, char** argv) {
132  folly::init(&argc, &argv, true);
133 
135  return 0;
136 }
int main(int argc, char **argv)
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
void runBenchmarks()
Definition: Benchmark.cpp:456
void runBenchmark(size_t numAwaits, size_t toSend)
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
Single-threaded task execution engine.
char ** argv
static size_t sNumAwaits
LoopController & loopController()
void loop(int iters)
BENCHMARK(fbFollyGlobalBenchmarkBaseline)
Definition: Benchmark.cpp:84
size_t fibersPoolSize() const
FirstArgOf< F >::type::value_type await(F &&func)
FiberManager & getFiberManager(EventBase &evb, const FiberManager::Options &opts)