已合并
添加覆盖率测试脚本 #2312
nuerxiati创建于 2025年2月27日
添加覆盖率测试脚本 #2312
已合并
nuerxiati创建于 2025年2月27日
master合入到master
2 个文件变更+144-0
Mtests/README.md+3-0
@@ -739,7 +739,10 @@
739 </tr>739 </tr>
740</table>740</table>
741 741 
742+### DT覆盖率看护
743+在NPU机器运行 `run_coverage.sh` 脚本,运行目录将生成 `htmlcov` 文件夹,将该文件夹复制到本地电脑,在浏览器中打开 `htmlcov/index.html` 文件,可以看到覆盖率信息。
742 744 
745+脚本中 `branch` 的值改为 `True` ,可以测试分支覆盖率。
743 746 
744 747 
745### 开发规则748### 开发规则
Atests/run_coverage.sh+141-0
@@ -0,0 +1,141 @@
1+# This script is used to run the coverage test for the pipeline, unit tests and shell scripts.
2+# define the base directory
3+BASE_DIR=$(dirname $(dirname "$(readlink -f "$0")"))
4+SOURCE_DIR="$BASE_DIR/mindspeed_llm"
5+PIPELINE_DIR="$BASE_DIR/tests/pipeline"
6+UT_DIR="$BASE_DIR/tests/ut"
7+ST_DIR="$BASE_DIR/tests/st/shell_scripts"
8+ 
9+# remove the existing coverage files
10+rm -f .coverage
11+rm -f .coverage.*
12+rm -rf htmlcov
13+ 
14+ 
15+# create the coverage configuration file
16+cat > ".coveragerc" << EOF
17+[run]
18+branch = False
19+parallel = False
20+source = $SOURCE_DIR
21+ 
22+[report]
23+show_missing = True
24+skip_covered = False
25+EOF
26+ 
27+ 
28+add_coverage() {
29+ sed -i "1a\import random" pretrain_gpt.py
30+ sed -i "2a\import time" pretrain_gpt.py
31+ sed -i "3a\import coverage" pretrain_gpt.py
32+ sed -i '4a\cov = coverage.Coverage(data_suffix=f"usecase-{time.time_ns()}_{random.randint(0, 100)}")' pretrain_gpt.py
33+ sed -i "5a\cov.start()" pretrain_gpt.py
34+ 
35+ sed -i "/ main()/a\ cov.stop()" pretrain_gpt.py
36+ sed -i "/ cov.stop()/a\ cov.save()" pretrain_gpt.py
37+ 
38+ sed -i "1a\import random" posttrain_gpt.py
39+ sed -i "2a\import time" posttrain_gpt.py
40+ sed -i "3a\import coverage" posttrain_gpt.py
41+ sed -i '4a\cov = coverage.Coverage(data_suffix=f"usecase-{time.time_ns()}_{random.randint(0, 100)}")' posttrain_gpt.py
42+ sed -i "5a\cov.start()" posttrain_gpt.py
43+ 
44+ sed -i "/ launch()/a\ cov.stop()" posttrain_gpt.py
45+ sed -i "/ cov.stop()/a\ cov.save()" posttrain_gpt.py
46+ 
47+ sed -i "1a\import random" ray_gpt.py
48+ sed -i "2a\import time" ray_gpt.py
49+ sed -i "3a\import coverage" ray_gpt.py
50+ sed -i '4a\cov = coverage.Coverage(data_suffix=f"usecase-{time.time_ns()}_{random.randint(0, 100)}")' ray_gpt.py
51+ sed -i "5a\cov.start()" ray_gpt.py
52+ 
53+ sed -i "/ main()/a\ cov.stop()" ray_gpt.py
54+ sed -i "/ cov.stop()/a\ cov.save()" ray_gpt.py
55+}
56+ 
57+ 
58+remove_coverage() {
59+ sed -i "2d" pretrain_gpt.py
60+ sed -i "2d" pretrain_gpt.py
61+ sed -i "2d" pretrain_gpt.py
62+ sed -i "2d" pretrain_gpt.py
63+ sed -i "2d" pretrain_gpt.py
64+ 
65+ sed -i "/ cov.stop()/d" pretrain_gpt.py
66+ sed -i "/ cov.save()/d" pretrain_gpt.py
67+ 
68+ sed -i "2d" posttrain_gpt.py
69+ sed -i "2d" posttrain_gpt.py
70+ sed -i "2d" posttrain_gpt.py
71+ sed -i "2d" posttrain_gpt.py
72+ sed -i "2d" posttrain_gpt.py
73+ 
74+ sed -i "/ cov.stop()/d" posttrain_gpt.py
75+ sed -i "/ cov.save()/d" posttrain_gpt.py
76+ 
77+ sed -i "2d" ray_gpt.py
78+ sed -i "2d" ray_gpt.py
79+ sed -i "2d" ray_gpt.py
80+ sed -i "2d" ray_gpt.py
81+ sed -i "2d" ray_gpt.py
82+ 
83+ sed -i "/ cov.stop()/d" ray_gpt.py
84+ sed -i "/ cov.save()/d" ray_gpt.py
85+}
86+ 
87+ 
88+# run the coverage for python files in the pipeline
89+find "$PIPELINE_DIR" -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
90+ if [ -d "$dir" ]; then
91+ find "$dir" -type f -name "*.py" | while read -r file; do
92+ coverage run -p --source=$SOURCE_DIR $file
93+ done
94+ fi
95+done
96+ 
97+# run the coverage for python files in the unit tests
98+find "$UT_DIR" -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
99+ if [ -d "$dir" ]; then
100+ find "$dir" -type f -name "*.py" | while read -r file; do
101+ coverage run -p --source=$SOURCE_DIR $file
102+ done
103+ fi
104+done
105+ 
106+add_coverage
107+ 
108+# run the coverage for shell scripts in the st
109+for test_case in "$ST_DIR"/*.sh; do
110+ file_name=$(basename "${test_case}")
111+ echo "Running $file_name..."
112+ bash $test_case
113+ SCRIPT_EXITCODE=${PIPESTATUS[0]}
114+ if [ $SCRIPT_EXITCODE -ne 0 ]; then
115+ echo "Script has failed. Exit!"
116+ remove_coverage()
117+ exit 1
118+ fi
119+done
120+ 
121+# run the coverage for shell scripts in the pipeline
122+find "$PIPELINE_DIR" -mindepth 1 -maxdepth 1 -type d | while read -r dir; do
123+ if [ -d "$dir" ]; then
124+ find "$dir" -type f -name "*.sh" | while read -r file; do
125+ bash $file
126+ SCRIPT_EXITCODE=${PIPESTATUS[0]}
127+ if [ $SCRIPT_EXITCODE -ne 0 ]; then
128+ echo "Script has failed. Exit!"
129+ remove_coverage()
130+ exit 1
131+ fi
132+ done
133+ fi
134+done
135+ 
136+remove_coverage
137+ 
138+ 
139+# generate the coverage report
140+coverage combine
141+coverage html