From f5487bd88f0264c7fe8117db35998b62d5ec78ed Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Sep 2024 17:58:18 +0800 Subject: [PATCH] test: perf parallel spawn function --- src/core_test/codec/common_code.cc | 12 ++---------- src/core_test/codec/helper/codec.h | 16 ++++++++++++++++ src/core_test/codec/raw_code.cc | 14 +++----------- src/core_test/codec/short_code.cc | 12 ++---------- 4 files changed, 23 insertions(+), 31 deletions(-) diff --git a/src/core_test/codec/common_code.cc b/src/core_test/codec/common_code.cc index 3bd4ecd..68afc8a 100644 --- a/src/core_test/codec/common_code.cc +++ b/src/core_test/codec/common_code.cc @@ -198,8 +198,7 @@ TEST(CommonCode, code_string) { } TEST(CommonCode, DISABLED_global_verify) { - BS::thread_pool pool; - auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [](auto start, auto end) { + const auto result = parallel_spawn(0x10'0000'0000ULL, [](uint64_t start, uint64_t end) { std::vector codes; for (uint64_t common_code = start; common_code < end; ++common_code) { // brute-force search if (CommonCode::check(common_code)) { @@ -207,13 +206,6 @@ TEST(CommonCode, DISABLED_global_verify) { } } return codes; - }, 0x1000); // split as 4096 pieces - - std::vector result; - result.reserve(ALL_CASES_NUM_); - for (auto &future : futures) { - const auto data = future.get(); - result.insert(result.end(), data.begin(), data.end()); // combine sections - } + }); EXPECT_EQ(result, all_common_codes()); } diff --git a/src/core_test/codec/helper/codec.h b/src/core_test/codec/helper/codec.h index f6e9baa..7d8182a 100644 --- a/src/core_test/codec/helper/codec.h +++ b/src/core_test/codec/helper/codec.h @@ -36,3 +36,19 @@ void common_code_parallel(std::function)> &&func); #define COMMON_CODE_PARALLEL(impl) CODE_PARALLEL(Common, common, impl) // ----------------------------------------------------------------------------------------- // + +/// Calculate multiple ranges separately and combine the results. +template +requires std::is_integral_v && std::is_invocable_v +auto parallel_spawn(T limit, F &&func) -> std::invoke_result_t { + BS::thread_pool pool; + std::invoke_result_t result; + for (auto &&future : pool.submit_blocks((T)0, limit, func, 0x1000)) { + const auto data = future.get(); + result.insert(result.end(), std::begin(data), std::end(data)); // combine sections + } + pool.wait(); + return result; +} + +// ----------------------------------------------------------------------------------------- // diff --git a/src/core_test/codec/raw_code.cc b/src/core_test/codec/raw_code.cc index 4547e0f..2ab3167 100644 --- a/src/core_test/codec/raw_code.cc +++ b/src/core_test/codec/raw_code.cc @@ -165,22 +165,14 @@ TEST(RawCode, DISABLED_global_verify) { return raw_code; }; - BS::thread_pool pool; - auto futures = pool.submit_blocks(0ULL, 0x10'0000'0000ULL, [](auto start, auto end) { + const auto result = parallel_spawn(0x10'0000'0000ULL, [](uint64_t start, uint64_t end) { std::vector codes; for (uint64_t common_code = start; common_code < end; ++common_code) { if (RawCode::check(force_convert(common_code))) { - codes.emplace_back(common_code); // store valid raw code + codes.emplace_back(common_code); // store valid code } } return codes; - }, 0x1000); // split as 4096 pieces - - std::vector result; - result.reserve(ALL_CASES_NUM_); - for (auto &future : futures) { - const auto data = future.get(); - result.insert(result.end(), data.begin(), data.end()); // combine sections - } + }); EXPECT_EQ(result, all_common_codes()); } diff --git a/src/core_test/codec/short_code.cc b/src/core_test/codec/short_code.cc index ca0851d..c587ea9 100644 --- a/src/core_test/codec/short_code.cc +++ b/src/core_test/codec/short_code.cc @@ -222,8 +222,7 @@ TEST(ShortCode, code_string) { TEST(ShortCode, DISABLED_global_verify) { speed_up_reset(); - BS::thread_pool pool; - auto futures = pool.submit_blocks(0U, SHORT_CODE_LIMIT, [](auto start, auto end) { + const auto result = parallel_spawn(SHORT_CODE_LIMIT, [](uint32_t start, uint32_t end) { std::vector codes; codes.reserve(end - start); for (uint32_t short_code = start; short_code < end; ++short_code) { @@ -232,13 +231,6 @@ TEST(ShortCode, DISABLED_global_verify) { codes.emplace_back(common_code.unwrap()); } return codes; - }, 0x1000); // split as 4096 pieces - - std::vector result; - result.reserve(ALL_CASES_NUM_); - for (auto &future : futures) { - const auto data = future.get(); - result.insert(result.end(), data.begin(), data.end()); // combine sections - } + }); EXPECT_EQ(result, all_common_codes()); }