Skip to content

Commit

Permalink
[test] add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Dec 29, 2023
1 parent b808c0d commit 869ba96
Show file tree
Hide file tree
Showing 19 changed files with 151 additions and 26 deletions.
Empty file modified CGraph-run-functional-tests.sh
100644 → 100755
Empty file.
Empty file modified CGraph-run-performance-tests.sh
100644 → 100755
Empty file.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ set(CMAKE_CXX_STANDARD 11)
option(CGRAPH_BUILD_FUNCTIONAL_TESTS "Enables builds of functional tests" OFF)
option(CGRAPH_BUILD_PERFORMANCE_TESTS "Enables builds of performance tests" OFF)


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

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

# 功能测试相关内容
if(CGRAPH_BUILD_FUNCTIONAL_TESTS)
message("[CGraph] build functional tests")
message("-- [CGraph] build functional tests")
add_subdirectory(./test/Functional)
endif(CGRAPH_BUILD_FUNCTIONAL_TESTS)

# 性能测试相关内容
if(CGRAPH_BUILD_PERFORMANCE_TESTS)
message("[CGraph] build performance tests")
message("-- [CGraph] build performance tests")
add_subdirectory(./test/Performance)
endif(CGRAPH_BUILD_PERFORMANCE_TESTS)
2 changes: 2 additions & 0 deletions src/GraphCtrl/GraphElement/GElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,15 @@ CStatus GElement::fatProcessor(const CFunctionType& type) {
CGRAPH_FUNCTION_CHECK_STATUS
status = init();
doAspect(GAspectType::FINISH_INIT, status);
trigger_times_ = 0;
break;
}
case CFunctionType::DESTROY: {
status = doAspect(GAspectType::BEGIN_DESTROY);
CGRAPH_FUNCTION_CHECK_STATUS
status = destroy();
doAspect(GAspectType::FINISH_DESTROY, status);
trigger_times_ = 0;
break;
}
default:
Expand Down
2 changes: 0 additions & 2 deletions src/GraphCtrl/GraphElement/GElementManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ CStatus GElementManager::init() {
status = element->fatProcessor(CFunctionType::INIT);
CGRAPH_FUNCTION_CHECK_STATUS
element->is_init_ = true;
element->trigger_times_ = 0;
}

CGRAPH_FUNCTION_END
Expand All @@ -49,7 +48,6 @@ CStatus GElementManager::destroy() {
status = element->fatProcessor(CFunctionType::DESTROY);
CGRAPH_FUNCTION_CHECK_STATUS
element->is_init_ = false;
element->trigger_times_ = 0;
}

CGRAPH_DELETE_PTR(engine_)
Expand Down
2 changes: 0 additions & 2 deletions src/GraphCtrl/GraphElement/GGroup/GGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ CStatus GGroup::init() {
CGRAPH_FUNCTION_CHECK_STATUS

is_init_ = true;
trigger_times_ = 0;
CGRAPH_FUNCTION_END
}

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

is_init_ = false;
trigger_times_ = 0;
CGRAPH_FUNCTION_END
}

Expand Down
2 changes: 0 additions & 2 deletions src/GraphCtrl/GraphElement/GGroup/GRegion/GRegion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ CStatus GRegion::init() {
this->manager_->setScheduleStrategy(CGRAPH_POOL_TASK_STRATEGY);

is_init_ = true;
trigger_times_ = 0;
CGRAPH_FUNCTION_END
}

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

is_init_ = false;
trigger_times_ = 0;
CGRAPH_FUNCTION_END
}

Expand Down
10 changes: 9 additions & 1 deletion src/UtilsCtrl/Timer/UTimeCounter.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef CGRAPH_UTIMECOUNTER_H
#define CGRAPH_UTIMECOUNTER_H

#include <string>
#include <chrono>

#include "../UtilsObject.h"
Expand All @@ -18,16 +19,23 @@ CGRAPH_NAMESPACE_BEGIN
class UTimeCounter : public UtilsObject {
public:
explicit UTimeCounter() {
key_ = CGRAPH_DEFAULT;
start_ts_ = std::chrono::steady_clock::now();
}

explicit UTimeCounter(const std::string& key) {
start_ts_ = std::chrono::steady_clock::now();
key_ = key;
}

~UTimeCounter() override {
std::chrono::duration<double, std::milli> span = std::chrono::steady_clock::now() - start_ts_;
CGraph::CGRAPH_ECHO("time counter is : [%0.2lf] ms", span.count());
CGraph::CGRAPH_ECHO("[%s]: time counter is : [%0.2lf] ms", key_.c_str(), span.count());
}

private:
std::chrono::steady_clock::time_point start_ts_;
std::string key_;
};

CGRAPH_NAMESPACE_END
Expand Down
1 change: 1 addition & 0 deletions test/Functional/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
set(CGRAPH_FUNCTIONAL_LIST
test-functional-01
test-functional-02
test-functional-03
)

foreach(func ${CGRAPH_FUNCTIONAL_LIST})
Expand Down
7 changes: 4 additions & 3 deletions test/Functional/test-functional-01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Desc:
***************************/

#include "../_Materials/TestGNodes.h"
#include "../_Materials/TestInclude.h"

using namespace CGraph;

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

{
UTimeCounter counter;
status = pipeline->process(1000000);
UTimeCounter counter("test_functional_01");
status = pipeline->process(500000);
}

if (status.isErr()) {
std::cout << status.getInfo() << std::endl;
}
Expand Down
5 changes: 2 additions & 3 deletions test/Functional/test-functional-02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
@Desc:
***************************/


#include "../_Materials/TestGNodes.h"
#include "../_Materials/TestInclude.h"

using namespace CGraph;

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

{
UTimeCounter counter;
UTimeCounter counter("test_functional_02");
status = pipeline->process(10000);
}

Expand Down
63 changes: 63 additions & 0 deletions test/Functional/test-functional-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: test-functional-03.cpp
@Time: 2023/12/29 22:13
@Desc:
***************************/


#include "../_Materials/TestInclude.h"

using namespace CGraph;

void test_functional_03() {
CStatus status;
GPipelinePtr pipeline = GPipelineFactory::create();
GElementPtr a, b_cluster, c, d_region, e = nullptr;

b_cluster = pipeline->createGGroup<GCluster>({
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB1", 1)), // 创建名为nodeB1的node信息,并将其放入b_cluster中
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB2", 3)), // 创建名为nodeB2且自循环3次的node信息,并将其放入b_cluster中
pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeB3", 1))
});

GElementPtr d1, d2, d3, d4, d23_cluster = nullptr;
d1 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo({}, "nodeD1", 1));
d2 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeD2", 1)); // 创建node,稍后放入cluster中
d3 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo("nodeD3", 1));
d23_cluster = pipeline->createGGroup<GCluster>({d2, d3}, {d1}, "clusterD23", 1);
d4 = pipeline->createGNode<TestMaterialAdd1GNode>(GNodeInfo({d1}, "nodeD4", 1));
d_region = pipeline->createGGroup<GRegion>({d1, d23_cluster, d4}); // 创建名为d_region的region信息,并将{d1,d23_cluster,d4}放入其中

status += pipeline->registerGElement<TestMaterialAdd1GNode>(&a, {}, "nodeA", 1);
status += pipeline->registerGGroup(&b_cluster, {}, "clusterB", 1);
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&c, {a, b_cluster}, "nodeC", 1);
status += pipeline->registerGGroup(&d_region, {a, b_cluster}, "regionD", 2); // 将名为regionD,依赖{a,b_cluster}执行且自循环2次的region信息,注册入pipeline中
status += pipeline->registerGElement<TestMaterialAdd1GNode>(&e, {c, d_region}, "nodeE", 1);
if (!status.isOK()) {
return;
}

{
UTimeCounter counter("test_functional_03");
pipeline->addGAspect<TestMaterialAdd1GAspect>();
status = pipeline->process(50000);
}

if (g_test_node_cnt % 58 != 0) {
// 58 是单次执行本测例的情况下,i++的次数。包含 aspect和 element
std::cout << g_test_node_cnt << " num can not divide 58." << std::endl;
}

if (status.isErr()) {
std::cout << status.getInfo() << std::endl;
}
GPipelineFactory::remove(pipeline);
}


int main() {
test_functional_03();
return 0;
}
4 changes: 2 additions & 2 deletions test/Performance/test-performance-01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Desc:
***************************/

#include "../_Materials/TestGNodes.h"
#include "../_Materials/TestInclude.h"

using namespace CGraph;

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

{
UTimeCounter counter;
UTimeCounter counter("test_performance_01");
for (int t = 0; t < 500000; t++) {
pipeline->run();
}
Expand Down
4 changes: 2 additions & 2 deletions test/Performance/test-performance-02.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Desc:
***************************/

#include "../_Materials/TestGNodes.h"
#include "../_Materials/TestInclude.h"

using namespace CGraph;

Expand All @@ -23,7 +23,7 @@ void test_performance_02() {
pipeline->setAutoCheck(false);
status += pipeline->init();
{
UTimeCounter counter;
UTimeCounter counter("test_performance_02");
for (int t = 0; t < 1000000; t++) {
pipeline->run();
}
Expand Down
4 changes: 2 additions & 2 deletions test/Performance/test-performance-03.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Desc:
***************************/

#include "../_Materials/TestGNodes.h"
#include "../_Materials/TestInclude.h"

using namespace CGraph;

Expand Down Expand Up @@ -36,7 +36,7 @@ void test_performance_03() {
status += pipeline->init();

{
UTimeCounter counter;
UTimeCounter counter("test_performance_03");
for (int t = 0; t < 1000000; t++) {
pipeline->run();
}
Expand Down
18 changes: 18 additions & 0 deletions test/_Materials/TestCommonDefine.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: TestCommonDefine.h
@Time: 2023/12/29 22:00
@Desc:
***************************/

#ifndef CGRAPH_TESTCOMMONDEFINE_H
#define CGRAPH_TESTCOMMONDEFINE_H

#include <atomic>

#include "CGraph.h"

std::atomic<unsigned int> g_test_node_cnt = {0};

#endif //CGRAPH_TESTCOMMONDEFINE_H
27 changes: 27 additions & 0 deletions test/_Materials/TestGAspects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: TestGAspects.h
@Time: 2023/12/29 21:59
@Desc:
***************************/

#ifndef CGRAPH_TESTGASPECTS_H
#define CGRAPH_TESTGASPECTS_H

#include "TestCommonDefine.h"

class TestMaterialAdd1GAspect : public CGraph::GAspect {
public:
CStatus beginRun() override {
g_test_node_cnt++;
return CStatus();
}

CVoid finishRun(const CStatus& curStatus) override {
g_test_node_cnt++;
}
};


#endif //CGRAPH_TESTGASPECTS_H
5 changes: 1 addition & 4 deletions test/_Materials/TestGNodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@
#ifndef CGRAPH_TESTGNODES_H
#define CGRAPH_TESTGNODES_H

#include <atomic>
#include "TestCommonDefine.h"

#include "CGraph.h"

std::atomic<unsigned int> g_test_node_cnt = {0};
class TestMaterialAdd1GNode : public CGraph::GNode {
public:
CStatus init() override {
Expand Down
16 changes: 16 additions & 0 deletions test/_Materials/TestInclude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: TestInclude.h
@Time: 2023/12/29 22:01
@Desc:
***************************/

#ifndef CGRAPH_TESTINCLUDE_H
#define CGRAPH_TESTINCLUDE_H

#include "TestCommonDefine.h"
#include "TestGNodes.h"
#include "TestGAspects.h"

#endif //CGRAPH_TESTINCLUDE_H

0 comments on commit 869ba96

Please sign in to comment.