Sfoglia il codice sorgente

修改channel测试范例

3.0.0
tearshark 2 anni fa
parent
commit
5ee1e7325b
3 ha cambiato i file con 28 aggiunte e 41 eliminazioni
  1. 1
    1
      CMakeLists.txt
  2. 1
    1
      benchmark/CMakeLists.txt
  3. 26
    39
      tutorial/test_async_channel.cpp

+ 1
- 1
CMakeLists.txt Vedi File

@@ -148,7 +148,7 @@ if(CMAKE_ENABLE_UNIT_TEST)
${ALL_TUTORIAL_FILES})
target_link_libraries(test_librf PUBLIC librf)
#add_subdirectory(benchmark)
add_subdirectory(benchmark)
endif()

+ 1
- 1
benchmark/CMakeLists.txt Vedi File

@@ -7,7 +7,7 @@ foreach(BENCHMARK_FILE_PATH ${BENCHMARK_FILES})

add_executable(${BENCHMARK_FILE_NAME} ${BENCHMARK_FILE_PATH})
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
)
endforeach(BENCHMARK_FILE_PATH)

+ 26
- 39
tutorial/test_async_channel.cpp Vedi File

@@ -165,56 +165,43 @@ void test_channel_performance_double_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 };
std::thread wr_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)
rd_th[i].join();
for (int i = 0; i < 4; ++i)
{
wr_th[i].join();
rd_th[i].join();
}
}
void test_channel_performance_four_coroutine(size_t capacity, size_t nThreads, int n)
@@ -279,7 +266,7 @@ void resumable_main_channel()
std::cout << "four thread" << std::endl;
//test_channel_performance_four_thread(1);
//test_channel_performance_four_thread(1000);
test_channel_performance_four_thread(1000);
std::cout << "four coroutine" << std::endl;
test_channel_performance_four_coroutine(1000, 4, N);

Loading…
Annulla
Salva