proxygen
ExceptionTracerBenchmark.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 <stdexcept>
18 #include <thread>
19 #include <vector>
20 
21 #include <glog/logging.h>
22 
23 #include <folly/Benchmark.h>
26 
27 void recurse(int level) {
28  if (level == 0) {
29  throw std::runtime_error("");
30  }
31  recurse(level - 1);
32  folly::doNotOptimizeAway(0); // prevent tail recursion
33 }
34 
35 void loop(int iters) {
36  for (int i = 0; i < iters * 100; ++i) {
37  try {
38  recurse(100);
39  } catch (const std::exception& e) {
41  }
42  }
43 }
44 
45 BENCHMARK(ExceptionTracer, iters) {
46  std::vector<std::thread> threads;
47  constexpr size_t kNumThreads = 10;
48  threads.resize(kNumThreads);
49  for (auto& t : threads) {
50  t = std::thread([iters]() { loop(iters); });
51  }
52  for (auto& t : threads) {
53  t.join();
54  }
55 }
56 
57 int main(int argc, char* argv[]) {
58  gflags::ParseCommandLineFlags(&argc, &argv, true);
59  google::InitGoogleLogging(argv[0]);
61  return 0;
62 }
int main(int argc, char *argv[])
static size_t const kNumThreads
void runBenchmarks()
Definition: Benchmark.cpp:456
std::vector< std::thread::id > threads
char ** argv
std::vector< ExceptionInfo > getCurrentExceptions()
void loop(int iters)
BENCHMARK(ExceptionTracer, iters)
void recurse(int level)
auto doNotOptimizeAway(const T &datum) -> typename std::enable_if< !detail::DoNotOptimizeAwayNeedsIndirect< T >::value >::type
Definition: Benchmark.h:258