proxygen
FibersBenchmark.cpp File Reference

Go to the source code of this file.

Functions

void runBenchmark (size_t numAwaits, size_t toSend)
 
 BENCHMARK (FiberManagerBasicOneAwait, iters)
 
 BENCHMARK (FiberManagerBasicFiveAwaits, iters)
 
 BENCHMARK (FiberManagerCreateDestroy, iters)
 
 BENCHMARK (FiberManagerAllocateDeallocatePattern, iters)
 
 BENCHMARK (FiberManagerAllocateLargeChunk, iters)
 
int main (int argc, char **argv)
 

Variables

static size_t sNumAwaits
 

Function Documentation

BENCHMARK ( FiberManagerBasicOneAwait  ,
iters   
)

Definition at line 67 of file FibersBenchmark.cpp.

References runBenchmark().

67  {
68  runBenchmark(1, iters);
69 }
void runBenchmark(size_t numAwaits, size_t toSend)
BENCHMARK ( FiberManagerBasicFiveAwaits  ,
iters   
)

Definition at line 71 of file FibersBenchmark.cpp.

References runBenchmark().

71  {
72  runBenchmark(5, iters);
73 }
void runBenchmark(size_t numAwaits, size_t toSend)
BENCHMARK ( FiberManagerCreateDestroy  ,
iters   
)

Definition at line 75 of file FibersBenchmark.cpp.

References folly::fibers::getFiberManager(), i, and folly::EventBase::loop().

75  {
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 }
FiberManager & getFiberManager(EventBase &evb, const FiberManager::Options &opts)
BENCHMARK ( FiberManagerAllocateDeallocatePattern  ,
iters   
)

Definition at line 84 of file FibersBenchmark.cpp.

References folly::fibers::FiberManager::addTask(), folly::fibers::FiberManager::fibersPoolSize(), i, folly::fibers::FiberManager::loopUntilNoReady(), and folly::fibers::FiberManager::Options::maxFibersPoolSize.

84  {
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 }
Single-threaded task execution engine.
BENCHMARK ( FiberManagerAllocateLargeChunk  ,
iters   
)

Definition at line 107 of file FibersBenchmark.cpp.

References folly::fibers::FiberManager::addTask(), folly::fibers::FiberManager::fibersPoolSize(), i, folly::fibers::FiberManager::loopUntilNoReady(), and folly::fibers::FiberManager::Options::maxFibersPoolSize.

107  {
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 }
Single-threaded task execution engine.
int main ( int  argc,
char **  argv 
)

Definition at line 131 of file FibersBenchmark.cpp.

References folly::init(), and folly::runBenchmarks().

131  {
132  folly::init(&argc, &argv, true);
133 
135  return 0;
136 }
void runBenchmarks()
Definition: Benchmark.cpp:456
void init(int *argc, char ***argv, bool removeFlags)
Definition: Init.cpp:34
char ** argv
void runBenchmark ( size_t  numAwaits,
size_t  toSend 
)

Definition at line 31 of file FibersBenchmark.cpp.

References folly::fibers::FiberManager::addTask(), folly::fibers::await(), i, loop(), folly::fibers::FiberManager::loopController(), folly::gen::move, and sNumAwaits.

Referenced by BENCHMARK().

31  {
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 }
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
Single-threaded task execution engine.
static size_t sNumAwaits
void loop(int iters)
FirstArgOf< F >::type::value_type await(F &&func)

Variable Documentation

size_t sNumAwaits
static

Definition at line 29 of file FibersBenchmark.cpp.

Referenced by runBenchmark().