proxygen
NotificationQueueBenchmark.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018-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 <folly/Benchmark.h>
20 #include <condition_variable>
21 #include <mutex>
22 #include <thread>
23 
24 using namespace folly;
25 
26 static size_t constexpr kMaxRead = 20;
27 
28 void runTest(int iters, int numThreads) {
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 }
77 
84 
85 int main(int argc, char* argv[]) {
86  gflags::ParseCommandLineFlags(&argc, &argv, true);
88 
89  return 0;
90 }
static size_t constexpr kMaxRead
void runTest(int iters, int numThreads)
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
void runBenchmarks()
Definition: Benchmark.cpp:456
std::vector< std::thread::id > threads
char ** argv
void terminateLoopSoon()
Definition: EventBase.cpp:493
bool runInEventBaseThread(void(*fn)(T *), T *arg)
Definition: EventBase.h:794
static map< string, int > m
#define BENCHMARK_PARAM(name, param)
Definition: Benchmark.h:417
void setMaxReadAtOnce(uint32_t maxAtOnce)
Definition: EventBase.cpp:201
std::mutex mutex
int main(int argc, char *argv[])