Fix copyright lines for Bits.h and move BitsBenchmark.cpp
[folly.git] / folly / test / ExceptionWrapperBenchmark.cpp
index 31e28f630a6537a5ae1e58a2b6816b0e035120c8..dc52f5ce3e7510c3ce93f9b8add581b445550f97 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-#include "folly/ExceptionWrapper.h"
+#include <folly/ExceptionWrapper.h>
 
-#include <gflags/gflags.h>
 #include <atomic>
 #include <exception>
-#include <vector>
 #include <stdexcept>
 #include <thread>
+#include <vector>
 
-#include "folly/Benchmark.h"
+#include <folly/Benchmark.h>
+#include <folly/portability/GFlags.h>
 
 DEFINE_int32(num_threads, 32, "Number of threads to run concurrency "
                               "benchmarks");
@@ -34,17 +34,19 @@ DEFINE_int32(num_threads, 32, "Number of threads to run concurrency "
  */
 BENCHMARK(exception_ptr_create_and_test, iters) {
   std::runtime_error e("payload");
-  for (int i = 0; i < iters; ++i) {
+  for (size_t i = 0; i < iters; ++i) {
     auto ep = std::make_exception_ptr(e);
-    assert(ep);
+    bool b = static_cast<bool>(ep);
+    folly::doNotOptimizeAway(b);
   }
 }
 
 BENCHMARK_RELATIVE(exception_wrapper_create_and_test, iters) {
   std::runtime_error e("payload");
-  for (int i = 0; i < iters; ++i) {
+  for (size_t i = 0; i < iters; ++i) {
     auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
-    assert(ew.get());
+    bool b = static_cast<bool>(ew);
+    folly::doNotOptimizeAway(b);
   }
 }
 
@@ -58,9 +60,10 @@ BENCHMARK(exception_ptr_create_and_test_concurrent, iters) {
       threads.emplace_back([&go, iters] {
         while (!go) { }
         std::runtime_error e("payload");
-        for (int i = 0; i < iters; ++i) {
+        for (size_t i = 0; i < iters; ++i) {
           auto ep = std::make_exception_ptr(e);
-          assert(ep);
+          bool b = static_cast<bool>(ep);
+          folly::doNotOptimizeAway(b);
         }
       });
     }
@@ -79,9 +82,10 @@ BENCHMARK_RELATIVE(exception_wrapper_create_and_test_concurrent, iters) {
       threads.emplace_back([&go, iters] {
         while (!go) { }
         std::runtime_error e("payload");
-        for (int i = 0; i < iters; ++i) {
+        for (size_t i = 0; i < iters; ++i) {
           auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
-          assert(ew.get());
+          bool b = static_cast<bool>(ew);
+          folly::doNotOptimizeAway(b);
         }
       });
     }
@@ -101,35 +105,32 @@ BENCHMARK_DRAW_LINE()
  */
 BENCHMARK(exception_ptr_create_and_throw, iters) {
   std::runtime_error e("payload");
-  for (int i = 0; i < iters; ++i) {
+  for (size_t i = 0; i < iters; ++i) {
     auto ep = std::make_exception_ptr(e);
     try {
       std::rethrow_exception(ep);
-      assert(false);
-    } catch (std::runtime_error& e) {
+    } catch (std::runtime_error&) {
     }
   }
 }
 
 BENCHMARK_RELATIVE(exception_wrapper_create_and_throw, iters) {
   std::runtime_error e("payload");
-  for (int i = 0; i < iters; ++i) {
+  for (size_t i = 0; i < iters; ++i) {
     auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
     try {
-      ew.throwException();
-      assert(false);
-    } catch (std::runtime_error& e) {
+      ew.throw_exception();
+    } catch (std::runtime_error&) {
     }
   }
 }
 
 BENCHMARK_RELATIVE(exception_wrapper_create_and_cast, iters) {
   std::runtime_error e("payload");
-  for (int i = 0; i < iters; ++i) {
+  for (size_t i = 0; i < iters; ++i) {
     auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
-    std::exception* basePtr = static_cast<std::exception*>(ew.get());
-    auto ep = dynamic_cast<std::runtime_error*>(basePtr);
-    assert(ep);
+    bool b = ew.is_compatible_with<std::runtime_error>();
+    folly::doNotOptimizeAway(b);
   }
 }
 
@@ -144,12 +145,11 @@ BENCHMARK(exception_ptr_create_and_throw_concurrent, iters) {
       threads.emplace_back([&go, iters] {
         while (!go) { }
         std::runtime_error e("payload");
-        for (int i = 0; i < iters; ++i) {
+        for (size_t i = 0; i < iters; ++i) {
           auto ep = std::make_exception_ptr(e);
           try {
             std::rethrow_exception(ep);
-            assert(false);
-          } catch (std::runtime_error& e) {
+          } catch (std::runtime_error&) {
           }
         }
       });
@@ -169,12 +169,11 @@ BENCHMARK_RELATIVE(exception_wrapper_create_and_throw_concurrent, iters) {
       threads.emplace_back([&go, iters] {
         while (!go) { }
         std::runtime_error e("payload");
-        for (int i = 0; i < iters; ++i) {
+        for (size_t i = 0; i < iters; ++i) {
           auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
           try {
-            ew.throwException();
-            assert(false);
-          } catch (std::runtime_error& e) {
+            ew.throw_exception();
+          } catch (std::runtime_error&) {
           }
         }
       });
@@ -194,11 +193,10 @@ BENCHMARK_RELATIVE(exception_wrapper_create_and_cast_concurrent, iters) {
       threads.emplace_back([&go, iters] {
         while (!go) { }
         std::runtime_error e("payload");
-        for (int i = 0; i < iters; ++i) {
+        for (size_t i = 0; i < iters; ++i) {
           auto ew = folly::make_exception_wrapper<std::runtime_error>(e);
-          std::exception* basePtr = static_cast<std::exception*>(ew.get());
-          auto ep = dynamic_cast<std::runtime_error*>(basePtr);
-          assert(ep);
+          bool b = ew.is_compatible_with<std::runtime_error>();
+          folly::doNotOptimizeAway(b);
         }
       });
     }
@@ -210,7 +208,7 @@ BENCHMARK_RELATIVE(exception_wrapper_create_and_cast_concurrent, iters) {
 }
 
 int main(int argc, char *argv[]) {
-  google::ParseCommandLineFlags(&argc, &argv, true);
+  gflags::ParseCommandLineFlags(&argc, &argv, true);
   folly::runBenchmarks();
   return 0;
 }