From: Michael Lee Date: Tue, 23 Feb 2016 16:00:49 +0000 (-0800) Subject: Split benchmarks and tests X-Git-Tag: deprecate-dynamic-initializer~48 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=d1d243a678452d0db095305a87e29c1d77406dc1;p=folly.git Split benchmarks and tests Summary:Benchmarks need their own main function, which means they aren't really useful in cases we can't use a custom main function. Reviewed By: yfeldblum Differential Revision: D2962104 fb-gh-sync-id: 25bdc6e5a8bdf8c3aa94d393207a74797b2e1234 shipit-source-id: 25bdc6e5a8bdf8c3aa94d393207a74797b2e1234 --- diff --git a/folly/test/BatonBenchmark.cpp b/folly/test/BatonBenchmark.cpp new file mode 100644 index 00000000..b9c86462 --- /dev/null +++ b/folly/test/BatonBenchmark.cpp @@ -0,0 +1,72 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace folly; +using namespace folly::test; +using folly::detail::EmulatedFutexAtomic; + +typedef DeterministicSchedule DSched; + +BENCHMARK(baton_pingpong, iters) { run_pingpong_test(iters); } + +BENCHMARK(baton_pingpong_emulated_futex, iters) { + run_pingpong_test(iters); +} + +BENCHMARK(posix_sem_pingpong, iters) { + sem_t sems[3]; + sem_t* a = sems + 0; + sem_t* b = sems + 2; // to get it on a different cache line + + sem_init(a, 0, 0); + sem_init(b, 0, 0); + auto thr = std::thread([=] { + for (size_t i = 0; i < iters; ++i) { + sem_wait(a); + sem_post(b); + } + }); + for (size_t i = 0; i < iters; ++i) { + sem_post(a); + sem_wait(b); + } + thr.join(); +} + +// I am omitting a benchmark result snapshot because these microbenchmarks +// mainly illustrate that PreBlockAttempts is very effective for rapid +// handoffs. The performance of Baton and sem_t is essentially identical +// to the required futex calls for the blocking case + +int main(int argc, char** argv) { + testing::InitGoogleTest(&argc, argv); + gflags::ParseCommandLineFlags(&argc, &argv, true); + + auto rv = RUN_ALL_TESTS(); + if (!rv && FLAGS_benchmark) { + folly::runBenchmarks(); + } + return rv; +} diff --git a/folly/test/BatonTest.cpp b/folly/test/BatonTest.cpp index 2d0e614f..dfe604ed 100644 --- a/folly/test/BatonTest.cpp +++ b/folly/test/BatonTest.cpp @@ -15,12 +15,11 @@ */ #include +#include #include #include #include -#include #include -#include using namespace folly; using namespace folly::test; @@ -34,111 +33,12 @@ TEST(Baton, basic) { b.wait(); } -template class Atom> -void run_pingpong_test(int numRounds) { - Baton batons[17]; - Baton& a = batons[0]; - Baton& b = batons[16]; // to get it on a different cache line - auto thr = DSched::thread([&]{ - for (int i = 0; i < numRounds; ++i) { - a.wait(); - a.reset(); - b.post(); - } - }); - for (int i = 0; i < numRounds; ++i) { - a.post(); - b.wait(); - b.reset(); - } - DSched::join(thr); -} - TEST(Baton, pingpong) { DSched sched(DSched::uniform(0)); run_pingpong_test(1000); } -BENCHMARK(baton_pingpong, iters) { - run_pingpong_test(iters); -} - -BENCHMARK(baton_pingpong_emulated_futex, iters) { - run_pingpong_test(iters); -} - -BENCHMARK(posix_sem_pingpong, iters) { - sem_t sems[3]; - sem_t* a = sems + 0; - sem_t* b = sems + 2; // to get it on a different cache line - - sem_init(a, 0, 0); - sem_init(b, 0, 0); - auto thr = std::thread([=]{ - for (size_t i = 0; i < iters; ++i) { - sem_wait(a); - sem_post(b); - } - }); - for (size_t i = 0; i < iters; ++i) { - sem_post(a); - sem_wait(b); - } - thr.join(); -} - -template class Atom, typename Clock> -void run_basic_timed_wait_tests() { - Baton b; - b.post(); - // tests if early delivery works fine - EXPECT_TRUE(b.timed_wait(Clock::now())); -} - -template class Atom, typename Clock> -void run_timed_wait_tmo_tests() { - Baton b; - - auto thr = DSched::thread([&]{ - bool rv = b.timed_wait(Clock::now() + std::chrono::milliseconds(1)); - // main thread is guaranteed to not post until timeout occurs - EXPECT_FALSE(rv); - }); - DSched::join(thr); -} - -template class Atom, typename Clock> -void run_timed_wait_regular_test() { - Baton b; - - auto thr = DSched::thread([&] { - // To wait forever we'd like to use time_point::max, but - // std::condition_variable does math to convert the timeout to - // system_clock without handling overflow. - auto farFuture = Clock::now() + std::chrono::hours(1000); - bool rv = b.timed_wait(farFuture); - if (!std::is_same, DeterministicAtomic>::value) { - // DeterministicAtomic ignores actual times, so doesn't guarantee - // a lack of timeout - EXPECT_TRUE(rv); - } - }); - - if (!std::is_same, DeterministicAtomic>::value) { - // If we are using std::atomic (or EmulatedFutexAtomic) then - // a sleep here guarantees to a large extent that 'thr' will - // execute wait before we post it, thus testing late delivery. For - // DeterministicAtomic, we just rely on DeterministicSchedule to do - // the scheduling. The test won't fail if we lose the race, we just - // don't get coverage. - std::this_thread::sleep_for(std::chrono::milliseconds(2)); - } - - b.post(); - DSched::join(thr); -} - TEST(Baton, timed_wait_basic_system_clock) { run_basic_timed_wait_tests(); run_basic_timed_wait_tests(); @@ -175,32 +75,8 @@ TEST(Baton, timed_wait_steady_clock) { run_timed_wait_regular_test(); } -template class Atom> -void run_try_wait_tests() { - Baton b; - EXPECT_FALSE(b.try_wait()); - b.post(); - EXPECT_TRUE(b.try_wait()); -} - TEST(Baton, try_wait) { run_try_wait_tests(); run_try_wait_tests(); run_try_wait_tests(); } - -// I am omitting a benchmark result snapshot because these microbenchmarks -// mainly illustrate that PreBlockAttempts is very effective for rapid -// handoffs. The performance of Baton and sem_t is essentially identical -// to the required futex calls for the blocking case - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - gflags::ParseCommandLineFlags(&argc, &argv, true); - - auto rv = RUN_ALL_TESTS(); - if (!rv && FLAGS_benchmark) { - folly::runBenchmarks(); - } - return rv; -} diff --git a/folly/test/BatonTestHelpers.h b/folly/test/BatonTestHelpers.h new file mode 100644 index 00000000..0d86b361 --- /dev/null +++ b/folly/test/BatonTestHelpers.h @@ -0,0 +1,108 @@ +/* + * 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +#include + +namespace folly { +namespace test { + +typedef DeterministicSchedule DSched; + +template