Browse Source

修改channel测试范例

3.0.0
tearshark 2 years ago
parent
commit
5ee1e7325b
3 changed files with 28 additions and 41 deletions
  1. 1
    1
      CMakeLists.txt
  2. 1
    1
      benchmark/CMakeLists.txt
  3. 26
    39
      tutorial/test_async_channel.cpp

+ 1
- 1
CMakeLists.txt View File

${ALL_TUTORIAL_FILES}) ${ALL_TUTORIAL_FILES})
target_link_libraries(test_librf PUBLIC librf) target_link_libraries(test_librf PUBLIC librf)
#add_subdirectory(benchmark)
add_subdirectory(benchmark)
endif() endif()

+ 1
- 1
benchmark/CMakeLists.txt View File



add_executable(${BENCHMARK_FILE_NAME} ${BENCHMARK_FILE_PATH}) add_executable(${BENCHMARK_FILE_NAME} ${BENCHMARK_FILE_PATH})
target_link_libraries(${BENCHMARK_FILE_NAME} PUBLIC librf) target_link_libraries(${BENCHMARK_FILE_NAME} PUBLIC librf)
target_compile_definitions(${TUTORIAL_FILE_NAME}
target_compile_definitions(${BENCHMARK_FILE_NAME}
PRIVATE LIBRF_TUTORIAL_STAND_ALONE=1 PRIVATE LIBRF_TUTORIAL_STAND_ALONE=1
) )
endforeach(BENCHMARK_FILE_PATH) endforeach(BENCHMARK_FILE_PATH)

+ 26
- 39
tutorial/test_async_channel.cpp View File

void test_channel_performance_four_thread(size_t buff_size) void test_channel_performance_four_thread(size_t buff_size)
{ {
//1的话,效率跟golang比,有点惨不忍睹。
//1000的话,由于几乎不需要调度器接入,效率就很高了,随便过千万数量级。
auto tstart = high_resolution_clock::now();
channel_t<bool> q{ 8 };
channel_t<int, false, true> c{ buff_size }; channel_t<int, false, true> c{ buff_size };
std::thread wr_th[4]; std::thread wr_th[4];
std::thread rd_th[4]; std::thread rd_th[4];
for (int i = 0; i < 4; ++i)
{
wr_th[i] = std::thread([c]
{
local_scheduler_t ls;
GO
{
for (int i = N - 1; i >= 0; --i)
{
co_await(c << i);
}
};
this_scheduler()->run_until_notask();
for (int i = 0; i < 4; ++i) {
wr_th[i] = std::thread([c, q] {
for (int i = N - 1; i >= 0; --i)
(void)(c << i);
(void)(q << true);
}); });
} }
for (int i = 0; i < 4; ++i)
{
rd_th[i] = std::thread([c]
{
local_scheduler_t ls;
auto tstart = high_resolution_clock::now();
GO
{
for (int i = N - 1; i >= 0; --i)
{
co_await c;
}
};
this_scheduler()->run_until_notask();
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
for (int i = 0; i < 4; ++i) {
rd_th[i] = std::thread([c, q] {
for (int i = N - 1; i >= 0; --i)
(void)c.read();
(void)(q << true);
}); });
} }
GO {
for (size_t i = 0; i < 8; ++i)
co_await q;
};
this_scheduler()->run_until_notask();
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
std::cout << "channel buff=" << c.capacity() << ", w/r " << N << " times, cost time " << dt << "s" << std::endl;
for (int i = 0; i < 4; ++i) for (int i = 0; i < 4; ++i)
rd_th[i].join();
for (int i = 0; i < 4; ++i)
{
wr_th[i].join(); wr_th[i].join();
rd_th[i].join();
}
} }
void test_channel_performance_four_coroutine(size_t capacity, size_t nThreads, int n) void test_channel_performance_four_coroutine(size_t capacity, size_t nThreads, int n)
std::cout << "four thread" << std::endl; std::cout << "four thread" << std::endl;
//test_channel_performance_four_thread(1); //test_channel_performance_four_thread(1);
//test_channel_performance_four_thread(1000);
test_channel_performance_four_thread(1000);
std::cout << "four coroutine" << std::endl; std::cout << "four coroutine" << std::endl;
test_channel_performance_four_coroutine(1000, 4, N); test_channel_performance_four_coroutine(1000, 4, N);

Loading…
Cancel
Save