Skip to content

Commit

Permalink
[feat] add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunelFeng committed Dec 3, 2023
1 parent 8c6fb59 commit 31fab44
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 3 deletions.
10 changes: 8 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ message("* \\____/ \\____/ /_/ \\__,_/ / .___/ /_/ /_/ *")
message("* /_/ by Chunel Feng *")
message("* * * * * * * * * * * * * * * * * * * * * * * * * * * * *")

cmake_minimum_required(VERSION 3.2.5)
cmake_minimum_required(VERSION 3.2.0)

project(CGraph VERSION 2.5.3)
project(CGraph VERSION 2.5.4)

# CGraph默认使用C++11版本,推荐使用C++17版本。暂不支持C++11以下版本
set(CMAKE_CXX_STANDARD 11)
Expand All @@ -23,3 +23,9 @@ include(cmake/CGraph-env-include.cmake)

add_subdirectory(./tutorial)
add_subdirectory(./example)

# 功能测试相关内容
# add_subdirectory(./test/functional)

# 性能测试相关内容
# add_subdirectory(./test/performance)
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,9 @@ int main() {
* 提供`proto`定义文件
* 添加`mutable`(异变)功能,提供依赖关系注册语法糖

[2023.12.03 - v2.5.4 - Chunel]
* 提供`test`内容,包含性能和功能方面的测试用例

</details>

------------
Expand Down
35 changes: 35 additions & 0 deletions src/UtilsCtrl/Timer/UTimeCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: UTimeCounter.h
@Time: 2023/12/3 17:49
@Desc:
***************************/

#ifndef CGRAPH_UTIMECOUNTER_H
#define CGRAPH_UTIMECOUNTER_H

#include <chrono>

#include "../UtilsObject.h"

CGRAPH_NAMESPACE_BEGIN

class UTimeCounter : public UtilsObject {
public:
explicit UTimeCounter() {
start_ts_ = std::chrono::steady_clock::now();
}

~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());
}

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

CGRAPH_NAMESPACE_END

#endif //CGRAPH_UTIMECOUNTER_H
15 changes: 15 additions & 0 deletions src/UtilsCtrl/Timer/UTimerInclude.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: UTimerInclude.h
@Time: 2023/12/3 17:50
@Desc:
***************************/

#ifndef CGRAPH_UTIMERINCLUDE_H
#define CGRAPH_UTIMERINCLUDE_H

#include "UTimer.h"
#include "UTimeCounter.h"

#endif //CGRAPH_UTIMERINCLUDE_H
2 changes: 1 addition & 1 deletion src/UtilsCtrl/UtilsInclude.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "Singleton/USingleton.h"
#include "Lru/ULru.h"
#include "Trie/UTrie.h"
#include "Timer/UTimer.h"
#include "Timer/UTimerInclude.h"
#include "Distance/UDistanceInclude.h"
#include "Random/URandom.h"
#include "SerialUniqueArray/USerialUniqueArray.h"
Expand Down
25 changes: 25 additions & 0 deletions test/_materials/TestGNodes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: TestGNodes.h
@Time: 2023/12/3 17:40
@Desc:
***************************/

#ifndef CGRAPH_TESTGNODES_H
#define CGRAPH_TESTGNODES_H

#include <atomic>

#include "CGraph.h"

std::atomic<unsigned int> g_test_cnt = {0};
class TestMaterialAdd1GNode : public CGraph::GNode {
public:
CStatus run() override {
g_test_cnt++;
return CStatus();
}
};

#endif //CGRAPH_TESTGNODES_H
10 changes: 10 additions & 0 deletions test/functional/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

set(CGRAPH_FUNCTIONAL_LIST
)

foreach(func ${CGRAPH_FUNCTIONAL_LIST})
add_executable(${func}
$<TARGET_OBJECTS:CGraph>
${func}.cpp
)
endforeach()
15 changes: 15 additions & 0 deletions test/performance/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

set(CGRAPH_PERFORMANCE_LIST
test-performance-01
test-performance-02
test-performance-03
)


foreach(perf ${CGRAPH_PERFORMANCE_LIST})
add_executable(${perf}
# 在自己的工程中引入CGraph功能,仅需引入 CGraph-env-include.cmake 后,加入这一句话即可
$<TARGET_OBJECTS:CGraph>
${perf}.cpp
)
endforeach()
50 changes: 50 additions & 0 deletions test/performance/test-performance-01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: test-performance-01.cpp
@Time: 2023/12/3 17:41
@Desc:
***************************/

#include "../_materials/TestGNodes.h"

using namespace CGraph;

void test_performance_01() {
// 并行的执行32次,对应第1个例子,8thread,32并发,50w次
GPipelinePtr pipeline = GPipelineFactory::create();
CStatus status;
GElementPtr arr[32];
UThreadPoolConfig config;
config.default_thread_size_ = 8;
config.secondary_thread_size_ = 0;
config.max_task_steal_range_ = 7;
config.max_thread_size_ = 8;
config.primary_thread_policy_ = CGRAPH_THREAD_SCHED_RR;
config.primary_thread_priority_ = 10;
config.primary_thread_empty_interval_ = 1;
config.primary_thread_busy_epoch_ = 500;
config.monitor_enable_ = false; // 关闭扩缩容机制
pipeline->setUniqueThreadPoolConfig(config);
for (auto& i : arr) {
pipeline->registerGElement<TestMaterialAdd1GNode>(&i);
}
pipeline->setAutoCheck(false);
status += pipeline->init();

{
UTimeCounter counter;
for (int t = 0; t < 500000; t++) {
pipeline->run();
}
}

status += pipeline->destroy();
GPipelineFactory::remove(pipeline);
}


int main() {
test_performance_01();
return 0;
}
39 changes: 39 additions & 0 deletions test/performance/test-performance-02.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: test-performance-02.cpp
@Time: 2023/12/3 18:28
@Desc:
***************************/

#include "../_materials/TestGNodes.h"

using namespace CGraph;

void test_performance_02() {
// 串行执行32次,对应第二个例子,1thread,32串行,1000w次
GPipelinePtr pipeline = GPipelineFactory::create();
CStatus status;
GElementPtr arr[32];
pipeline->registerGElement<TestMaterialAdd1GNode>(&arr[0]);
for (int i = 1; i < 32; i++) {
pipeline->registerGElement<TestMaterialAdd1GNode>(&arr[i], {arr[i - 1]});
}
pipeline->makeSerial();
pipeline->setAutoCheck(false);
status += pipeline->init();
{
UTimeCounter counter;
for (int t = 0; t < 1000000; t++) {
pipeline->run();
}
}

status += pipeline->destroy();
GPipelineFactory::remove(pipeline);
}

int main() {
test_performance_02();
return 0;
}
52 changes: 52 additions & 0 deletions test/performance/test-performance-03.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/***************************
@Author: Chunel
@Contact: [email protected]
@File: test-performance-03.cpp
@Time: 2023/12/3 18:29
@Desc:
***************************/

#include "../_materials/TestGNodes.h"

using namespace CGraph;

void test_performance_03() {
// 简单dag场景,对应第三个例子,2thread,dag,100w次
GPipelinePtr pipeline = GPipelineFactory::create();
CStatus status;
GElementPtr a,b1,b2,c1,c2,d = nullptr;
UThreadPoolConfig config;
config.default_thread_size_ = 2; // 我的笔记本,是8核心的 macbook pro m1
config.secondary_thread_size_ = 0;
config.max_task_steal_range_ = 1;
config.max_thread_size_ = 2;
config.primary_thread_empty_interval_ = 1;
config.primary_thread_busy_epoch_ = 500;
config.monitor_enable_ = false; // 关闭扩缩容机制
config.primary_thread_policy_ = CGRAPH_THREAD_SCHED_RR;
config.primary_thread_priority_ = 10;
pipeline->setUniqueThreadPoolConfig(config);
pipeline->setAutoCheck(false);
pipeline->registerGElement<TestMaterialAdd1GNode>(&a);
pipeline->registerGElement<TestMaterialAdd1GNode>(&b1, {a});
pipeline->registerGElement<TestMaterialAdd1GNode>(&b2, {b1});
pipeline->registerGElement<TestMaterialAdd1GNode>(&c1, {a});
pipeline->registerGElement<TestMaterialAdd1GNode>(&c2, {c1});
pipeline->registerGElement<TestMaterialAdd1GNode>(&d, {b2, c2});
status += pipeline->init();

{
UTimeCounter counter;
for (int t = 0; t < 1000000; t++) {
pipeline->run();
}
}

status += pipeline->destroy();
GPipelineFactory::remove(pipeline);
}

int main() {
test_performance_03();
return 0;
}

0 comments on commit 31fab44

Please sign in to comment.