Changeset View
Changeset View
Standalone View
Standalone View
source/ps/tests/test_TaskManager.h
/* Copyright (C) 2021 Wildfire Games. | /* Copyright (C) 2021 Wildfire Games. | ||||
Lint: Inaccurate Copyright Year: Inaccurate Copyright Year | |||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
* GNU General Public License for more details. | * GNU General Public License for more details. | ||||
* | * | ||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "lib/self_test.h" | #include "lib/self_test.h" | ||||
#include "ps/Future.h" | #include "ps/Future.h" | ||||
#include "ps/TaskManager.h" | #include "ps/TaskManager.h" | ||||
#include <atomic> | #include <atomic> | ||||
#include <array> | |||||
#include <chrono> | |||||
#include <condition_variable> | #include <condition_variable> | ||||
#include <mutex> | #include <mutex> | ||||
#include <optional> | |||||
#include <thread> | |||||
#include <type_traits> | |||||
using namespace std::literals; | |||||
class TestTaskManager : public CxxTest::TestSuite | class TestTaskManager : public CxxTest::TestSuite | ||||
{ | { | ||||
public: | public: | ||||
void test_basic() | void test_basic() | ||||
{ | { | ||||
Threading::TaskManager& taskManager = Threading::TaskManager::Instance(); | Threading::TaskManager& taskManager = Threading::TaskManager::Instance(); | ||||
// There is a minimum of 3. | // There is a minimum of 3. | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | #define ITERATIONS 100000 | ||||
f1.Wait(); | f1.Wait(); | ||||
f2.Wait(); | f2.Wait(); | ||||
f3.Wait(); | f3.Wait(); | ||||
for (size_t i = 0; i < ITERATIONS; ++i) | for (size_t i = 0; i < ITERATIONS; ++i) | ||||
TS_ASSERT_EQUALS(futures[i].Get(), 5); | TS_ASSERT_EQUALS(futures[i].Get(), 5); | ||||
#undef ITERATIONS | #undef ITERATIONS | ||||
} | } | ||||
/** | |||||
* Measures the performance cost all Future have to pay. Even if they return nothing and never have | |||||
* to wait for the result. | |||||
*/ | |||||
void test_perf_mandatory_cost_DISABLED() | |||||
{ | |||||
constexpr size_t iterations{50000}; | |||||
Threading::TaskManager& manager{Threading::TaskManager::Instance()}; | |||||
// Use class over lambda so it is readable in the debug-symbols. | |||||
class Noop | |||||
{ | |||||
public: | |||||
void operator()() noexcept {}; | |||||
}; | |||||
// Use std::optional so we can measure destruction. | |||||
auto futures{std::make_optional<std::array<Future<void>, iterations>>()}; | |||||
const auto beginning{std::chrono::steady_clock::now()}; | |||||
for (Future<void>& future : *futures) | |||||
future = manager.PushTask(Noop{}); | |||||
const auto enqueued{std::chrono::steady_clock::now()}; | |||||
for (Future<void>& future : *futures) | |||||
future.Wait(); | |||||
const auto joined{std::chrono::steady_clock::now()}; | |||||
futures.reset(); | |||||
const auto destructed{std::chrono::steady_clock::now()}; | |||||
std::cout << "async cost:\n"; | |||||
std::cout << "enqueueing:\t" | |||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(enqueued - beginning).count() | |||||
<< "ms\n"; | |||||
std::cout << "work done:\t" | |||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(joined - beginning).count() | |||||
<< "ms\n"; | |||||
std::cout << "total:\t" | |||||
<< std::chrono::duration_cast<std::chrono::milliseconds>(destructed - beginning).count() | |||||
<< "ms\n"; | |||||
} | |||||
/** | |||||
* A more realistic measurement: Enqueue one task and wait for it. | |||||
*/ | |||||
void test_perf_round_trip_DISABLED() | |||||
{ | |||||
constexpr size_t iterations{50000}; | |||||
Threading::TaskManager& manager{Threading::TaskManager::Instance()}; | |||||
// Use class over lambda so it is readable in the debug-symbols. | |||||
class EmulateWork | |||||
{ | |||||
public: | |||||
auto operator()() | |||||
{ | |||||
// We have to time this since it might sleep longer than the provided duration. | |||||
const auto beginSleep{std::chrono::steady_clock::now()}; | |||||
std::this_thread::sleep_for(1us); | |||||
return std::chrono::steady_clock::now() - beginSleep; | |||||
} | |||||
}; | |||||
auto accumulator{std::chrono::steady_clock::duration::zero()}; | |||||
for (size_t i{0}; i != iterations; ++i) | |||||
{ | |||||
const auto beginning{std::chrono::steady_clock::now()}; | |||||
Future f{manager.PushTask(EmulateWork{})}; | |||||
const auto sleepTime{f.Get()}; | |||||
const auto joined{std::chrono::steady_clock::now()}; | |||||
static_assert( | |||||
std::is_same_v<decltype(accumulator), std::remove_const_t<decltype(sleepTime)>>); | |||||
accumulator += joined - beginning - sleepTime; | |||||
} | |||||
std::cout << "async round-trip:\n"; | |||||
std::cout << "total:\t" << | |||||
std::chrono::duration_cast<std::chrono::milliseconds>(accumulator).count() << "ms\n"; | |||||
} | |||||
}; | }; |
Wildfire Games · Phabricator
Inaccurate Copyright Year