proxygen
NotificationQueueBenchmark.cpp File Reference
#include <folly/Benchmark.h>
#include <folly/io/async/EventBase.h>
#include <folly/synchronization/Baton.h>
#include <condition_variable>
#include <mutex>
#include <thread>

Go to the source code of this file.

Functions

void runTest (int iters, int numThreads)
 
int main (int argc, char *argv[])
 

Variables

static size_t constexpr kMaxRead = 20
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 85 of file NotificationQueueBenchmark.cpp.

References argv, and folly::runBenchmarks().

85  {
86  gflags::ParseCommandLineFlags(&argc, &argv, true);
88 
89  return 0;
90 }
void runBenchmarks()
Definition: Benchmark.cpp:456
char ** argv
void runTest ( int  iters,
int  numThreads 
)

Definition at line 28 of file NotificationQueueBenchmark.cpp.

References BENCHMARK_PARAM, folly::BenchmarkSuspender::dismiss(), i, kMaxRead, folly::EventBase::loopForever(), m, mutex, folly::BenchmarkSuspender::rehire(), folly::EventBase::runInEventBaseThread(), folly::EventBase::setMaxReadAtOnce(), folly::pushmi::detail::t, folly::EventBase::terminateLoopSoon(), and threads.

28  {
29  BenchmarkSuspender susp;
30  EventBase evb;
32 
33  std::mutex m;
34  std::condition_variable cv;
35  int numRunning = 0;
36  int numProcessed = 0;
37  int numTotal = iters * numThreads;
38 
39  std::vector<std::thread> threads;
40  for (int i = 0; i < numThreads; i++) {
41  threads.push_back(std::thread([&]() mutable {
42  // wait for all the threads to start up
43  bool notifyAll = false;
44  {
45  std::lock_guard<std::mutex> lk(m);
46  if (++numRunning == numThreads) {
47  notifyAll = true;
48  susp.dismiss();
49  }
50  }
51 
52  if (notifyAll) {
53  cv.notify_all();
54  } else {
55  std::unique_lock<std::mutex> lk(m);
56  cv.wait(lk, [&]() { return numRunning == numThreads; });
57  }
58 
59  for (auto j = 0; j < iters; j++) {
60  evb.runInEventBaseThread([&]() mutable {
61  if (++numProcessed == numTotal) {
62  evb.terminateLoopSoon();
63  ;
64  }
65  });
66  }
67  }));
68  }
69 
70  evb.loopForever();
71  susp.rehire();
72 
73  for (auto& t : threads) {
74  t.join();
75  }
76 }
static size_t constexpr kMaxRead
std::vector< std::thread::id > threads
void terminateLoopSoon()
Definition: EventBase.cpp:493
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
static map< string, int > m
void setMaxReadAtOnce(uint32_t maxAtOnce)
Definition: EventBase.cpp:201
std::mutex mutex

Variable Documentation

size_t constexpr kMaxRead = 20
static

Definition at line 26 of file NotificationQueueBenchmark.cpp.

Referenced by runTest().