folly copyright 2015 -> copyright 2016
[folly.git] / folly / experimental / exception_tracer / ExceptionTracerBenchmark.cpp
1 /*
2  * Copyright 2016 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 <gflags/gflags.h>
22 #include <glog/logging.h>
23
24 #include <folly/Benchmark.h>
25 #include <folly/experimental/exception_tracer/ExceptionTracer.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) {
40       folly::exception_tracer::getCurrentExceptions();
41     }
42   }
43 }
44
45 BENCHMARK(ExceptionTracer, iters) {
46   std::vector<std::thread> threads;
47   constexpr size_t kNumThreads = 10;
48   threads.resize(10);
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]);
60   folly::runBenchmarks();
61   return 0;
62 }