/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2016 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/Singleton.h>
#include <folly/io/async/EventBase.h>
#include <folly/test/SingletonTestStructs.h>
-#include <folly/Benchmark.h>
#include <glog/logging.h>
#include <gtest/gtest.h>
EXPECT_NE(s2, nullptr);
EXPECT_EQ(s1, s2);
+ EXPECT_EQ(s1.get(), SingletonBasicUsage<Watchdog>::try_get_fast().get());
std::shared_ptr<ChildWatchdog> s3 =
SingletonBasicUsage<ChildWatchdog>::try_get();
using SingletonCreationError = Singleton<T, Tag, CreationErrorTag>;
TEST(Singleton, SingletonCreationError) {
- auto& vault = *SingletonVault::singleton<CreationErrorTag>();
+ SingletonVault::singleton<CreationErrorTag>();
SingletonCreationError<ErrorConstructor> error_once_singleton;
// first time should error out
- EXPECT_THROW(error_once_singleton.get_weak().lock(), std::runtime_error);
+ EXPECT_THROW(error_once_singleton.try_get(), std::runtime_error);
// second time it'll work fine
- error_once_singleton.get_weak().lock();
+ error_once_singleton.try_get();
SUCCEED();
}
std::vector<std::thread> ts;
for (size_t i = 0; i < 100; ++i) {
ts.emplace_back([&]() {
- slowpoke_singleton.get_weak().lock();
+ slowpoke_singleton.try_get();
});
}
[&] {didEagerInit = true; return new std::string("foo"); })
.shouldEagerInit();
folly::EventBase eb;
+ folly::Baton<> done;
vault.registrationComplete();
EXPECT_FALSE(didEagerInit);
- auto result = vault.doEagerInitVia(&eb); // a Future<Unit> is returned
+ vault.doEagerInitVia(eb, &done);
eb.loop();
- result.get(); // ensure this completed successfully and didn't hang forever
+ done.wait();
EXPECT_TRUE(didEagerInit);
sing.get_weak(); // (avoid compile error complaining about unused var 'sing')
}
for (size_t j = 0; j < kThreads; j++) {
threads.push_back(std::make_shared<std::thread>([&] {
barrier.wait();
- vault.doEagerInitVia(&exe).get();
+ vault.doEagerInitVia(exe);
}));
}
}
}
-// Benchmarking a normal singleton vs a Meyers singleton vs a Folly
-// singleton. Meyers are insanely fast, but (hopefully) Folly
-// singletons are fast "enough."
-int* getMeyersSingleton() {
- static auto ret = new int(0);
- return ret;
-}
-
-int normal_singleton_value = 0;
-int* getNormalSingleton() {
- doNotOptimizeAway(&normal_singleton_value);
- return &normal_singleton_value;
-}
-
struct MockTag {};
template <typename T, typename Tag = detail::DefaultTag>
using SingletonMock = Singleton <T, Tag, MockTag>;
EXPECT_NE(serial_count_first, serial_count_mock);
}
-struct BenchmarkSingleton {
- int val = 0;
-};
-
-BENCHMARK(NormalSingleton, n) {
- for (size_t i = 0; i < n; ++i) {
- doNotOptimizeAway(getNormalSingleton());
- }
-}
-
-BENCHMARK_RELATIVE(MeyersSingleton, n) {
- for (size_t i = 0; i < n; ++i) {
- doNotOptimizeAway(getMeyersSingleton());
- }
-}
-
-struct BenchmarkTag {};
-template <typename T, typename Tag = detail::DefaultTag>
-using SingletonBenchmark = Singleton <T, Tag, BenchmarkTag>;
-
-struct GetTag{};
-struct GetWeakTag{};
-
-SingletonBenchmark<BenchmarkSingleton, GetTag> benchmark_singleton_get;
-SingletonBenchmark<BenchmarkSingleton, GetWeakTag> benchmark_singleton_get_weak;
-
-BENCHMARK_RELATIVE(FollySingleton, n) {
- for (size_t i = 0; i < n; ++i) {
- SingletonBenchmark<BenchmarkSingleton, GetTag>::try_get();
- }
-}
-
-BENCHMARK_RELATIVE(FollySingletonWeak, n) {
- for (size_t i = 0; i < n; ++i) {
- SingletonBenchmark<BenchmarkSingleton, GetWeakTag>::get_weak();
- }
-}
-
int main(int argc, char* argv[]) {
testing::InitGoogleTest(&argc, argv);
google::InitGoogleLogging(argv[0]);
SingletonVault::singleton()->registrationComplete();
auto ret = RUN_ALL_TESTS();
- if (!ret) {
- folly::runBenchmarksOnFlag();
- }
+
return ret;
}