/*
- * 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.
*/
#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/portability/GFlags.h>
DEFINE_int32(num_threads, 32, "Number of threads to run concurrency "
"benchmarks");
*/
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);
}
}
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);
}
});
}
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);
}
});
}
*/
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);
}
}
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&) {
}
}
});
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&) {
}
}
});
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);
}
});
}
}
int main(int argc, char *argv[]) {
- google::ParseCommandLineFlags(&argc, &argv, true);
+ gflags::ParseCommandLineFlags(&argc, &argv, true);
folly::runBenchmarks();
return 0;
}