Skip to content

Commit 869ba96

Browse files
committed
[test] add test case
1 parent b808c0d commit 869ba96

19 files changed

+151
-26
lines changed

CGraph-run-functional-tests.sh

100644100755
File mode changed.

CGraph-run-performance-tests.sh

100644100755
File mode changed.

CMakeLists.txt

+2-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ set(CMAKE_CXX_STANDARD 11)
1818
option(CGRAPH_BUILD_FUNCTIONAL_TESTS "Enables builds of functional tests" OFF)
1919
option(CGRAPH_BUILD_PERFORMANCE_TESTS "Enables builds of performance tests" OFF)
2020

21-
2221
# 如果开启此宏定义,则CGraph执行过程中,不会在控制台打印任何信息
2322
# add_definitions(-D_CGRAPH_SILENCE_)
2423

@@ -30,12 +29,12 @@ add_subdirectory(./example)
3029

3130
# 功能测试相关内容
3231
if(CGRAPH_BUILD_FUNCTIONAL_TESTS)
33-
message("[CGraph] build functional tests")
32+
message("-- [CGraph] build functional tests")
3433
add_subdirectory(./test/Functional)
3534
endif(CGRAPH_BUILD_FUNCTIONAL_TESTS)
3635

3736
# 性能测试相关内容
3837
if(CGRAPH_BUILD_PERFORMANCE_TESTS)
39-
message("[CGraph] build performance tests")
38+
message("-- [CGraph] build performance tests")
4039
add_subdirectory(./test/Performance)
4140
endif(CGRAPH_BUILD_PERFORMANCE_TESTS)

src/GraphCtrl/GraphElement/GElement.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -269,13 +269,15 @@ CStatus GElement::fatProcessor(const CFunctionType& type) {
269269
CGRAPH_FUNCTION_CHECK_STATUS
270270
status = init();
271271
doAspect(GAspectType::FINISH_INIT, status);
272+
trigger_times_ = 0;
272273
break;
273274
}
274275
case CFunctionType::DESTROY: {
275276
status = doAspect(GAspectType::BEGIN_DESTROY);
276277
CGRAPH_FUNCTION_CHECK_STATUS
277278
status = destroy();
278279
doAspect(GAspectType::FINISH_DESTROY, status);
280+
trigger_times_ = 0;
279281
break;
280282
}
281283
default:

src/GraphCtrl/GraphElement/GElementManager.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ CStatus GElementManager::init() {
3535
status = element->fatProcessor(CFunctionType::INIT);
3636
CGRAPH_FUNCTION_CHECK_STATUS
3737
element->is_init_ = true;
38-
element->trigger_times_ = 0;
3938
}
4039

4140
CGRAPH_FUNCTION_END
@@ -49,7 +48,6 @@ CStatus GElementManager::destroy() {
4948
status = element->fatProcessor(CFunctionType::DESTROY);
5049
CGRAPH_FUNCTION_CHECK_STATUS
5150
element->is_init_ = false;
52-
element->trigger_times_ = 0;
5351
}
5452

5553
CGRAPH_DELETE_PTR(engine_)

src/GraphCtrl/GraphElement/GGroup/GGroup.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ CStatus GGroup::init() {
2424
CGRAPH_FUNCTION_CHECK_STATUS
2525

2626
is_init_ = true;
27-
trigger_times_ = 0;
2827
CGRAPH_FUNCTION_END
2928
}
3029

@@ -39,7 +38,6 @@ CStatus GGroup::destroy() {
3938
CGRAPH_FUNCTION_CHECK_STATUS
4039

4140
is_init_ = false;
42-
trigger_times_ = 0;
4341
CGRAPH_FUNCTION_END
4442
}
4543

src/GraphCtrl/GraphElement/GGroup/GRegion/GRegion.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ CStatus GRegion::init() {
3838
this->manager_->setScheduleStrategy(CGRAPH_POOL_TASK_STRATEGY);
3939

4040
is_init_ = true;
41-
trigger_times_ = 0;
4241
CGRAPH_FUNCTION_END
4342
}
4443

@@ -49,7 +48,6 @@ CStatus GRegion::destroy() {
4948
CGRAPH_FUNCTION_CHECK_STATUS
5049

5150
is_init_ = false;
52-
trigger_times_ = 0;
5351
CGRAPH_FUNCTION_END
5452
}
5553

src/UtilsCtrl/Timer/UTimeCounter.h

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef CGRAPH_UTIMECOUNTER_H
1010
#define CGRAPH_UTIMECOUNTER_H
1111

12+
#include <string>
1213
#include <chrono>
1314

1415
#include "../UtilsObject.h"
@@ -18,16 +19,23 @@ CGRAPH_NAMESPACE_BEGIN
1819
class UTimeCounter : public UtilsObject {
1920
public:
2021
explicit UTimeCounter() {
22+
key_ = CGRAPH_DEFAULT;
2123
start_ts_ = std::chrono::steady_clock::now();
2224
}
2325

26+
explicit UTimeCounter(const std::string& key) {
27+
start_ts_ = std::chrono::steady_clock::now();
28+
key_ = key;
29+
}
30+
2431
~UTimeCounter() override {
2532
std::chrono::duration<double, std::milli> span = std::chrono::steady_clock::now() - start_ts_;
26-
CGraph::CGRAPH_ECHO("time counter is : [%0.2lf] ms", span.count());
33+
CGraph::CGRAPH_ECHO("[%s]: time counter is : [%0.2lf] ms", key_.c_str(), span.count());
2734
}
2835

2936
private:
3037
std::chrono::steady_clock::time_point start_ts_;
38+
std::string key_;
3139
};
3240

3341
CGRAPH_NAMESPACE_END

test/Functional/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
set(CGRAPH_FUNCTIONAL_LIST
33
test-functional-01
44
test-functional-02
5+
test-functional-03
56
)
67

78
foreach(func ${CGRAPH_FUNCTIONAL_LIST})

test/Functional/test-functional-01.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Desc:
77
***************************/
88

9-
#include "../_Materials/TestGNodes.h"
9+
#include "../_Materials/TestInclude.h"
1010

1111
using namespace CGraph;
1212

@@ -26,9 +26,10 @@ void test_functional_01() {
2626
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&j, {h});
2727

2828
{
29-
UTimeCounter counter;
30-
status = pipeline->process(1000000);
29+
UTimeCounter counter("test_functional_01");
30+
status = pipeline->process(500000);
3131
}
32+
3233
if (status.isErr()) {
3334
std::cout << status.getInfo() << std::endl;
3435
}

test/Functional/test-functional-02.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
@Desc:
77
***************************/
88

9-
10-
#include "../_Materials/TestGNodes.h"
9+
#include "../_Materials/TestInclude.h"
1110

1211
using namespace CGraph;
1312

@@ -46,7 +45,7 @@ void test_functional_02() {
4645
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&n, {l, cluster2}, "n");
4746

4847
{
49-
UTimeCounter counter;
48+
UTimeCounter counter("test_functional_02");
5049
status = pipeline->process(10000);
5150
}
5251

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: test-functional-03.cpp
5+
@Time: 2023/12/29 22:13
6+
@Desc:
7+
***************************/
8+
9+
10+
#include "../_Materials/TestInclude.h"
11+
12+
using namespace CGraph;
13+
14+
void test_functional_03() {
15+
CStatus status;
16+
GPipelinePtr pipeline = GPipelineFactory::create();
17+
GElementPtr a, b_cluster, c, d_region, e = nullptr;
18+
19+
b_cluster = pipeline->createGGroup<GCluster>({
20+
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB1", 1)), // 创建名为nodeB1的node信息,并将其放入b_cluster中
21+
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB2", 3)), // 创建名为nodeB2且自循环3次的node信息,并将其放入b_cluster中
22+
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB3", 1))
23+
});
24+
25+
GElementPtr d1, d2, d3, d4, d23_cluster = nullptr;
26+
d1 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo({}, "nodeD1", 1));
27+
d2 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeD2", 1)); // 创建node,稍后放入cluster中
28+
d3 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeD3", 1));
29+
d23_cluster = pipeline->createGGroup<GCluster>({d2, d3}, {d1}, "clusterD23", 1);
30+
d4 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo({d1}, "nodeD4", 1));
31+
d_region = pipeline->createGGroup<GRegion>({d1, d23_cluster, d4}); // 创建名为d_region的region信息,并将{d1,d23_cluster,d4}放入其中
32+
33+
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&a, {}, "nodeA", 1);
34+
status += pipeline->registerGGroup(&b_cluster, {}, "clusterB", 1);
35+
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&c, {a, b_cluster}, "nodeC", 1);
36+
status += pipeline->registerGGroup(&d_region, {a, b_cluster}, "regionD", 2); // 将名为regionD,依赖{a,b_cluster}执行且自循环2次的region信息,注册入pipeline中
37+
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&e, {c, d_region}, "nodeE", 1);
38+
if (!status.isOK()) {
39+
return;
40+
}
41+
42+
{
43+
UTimeCounter counter("test_functional_03");
44+
pipeline->addGAspect<TestMaterialAdd1GAspect>();
45+
status = pipeline->process(50000);
46+
}
47+
48+
if (g_test_node_cnt % 58 != 0) {
49+
// 58 是单次执行本测例的情况下,i++的次数。包含 aspect和 element
50+
std::cout << g_test_node_cnt << " num can not divide 58." << std::endl;
51+
}
52+
53+
if (status.isErr()) {
54+
std::cout << status.getInfo() << std::endl;
55+
}
56+
GPipelineFactory::remove(pipeline);
57+
}
58+
59+
60+
int main() {
61+
test_functional_03();
62+
return 0;
63+
}

test/Performance/test-performance-01.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Desc:
77
***************************/
88

9-
#include "../_Materials/TestGNodes.h"
9+
#include "../_Materials/TestInclude.h"
1010

1111
using namespace CGraph;
1212

@@ -33,7 +33,7 @@ void test_performance_01() {
3333
status += pipeline->init();
3434

3535
{
36-
UTimeCounter counter;
36+
UTimeCounter counter("test_performance_01");
3737
for (int t = 0; t < 500000; t++) {
3838
pipeline->run();
3939
}

test/Performance/test-performance-02.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Desc:
77
***************************/
88

9-
#include "../_Materials/TestGNodes.h"
9+
#include "../_Materials/TestInclude.h"
1010

1111
using namespace CGraph;
1212

@@ -23,7 +23,7 @@ void test_performance_02() {
2323
pipeline->setAutoCheck(false);
2424
status += pipeline->init();
2525
{
26-
UTimeCounter counter;
26+
UTimeCounter counter("test_performance_02");
2727
for (int t = 0; t < 1000000; t++) {
2828
pipeline->run();
2929
}

test/Performance/test-performance-03.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
@Desc:
77
***************************/
88

9-
#include "../_Materials/TestGNodes.h"
9+
#include "../_Materials/TestInclude.h"
1010

1111
using namespace CGraph;
1212

@@ -36,7 +36,7 @@ void test_performance_03() {
3636
status += pipeline->init();
3737

3838
{
39-
UTimeCounter counter;
39+
UTimeCounter counter("test_performance_03");
4040
for (int t = 0; t < 1000000; t++) {
4141
pipeline->run();
4242
}

test/_Materials/TestCommonDefine.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: TestCommonDefine.h
5+
@Time: 2023/12/29 22:00
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_TESTCOMMONDEFINE_H
10+
#define CGRAPH_TESTCOMMONDEFINE_H
11+
12+
#include <atomic>
13+
14+
#include "CGraph.h"
15+
16+
std::atomic<unsigned int> g_test_node_cnt = {0};
17+
18+
#endif //CGRAPH_TESTCOMMONDEFINE_H

test/_Materials/TestGAspects.h

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: TestGAspects.h
5+
@Time: 2023/12/29 21:59
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_TESTGASPECTS_H
10+
#define CGRAPH_TESTGASPECTS_H
11+
12+
#include "TestCommonDefine.h"
13+
14+
class TestMaterialAdd1GAspect : public CGraph::GAspect {
15+
public:
16+
CStatus beginRun() override {
17+
g_test_node_cnt++;
18+
return CStatus();
19+
}
20+
21+
CVoid finishRun(const CStatus& curStatus) override {
22+
g_test_node_cnt++;
23+
}
24+
};
25+
26+
27+
#endif //CGRAPH_TESTGASPECTS_H

test/_Materials/TestGNodes.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@
99
#ifndef CGRAPH_TESTGNODES_H
1010
#define CGRAPH_TESTGNODES_H
1111

12-
#include <atomic>
12+
#include "TestCommonDefine.h"
1313

14-
#include "CGraph.h"
15-
16-
std::atomic<unsigned int> g_test_node_cnt = {0};
1714
class TestMaterialAdd1GNode : public CGraph::GNode {
1815
public:
1916
CStatus init() override {

test/_Materials/TestInclude.h

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/***************************
2+
@Author: Chunel
3+
4+
@File: TestInclude.h
5+
@Time: 2023/12/29 22:01
6+
@Desc:
7+
***************************/
8+
9+
#ifndef CGRAPH_TESTINCLUDE_H
10+
#define CGRAPH_TESTINCLUDE_H
11+
12+
#include "TestCommonDefine.h"
13+
#include "TestGNodes.h"
14+
#include "TestGAspects.h"
15+
16+
#endif //CGRAPH_TESTINCLUDE_H

0 commit comments

Comments
 (0)