/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve. 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 using paddle::AsyncThreadPool; // NOLINT TEST(AsyncThreadPool, addJob) { AsyncThreadPool pool(8); auto a = pool.addJob([]{ return 1; }); auto b = pool.addJob([] { return true; }); auto c = pool.addJob([] { return false; }); ASSERT_EQ(a.get(), 1); ASSERT_TRUE(b.get()); ASSERT_FALSE(c.get()); } TEST(AsyncThreadPool, addBatchJob) { AsyncThreadPool pool(8); std::atomic counter{0}; std::vector jobs; for (int i = 0; i < 10000; i++) { jobs.emplace_back( [&] { counter++; }); } pool.addBatchJobs(jobs); ASSERT_EQ(counter, 10000); } TEST(AsyncThreadPool, multiThreadAddBatchJob) { AsyncThreadPool levelOnePool(200); AsyncThreadPool levelTwoPool(200); std::shared_ptr mut = std::make_shared(); int counter = 0; const int numMonitors = 300; const int numSlaves = 300; std::vector moniterJobs(numMonitors, [&] { std::vector slaveJobs(numSlaves, [mut, &counter] { std::lock_guard lk(*mut); counter++; }); levelTwoPool.addBatchJobs(slaveJobs); }); levelOnePool.addBatchJobs(moniterJobs); ASSERT_EQ(counter, numMonitors * numSlaves); } TEST(AsyncThreadPool, addBatchJobWithResults) { AsyncThreadPool pool(100); std::vector > jobs; const int numJobs = 100; for (int i = 0; i < numJobs; i++) { jobs.emplace_back( [i]{ return i; }); } std::vector res; pool.addBatchJobs(jobs, res); for (int i = 0; i < numJobs; i++) { ASSERT_EQ(res[i], i); } } int main(int argc, char** argv) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }