Skip to content

Commit

Permalink
[test] add one test case (#380)
Browse files Browse the repository at this point in the history
* Create E05-per_test.cpp
  • Loading branch information
jiangliu123456 committed Jun 2, 2024
1 parent 86680c9 commit 7d5b5a9
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
3 changes: 2 additions & 1 deletion example/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ set(CGRAPH_EXAMPLE_LIST
E02-MockGUI
E03-ThirdFlow
E04-MapReduce
E05-per_test
)

foreach(example ${CGRAPH_EXAMPLE_LIST})
add_executable(${example}
$<TARGET_OBJECTS:CGraph>
${example}.cpp
)
endforeach()
endforeach()
105 changes: 105 additions & 0 deletions example/E05-per_test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "../src/CGraph.h"
#include "unistd.h"
using namespace CGraph;

volatile int flag[3] = {0,0,0};
#define LOOP 1000

class MyNode1 : public CGraph::GNode {
public:
CStatus run() override {
float a = 1.1;
float b = 2.2;
float c = 3.3;
for (int i = 0; i < 10; i++) {
a = b*c;
b++;
}
__sync_fetch_and_add(&flag[1], 1);
return CStatus();
}
};

class MyNode2 : public CGraph::GNode {
public:
CStatus run() override {
int j = -1;
int k = 0;
int t = 0;
for (int i = 0; i < 10; i++) {
t = k;
k = j;
j = t;
}
__sync_fetch_and_add(&flag[0], 1);
return CStatus();
}
};

class MyNode3 : public CGraph::GNode {
public:
CStatus run() override {
long a = 4;
long b = 5;
long c = 6;
for (int i = 0; i < 10; i++) {
a = a*a -1;
b = b*b -4;
c = c*c -3;
}
__sync_fetch_and_add(&flag[2], 1);
return CStatus();
}
};

int main() {
struct timespec ts;
struct timespec ts2;
int time[3] = {0,0,0};
for (int i= 0; i < 10000; i++)
{
/* 创建一个流水线,用于设定和执行流图信息 */
GPipelinePtr pipeline = GPipelineFactory::create();
GElementPtr a, b, c = nullptr;

UThreadPoolConfig config;
config.default_thread_size_ = 2;
config.secondary_thread_size_ = 0;
pipeline->setUniqueThreadPoolConfig(config); // 设置3个线程执行

//计时 1
clock_gettime(CLOCK_REALTIME, &ts);

int loop=LOOP;
while(loop--)
{
/* 注册节点之间的依赖关系 */
pipeline->registerGElement<MyNode1>(&a, {}, "nodeA");
pipeline->registerGElement<MyNode2>(&b, {}, "nodeB");
pipeline->registerGElement<MyNode3>(&c, {}, "nodeC");
// 等待任务执行完成
// ...
}
pipeline->process();
while(flag[0]!=LOOP || flag[1]!=LOOP || flag[2]!=LOOP) {
sleep(0);
}

//计时2
clock_gettime(CLOCK_REALTIME, &ts2);

GPipelineFactory::remove(pipeline);
flag[0] = 0;
flag[1] = 0;
flag[2] = 0;

time[0] = (ts2.tv_sec-ts.tv_sec)*1000000+(ts2.tv_nsec/1000)-(ts.tv_nsec/1000);
time[1]+=time[0];//sum
time[2] =time[1]/(i+1);//avg
printf("[loop:%04d]cur_time:%04d us,avg_time:%04d us\n", i+1,time[0], time[2]);
}

sleep(1);

return 0;
}

0 comments on commit 7d5b5a9

Please sign in to comment.