Skip to content

Commit 70b7067

Browse files
committed
新增一个channel测试项目
1 parent 5316643 commit 70b7067

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
/vs_proj/x64
66
/vs_proj/.vs
77
/vs_proj/librf.vcxproj.user
8+
/.vs
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
2+
#include <chrono>
3+
#include <iostream>
4+
#include <string>
5+
#include <thread>
6+
#include <deque>
7+
#include <mutex>
8+
9+
#include "librf.h"
10+
11+
using namespace resumef;
12+
using namespace std::chrono;
13+
using namespace std::literals;
14+
15+
const static auto MaxNum = 20000;
16+
using int_channel_ptr = std::shared_ptr<channel_t<intptr_t>>;
17+
18+
static future_vt passing_next(int_channel_ptr rd, int_channel_ptr wr)
19+
{
20+
for (;;)
21+
{
22+
intptr_t value = co_await *rd;
23+
co_await (*wr << (value + 1));
24+
}
25+
}
26+
27+
void benchmark_main_channel_passing_next()
28+
{
29+
int_channel_ptr head = std::make_shared<channel_t<intptr_t>>(1);
30+
int_channel_ptr in = head;
31+
int_channel_ptr tail = nullptr;
32+
33+
for (int i = 0; i < MaxNum; ++i)
34+
{
35+
tail = std::make_shared<channel_t<intptr_t>>(1);
36+
go passing_next(in, tail);
37+
in = tail;
38+
}
39+
40+
GO
41+
{
42+
for (;;)
43+
{
44+
auto tstart = high_resolution_clock::now();
45+
46+
co_await (*head << 0);
47+
intptr_t value = co_await *tail;
48+
49+
auto dt = duration_cast<duration<double>>(high_resolution_clock::now() - tstart).count();
50+
std::cout << value << " cost time " << dt << "s" << std::endl;
51+
}
52+
};
53+
54+
this_scheduler()->run_until_notask();
55+
}

vs_proj/librf.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
<ItemGroup>
183183
<ClCompile Include="..\benchmark\benchmark_asio_echo.cpp" />
184184
<ClCompile Include="..\benchmark\benchmark_async_mem.cpp" />
185+
<ClCompile Include="..\benchmark\benchmark_channel_passing_next.cpp" />
185186
<ClCompile Include="..\librf\src\event.cpp" />
186187
<ClCompile Include="..\librf\src\mutex.cpp" />
187188
<ClCompile Include="..\librf\src\rf_task.cpp" />

vs_proj/librf.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@
106106
<ClCompile Include="..\tutorial\test_async_modern_cb.cpp">
107107
<Filter>tutorial</Filter>
108108
</ClCompile>
109+
<ClCompile Include="..\benchmark\benchmark_channel_passing_next.cpp">
110+
<Filter>benchmark</Filter>
111+
</ClCompile>
109112
</ItemGroup>
110113
<ItemGroup>
111114
<ClInclude Include="..\librf\librf.h">

0 commit comments

Comments
 (0)