| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #define BOOST_TEST_DYN_LINK
- #define BOOST_TEST_MAIN // in only one cpp file
- #include "utility/coro_signal2.hpp"
- #include "core/sophiar_obj.hpp"
- #include "utility/debug_utility.hpp"
- #include <boost/asio/co_spawn.hpp>
- #include <boost/asio/detached.hpp>
- #include <boost/asio/this_coro.hpp>
- #include <boost/asio/use_awaitable.hpp>
- #include <boost/test/unit_test.hpp>
- #include <chrono>
- #include <iostream>
- #include <vector>
- using boost::asio::awaitable;
- using boost::asio::co_spawn;
- using boost::asio::detached;
- using boost::asio::use_awaitable;
- using namespace sophiar;
- using namespace std::chrono_literals;
- coro_signal2 sig_a{global_context}, sig_b{global_context};
- awaitable<void> coro_a() {
- auto watcher_b = sig_b.new_watcher(global_context);
- FILE_LINE_TRACE
- for (int i = 0; i < 1000; ++i) {
- sig_a.try_notify_all();
- co_await watcher_b.coro_wait();
- }
- FILE_LINE_TRACE
- sig_a.try_notify_all();
- co_return;
- }
- awaitable<void> coro_b() {
- co_await coro_sleep(10ms);
- auto watcher_a = sig_a.new_watcher(global_context);
- FILE_LINE_TRACE
- for (int i = 0; i < 1000; ++i) {
- sig_b.try_notify_all();
- co_await watcher_a.coro_wait();
- }
- FILE_LINE_TRACE
- sig_b.try_notify_all();
- co_return;
- }
- awaitable<void> coro_signal2_bench() {
- constexpr auto length = 100000;
- std::vector<std::unique_ptr<coro_signal2>> sig_pool;
- std::vector<signal_watcher> watcher_pool;
- for (int i = 0; i < length; ++i) {
- auto new_sig = std::make_unique<coro_signal2>(global_context);
- watcher_pool.push_back(new_sig->new_watcher(global_context));
- sig_pool.push_back(std::move(new_sig));
- }
- for (int i = 0; i < length - 1; ++i) {
- co_spawn(global_context, [&]() -> awaitable<void> {
- int index = i;
- co_await watcher_pool[index].coro_wait();
- sig_pool[index + 1]->try_notify_all();
- }, detached);
- }
- co_await coro_sleep(10ms);
- FILE_LINE_TRACE
- sig_pool[0]->try_notify_all();
- co_await watcher_pool[length - 1].coro_wait();
- FILE_LINE_TRACE
- co_return;
- }
- BOOST_AUTO_TEST_CASE(test_coro_signal2) {
- // co_spawn(global_context, coro_a(), detached);
- // co_spawn(global_context, coro_b(), detached);
- co_spawn(global_context, coro_signal2_bench(), detached);
- global_context.run();
- }
|