run_tests.sh 6.8 KB
Newer Older
chengshunyan's avatar
chengshunyan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/bin/bash
# DCU Performance Analyzer 测试脚本
# 用于验证工具的功能正确性和稳定性

set -e

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

# 工具路径
ANALYZER="./dist/dcu_analyzer"
PYTHON_ANALYZER="./dcu_performance_analyzer.py"

# 测试计数
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0

# 日志文件
TEST_LOG="test_results.log"

# 函数定义
log_info() {
    echo -e "${GREEN}[INFO]${NC} $1" | tee -a "$TEST_LOG"
}

log_error() {
    echo -e "${RED}[ERROR]${NC} $1" | tee -a "$TEST_LOG"
}

log_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1" | tee -a "$TEST_LOG"
}

run_test() {
    local test_name="$1"
    local test_cmd="$2"
    local expected_result="$3"
    
    TOTAL_TESTS=$((TOTAL_TESTS + 1))
    
    log_info "运行测试: $test_name"
    echo "命令: $test_cmd" | tee -a "$TEST_LOG"
    
    if eval "$test_cmd"; then
        if [ "$expected_result" = "pass" ]; then
            log_info "✓ $test_name 通过"
            PASSED_TESTS=$((PASSED_TESTS + 1))
            return 0
        else
            log_error "✗ $test_name 失败 (期望失败但通过了)"
            FAILED_TESTS=$((FAILED_TESTS + 1))
            return 1
        fi
    else
        if [ "$expected_result" = "fail" ]; then
            log_info "✓ $test_name 通过 (期望失败)"
            PASSED_TESTS=$((PASSED_TESTS + 1))
            return 0
        else
            log_error "✗ $test_name 失败"
            FAILED_TESTS=$((FAILED_TESTS + 1))
            return 1
        fi
    fi
}

check_file_exists() {
    local file="$1"
    local description="$2"
    
    if [ -f "$file" ]; then
        log_info "✓ $description 文件存在: $file"
        return 0
    else
        log_error "✗ $description 文件不存在: $file"
        return 1
    fi
}

check_directory_exists() {
    local dir="$1"
    local description="$2"
    
    if [ -d "$dir" ]; then
        log_info "✓ $description 目录存在: $dir"
        return 0
    else
        log_error "✗ $description 目录不存在: $dir"
        return 1
    fi
}

# 清理函数
cleanup() {
    log_info "清理测试文件..."
    rm -rf dcu_analysis_* test_output 2>/dev/null || true
}

# 主测试函数
main() {
    echo "=========================================" | tee "$TEST_LOG"
    echo "DCU Performance Analyzer 测试套件" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    echo "测试开始时间: $(date)" | tee -a "$TEST_LOG"
    echo "" | tee -a "$TEST_LOG"
    
    # 清理之前的测试文件
    cleanup
    
    # 测试1: Python版本帮助信息
    run_test "Python版本帮助信息" \
        "python3 $PYTHON_ANALYZER --help > /dev/null 2>&1" \
        "pass"
    
    # 测试2: Python版本版本信息
    run_test "Python版本版本信息" \
        "python3 $PYTHON_ANALYZER --version > /dev/null 2>&1" \
        "pass"
    
    # 测试3: 可执行文件帮助信息
    if [ -f "$ANALYZER" ]; then
        run_test "可执行文件帮助信息" \
            "$ANALYZER --help > /dev/null 2>&1" \
            "pass"
        
        # 测试4: 可执行文件版本信息
        run_test "可执行文件版本信息" \
            "$ANALYZER --version > /dev/null 2>&1" \
            "pass"
    else
        log_warning "可执行文件不存在,跳过相关测试"
    fi
    
    # 测试5: 基本功能测试 (Python版本)
    run_test "基本功能测试 (Python)" \
        "python3 $PYTHON_ANALYZER -c system performance -o test_output > /dev/null 2>&1" \
        "pass"
    
    # 验证输出文件
    if check_directory_exists "test_output" "输出目录"; then
        check_file_exists "test_output/reports/analysis_report.json" "JSON报告"
        check_file_exists "test_output/reports/analysis_summary.txt" "文本摘要"
        check_file_exists "test_output/data/system_info.json" "系统信息"
        check_file_exists "test_output/data/performance_metrics.json" "性能指标"
        check_file_exists "test_output/logs/analysis.log" "分析日志"
    fi
    
    # 测试6: 数据打包功能
    if [ -d "test_output" ]; then
        run_test "数据打包功能" \
            "python3 $PYTHON_ANALYZER -c system -o test_package > /dev/null 2>&1 && ls test_package.tar.gz > /dev/null 2>&1" \
            "pass"
    fi
    
    # 测试7: 静默模式
    run_test "静默模式" \
        "python3 $PYTHON_ANALYZER -c system -q -o test_quiet > /dev/null 2>&1" \
        "pass"
    
    # 测试8: 调试模式
    run_test "调试模式" \
        "python3 $PYTHON_ANALYZER -c system -d -o test_debug > /dev/null 2>&1" \
        "pass"
    
    # 测试9: 错误处理 (无效模块)
    run_test "错误处理 (无效模块)" \
        "python3 $PYTHON_ANALYZER -c invalid_module -o test_error 2>&1 | grep -q 'error'" \
        "fail"
    
    # 测试10: 单元测试
    if [ -f "test_analyzer.py" ]; then
        run_test "单元测试" \
            "python3 -m pytest test_analyzer.py -v > /dev/null 2>&1 || python3 test_analyzer.py" \
            "pass"
    else
        log_warning "单元测试文件不存在,跳过单元测试"
    fi
    
    # 测试11: 性能测试 (检查执行时间)
    log_info "运行性能测试..."
    start_time=$(date +%s)
    python3 $PYTHON_ANALYZER -c system performance -o perf_test > /dev/null 2>&1
    end_time=$(date +%s)
    execution_time=$((end_time - start_time))
    
    if [ $execution_time -lt 30 ]; then
        log_info "✓ 性能测试通过 (执行时间: ${execution_time}秒)"
        PASSED_TESTS=$((PASSED_TESTS + 1))
    else
        log_error "✗ 性能测试失败 (执行时间过长: ${execution_time}秒)"
        FAILED_TESTS=$((FAILED_TESTS + 1))
    fi
    
    # 测试结果统计
    echo "" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    echo "测试完成时间: $(date)" | tee -a "$TEST_LOG"
    echo "" | tee -a "$TEST_LOG"
    echo "测试结果统计:" | tee -a "$TEST_LOG"
    echo "  总测试数: $TOTAL_TESTS" | tee -a "$TEST_LOG"
    echo "  通过: $PASSED_TESTS" | tee -a "$TEST_LOG"
    echo "  失败: $FAILED_TESTS" | tee -a "$TEST_LOG"
    echo "  通过率: $(echo "scale=1; $PASSED_TESTS * 100 / $TOTAL_TESTS" | bc -l 2>/dev/null || echo "N/A")%" | tee -a "$TEST_LOG"
    echo "=========================================" | tee -a "$TEST_LOG"
    
    # 清理
    cleanup
    
    # 返回结果
    if [ $FAILED_TESTS -eq 0 ]; then
        log_info "所有测试通过!"
        exit 0
    else
        log_error "部分测试失败!"
        exit 1
    fi
}

# 检查依赖
log_info "检查测试依赖..."
if ! command -v python3 &> /dev/null; then
    log_error "Python3 未安装"
    exit 1
fi

if ! command -v bc &> /dev/null; then
    log_warning "bc 计算器未安装,无法显示精确通过率"
fi

# 运行主函数
main