1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-14 13:41:12 +00:00

Streamline WorkerThreadPool tests and make them more robust

This commit is contained in:
myaaaaaaaaa
2023-02-15 12:34:26 -05:00
parent 964a5353db
commit c996863464
2 changed files with 59 additions and 100 deletions

View File

@@ -37,120 +37,73 @@
namespace TestWorkerThreadPool { namespace TestWorkerThreadPool {
int u32scmp(const char32_t *l, const char32_t *r) { static LocalVector<SafeNumeric<int>> counter;
for (; *l == *r && *l && *r; l++, r++) {
// Continue.
}
return *l - *r;
}
static void static_test(void *p_arg) { static void static_test(void *p_arg) {
SafeNumeric<uint32_t> *counter = (SafeNumeric<uint32_t> *)p_arg; counter[(uint64_t)p_arg].increment();
counter->increment(); counter[0].add(2);
} }
static SafeNumeric<uint32_t> callable_counter;
static void static_callable_test() { static void static_callable_test() {
callable_counter.increment(); counter[0].sub(2);
} }
TEST_CASE("[WorkerThreadPool] Process threads using individual tasks") {
for (int iterations = 0; iterations < 500; iterations++) {
const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
const bool low_priority = Math::rand() % 2;
TEST_CASE("[WorkerThreadPool] Process 256 threads using native task") { LocalVector<WorkerThreadPool::TaskID> tasks1;
const int count = 256; LocalVector<WorkerThreadPool::TaskID> tasks2;
SafeNumeric<uint32_t> counter; tasks1.resize(count);
WorkerThreadPool::TaskID tasks[count]; tasks2.resize(count);
counter.clear();
counter.resize(count);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
tasks[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, &counter, true); tasks1[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, (void *)(uintptr_t)i, low_priority);
tasks2[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), !low_priority);
} }
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]); WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks1[i]);
WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks2[i]);
} }
CHECK(counter.get() == count); bool all_run_once = true;
}
TEST_CASE("[WorkerThreadPool] Process 256 threads using native low priority") {
const int count = 256;
SafeNumeric<uint32_t> counter = SafeNumeric<uint32_t>(0);
WorkerThreadPool::TaskID tasks[count];
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
tasks[i] = WorkerThreadPool::get_singleton()->add_native_task(static_test, &counter, false); //Reduce number of check messages
all_run_once &= counter[i].get() == 1;
} }
for (int i = 0; i < count; i++) { CHECK(all_run_once);
WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
} }
CHECK(counter.get() == count);
}
TEST_CASE("[WorkerThreadPool] Process 256 threads using callable") {
const int count = 256;
WorkerThreadPool::TaskID tasks[count];
callable_counter.set(0);
for (int i = 0; i < count; i++) {
tasks[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), true);
}
for (int i = 0; i < count; i++) {
WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
}
CHECK(callable_counter.get() == count);
}
TEST_CASE("[WorkerThreadPool] Process 256 threads using callable low priority") {
const int count = 256;
WorkerThreadPool::TaskID tasks[count];
callable_counter.set(0);
for (int i = 0; i < count; i++) {
tasks[i] = WorkerThreadPool::get_singleton()->add_task(callable_mp_static(static_callable_test), false);
}
for (int i = 0; i < count; i++) {
WorkerThreadPool::get_singleton()->wait_for_task_completion(tasks[i]);
}
CHECK(callable_counter.get() == count);
} }
static void static_group_test(void *p_arg, uint32_t p_index) { static void static_group_test(void *p_arg, uint32_t p_index) {
SafeNumeric<uint32_t> *counter = (SafeNumeric<uint32_t> *)p_arg; counter[p_index].increment();
counter->exchange_if_greater(p_index); counter[0].add((uintptr_t)p_arg);
} }
TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group") {
const int count = 256;
SafeNumeric<uint32_t> counter;
WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, &counter, count, -1, true);
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
CHECK(counter.get() == count - 1);
}
TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group low priority") {
const int count = 256;
SafeNumeric<uint32_t> counter;
WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, &counter, count, -1, false);
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group);
CHECK(counter.get() == count - 1);
}
static SafeNumeric<uint32_t> callable_group_counter;
static void static_callable_group_test(uint32_t p_index) { static void static_callable_group_test(uint32_t p_index) {
callable_group_counter.exchange_if_greater(p_index); counter[p_index].increment();
counter[0].sub(2);
} }
TEST_CASE("[WorkerThreadPool] Process elements using group tasks") {
for (int iterations = 0; iterations < 500; iterations++) {
const int count = Math::pow(2.0f, Math::random(0.0f, 5.0f));
const int tasks = Math::pow(2.0f, Math::random(0.0f, 5.0f));
const bool low_priority = Math::rand() % 2;
TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group") { counter.clear();
const int count = 256; counter.resize(count);
WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, -1, true); WorkerThreadPool::GroupID group1 = WorkerThreadPool::get_singleton()->add_native_group_task(static_group_test, (void *)2, count, tasks, !low_priority);
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group); WorkerThreadPool::GroupID group2 = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, tasks, low_priority);
CHECK(callable_group_counter.get() == count - 1); WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group1);
} WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group2);
TEST_CASE("[WorkerThreadPool] Process 256 elements on native task group low priority") { bool all_run_once = true;
const int count = 256; for (int i = 0; i < count; i++) {
callable_group_counter.set(0); //Reduce number of check messages
WorkerThreadPool::GroupID group = WorkerThreadPool::get_singleton()->add_group_task(callable_mp_static(static_callable_group_test), count, -1, false); all_run_once &= counter[i].get() == 2;
WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group); }
CHECK(callable_group_counter.get() == count - 1); CHECK(all_run_once);
}
} }
} // namespace TestWorkerThreadPool } // namespace TestWorkerThreadPool

View File

@@ -202,7 +202,7 @@ struct GodotTestCaseListener : public doctest::IReporter {
ThemeDB *theme_db = nullptr; ThemeDB *theme_db = nullptr;
void test_case_start(const doctest::TestCaseData &p_in) override { void test_case_start(const doctest::TestCaseData &p_in) override {
SignalWatcher::get_singleton()->_clear_signals(); reinitialize();
String name = String(p_in.m_name); String name = String(p_in.m_name);
String suite_name = String(p_in.m_test_suite); String suite_name = String(p_in.m_test_suite);
@@ -343,11 +343,11 @@ struct GodotTestCaseListener : public doctest::IReporter {
} }
void test_case_reenter(const doctest::TestCaseData &) override { void test_case_reenter(const doctest::TestCaseData &) override {
SignalWatcher::get_singleton()->_clear_signals(); reinitialize();
} }
void subcase_start(const doctest::SubcaseSignature &) override { void subcase_start(const doctest::SubcaseSignature &) override {
SignalWatcher::get_singleton()->_clear_signals(); reinitialize();
} }
void report_query(const doctest::QueryData &) override {} void report_query(const doctest::QueryData &) override {}
@@ -357,6 +357,12 @@ struct GodotTestCaseListener : public doctest::IReporter {
void log_assert(const doctest::AssertData &in) override {} void log_assert(const doctest::AssertData &in) override {}
void log_message(const doctest::MessageData &) override {} void log_message(const doctest::MessageData &) override {}
void test_case_skipped(const doctest::TestCaseData &) override {} void test_case_skipped(const doctest::TestCaseData &) override {}
private:
void reinitialize() {
Math::seed(0x60d07);
SignalWatcher::get_singleton()->_clear_signals();
}
}; };
REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener); REGISTER_LISTENER("GodotTestCaseListener", 1, GodotTestCaseListener);