-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.sh
More file actions
executable file
·905 lines (784 loc) · 34.8 KB
/
Copy pathbench.sh
File metadata and controls
executable file
·905 lines (784 loc) · 34.8 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
#!/bin/bash
# 清理临时文件的函数
cleanup_temp_files() {
echo "正在清理临时文件..."
rm -f temp_file/test_results/*_test_output.s # 汇编文件
rm -f temp_file/test_results/*_test_output # 可执行文件
rm -f temp_file/test_results/*.actual # 实际输出文件
rm -f temp_file/test_results/*.actual.norm # 标准化实际输出文件
rm -f temp_file/test_results/*.expected.norm # 标准化期望输出文件
rm -f temp_file/test_results/*_stderr.tmp # stderr临时文件
echo "临时文件清理完成"
}
# 中断计数器,用于强制退出机制
interrupt_count=0
# 当前运行进程的PID,用于中断时终止
current_pid=""
# 改进的信号处理器,支持强制退出
cleanup_and_exit() {
interrupt_count=$((interrupt_count + 1))
if [ $interrupt_count -eq 1 ]; then
echo ""
echo "收到中断信号 (Ctrl+C),正在清理并退出..."
echo "如需强制退出,请再次按 Ctrl+C"
# 尝试杀死当前运行的子进程
if [ -n "$current_pid" ]; then
echo "正在终止当前进程 (PID: $current_pid)..."
kill -TERM "$current_pid" 2>/dev/null || true
sleep 2
kill -KILL "$current_pid" 2>/dev/null || true
fi
cleanup_temp_files
exit 130 # 标准的Ctrl+C退出码
else
echo ""
echo "收到第二次中断信号,强制退出!"
# 强制杀死所有相关进程
pkill -f "compiler.*\.sy" 2>/dev/null || true
pkill -f "qemu-aarch64" 2>/dev/null || true
pkill -f "aarch64-linux-gnu-gcc" 2>/dev/null || true
cleanup_temp_files
exit 130
fi
}
# 设置信号处理器
trap cleanup_and_exit INT TERM
trap cleanup_temp_files EXIT
# 显示帮助信息
show_help() {
echo "用法: $0 [选项]"
echo ""
echo "选项:"
echo " -f, --functional-only 只测试功能测试用例 (testdata/functional 和 testdata/h_functional)"
echo " -p, --performance 只测试性能测试用例 (testdata/performance)"
echo " -g, --gcc 使用GCC编译性能测试用例并统计时长 (首次运行时需要)"
echo " -h, --help 显示此帮助信息"
echo ""
echo "测试模式说明:"
echo " 无选项: 功能测试用例(仅判断对错) + 性能测试用例(判断对错+统计时长)"
echo " -f: 功能测试用例(仅判断对错)"
echo " -p: 性能测试用例(判断对错+统计时长)"
echo " -g: 性能测试用例(用GCC编译并统计时长,用于建立基准)"
echo ""
echo "示例:"
echo " $0 -g # 首次运行:建立GCC性能基准"
echo " $0 -p # 测试我们编译器的性能"
echo " $0 -f # 只测试功能用例"
echo " $0 # 测试所有用例"
}
# 解析命令行参数
FUNCTIONAL_ONLY=false
PERFORMANCE_ONLY=false
GCC_ONLY=false
while [[ $# -gt 0 ]]; do
case $1 in
-f|--functional-only)
FUNCTIONAL_ONLY=true
shift
;;
-p|--performance)
PERFORMANCE_ONLY=true
shift
;;
-g|--gcc)
GCC_ONLY=true
shift
;;
-h|--help)
show_help
exit 0
;;
*)
echo "未知选项: $1"
echo "使用 -h 或 --help 查看帮助信息"
exit 1
;;
esac
done
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 符号定义
CHECK_MARK='\u2714'
CROSS_MARK='\u2718'
# 设置总结日志文件 - 按时间命名
TIMESTAMP=$(date '+%Y%m%d_%H%M%S')
if [ "$PERFORMANCE_ONLY" = true ]; then
SUMMARY_LOG="temp_file/test_results/performance_summary_${TIMESTAMP}.log"
elif [ "$GCC_ONLY" = true ]; then
SUMMARY_LOG="temp_file/test_results/gcc_benchmark_${TIMESTAMP}.log"
else
SUMMARY_LOG="temp_file/test_results/summary_${TIMESTAMP}.log"
fi
# 确保测试结果目录存在
mkdir -p temp_file/test_results
# 清理之前可能残留的临时文件
cleanup_temp_files
# GCC缓存文件路径
GCC_CACHE_FILE="temp_file/test_results/gcc_performance_cache.txt"
# 向终端输出带颜色的文本,向日志文件输出无颜色文本
log_echo() {
# 终端输出(带颜色)
echo -e "$1"
# 创建无颜色的版本写入日志文件
local log_content="$1"
# 直接用纯文本版本替代有颜色版本,而不是用sed替换
case "$log_content" in
*"${GREEN}${CHECK_MARK}"*)
log_content="[PASS]${log_content#*${CHECK_MARK}}"
log_content=${log_content//${GREEN}/}
log_content=${log_content//${NC}/}
;;
*"${RED}${CROSS_MARK}"*)
log_content="[FAIL]${log_content#*${CROSS_MARK}}"
log_content=${log_content//${RED}/}
log_content=${log_content//${NC}/}
;;
*"${YELLOW}${CROSS_MARK}"*)
log_content="[SKIP]${log_content#*${CROSS_MARK}}"
log_content=${log_content//${YELLOW}/}
log_content=${log_content//${NC}/}
;;
*"${BLUE}"*)
log_content=${log_content//${BLUE}/}
log_content=${log_content//${NC}/}
;;
esac
echo "$log_content" >> "$SUMMARY_LOG"
}
# 写入日志文件标题
echo "=============================================" >> "$SUMMARY_LOG"
if [ "$PERFORMANCE_ONLY" = true ]; then
echo " SysY编译器性能测试结果摘要" >> "$SUMMARY_LOG"
elif [ "$GCC_ONLY" = true ]; then
echo " GCC性能基准测试结果摘要" >> "$SUMMARY_LOG"
else
echo " SysY编译器测试结果摘要" >> "$SUMMARY_LOG"
fi
echo "=============================================" >> "$SUMMARY_LOG"
echo "运行日期: $(date)" >> "$SUMMARY_LOG"
if [ "$FUNCTIONAL_ONLY" = true ]; then
echo "测试模式: 仅功能测试用例" >> "$SUMMARY_LOG"
elif [ "$PERFORMANCE_ONLY" = true ]; then
echo "测试模式: 仅性能测试用例" >> "$SUMMARY_LOG"
elif [ "$GCC_ONLY" = true ]; then
echo "测试模式: GCC性能基准测试" >> "$SUMMARY_LOG"
else
echo "测试模式: 全部测试用例" >> "$SUMMARY_LOG"
fi
echo "=============================================" >> "$SUMMARY_LOG"
echo "" >> "$SUMMARY_LOG"
if [ "$FUNCTIONAL_ONLY" = true ]; then
log_echo "${BLUE}====== 开始测试功能测试用例 (functional & h_functional) ======${NC}"
elif [ "$PERFORMANCE_ONLY" = true ]; then
log_echo "${BLUE}====== 开始测试性能测试用例 (performance) ======${NC}"
elif [ "$GCC_ONLY" = true ]; then
log_echo "${BLUE}====== 开始GCC性能基准测试 (performance) ======${NC}"
else
log_echo "${BLUE}====== 开始测试 testdata 中的所有 .sy 文件 ======${NC}"
fi
# 记录开始时间
start_time=$(date +%s)
# 统计变量
total=0
passed=0
failed=0
skipped=0
timeout_count=0
# 性能测试专用统计变量
total_our_time=0
total_gcc_time=0
performance_comparisons=0
total_performance_score=0
# 根据测试模式设置预期的总测试用例数
if [ "$FUNCTIONAL_ONLY" = true ]; then
total_expected=140 # 功能测试用例的预期数量 (functional + h_functional)
elif [ "$PERFORMANCE_ONLY" = true ] || [ "$GCC_ONLY" = true ]; then
total_expected=59 # 性能测试用例的预期数量 (performance)
else
total_expected=199 # 全部测试用例的预期数量
fi
# 超时设置(秒)
TIMEOUT_SECONDS=30
# 检查目录是否应该被测试
should_test_directory() {
local dir="$1"
if [ "$FUNCTIONAL_ONLY" = true ]; then
# 仅功能测试模式:只测试 functional 和 h_functional 目录
case "$dir" in
*"/functional"* | *"/h_functional"*)
return 0
;;
*)
return 1
;;
esac
elif [ "$PERFORMANCE_ONLY" = true ] || [ "$GCC_ONLY" = true ]; then
# 仅性能测试模式或GCC基准测试模式:只测试 performance 目录
case "$dir" in
*"/performance"*)
return 0
;;
*)
return 1
;;
esac
else
# 如果不是特定测试模式,测试所有目录
return 0
fi
}
# 获取或缓存GCC编译的性能数据
get_gcc_performance() {
local syfile="$1"
local infile="$2"
local basename="$3"
# 检查缓存文件中是否已有该测试用例的数据
if [ -f "$GCC_CACHE_FILE" ] && grep -q "^$basename:" "$GCC_CACHE_FILE"; then
# 从缓存中读取时间(如果有多个条目,取最后一个)
local cached_time=$(grep "^$basename:" "$GCC_CACHE_FILE" | tail -1 | cut -d: -f2)
echo "$cached_time"
return 0
fi
# 缓存中没有,需要编译并测试
local gcc_temp_dir=$(mktemp -d)
local gcc_output="$gcc_temp_dir/gcc_output"
local temp_c_file="$gcc_temp_dir/temp.c"
# 创建临时C文件,添加必要的函数声明和修复语法
{
echo '#include <stdio.h>'
echo '#include <sys/time.h>'
echo 'int getint();'
echo 'float getfloat();'
echo 'void putint(int a);'
echo 'void putfloat(float a);'
echo 'void putch(int a);'
echo 'void _sysy_starttime(int lineno);'
echo 'void _sysy_stoptime(int lineno);'
echo '#define starttime() _sysy_starttime(__LINE__)'
echo '#define stoptime() _sysy_stoptime(__LINE__)'
echo ''
# 将const声明转换为#define(支持等号前后的空格)
sed -e 's/const int \([A-Za-z_][A-Za-z0-9_]*\) *=\s*\([0-9]*\);/#define \1 \2/' \
-e 's/const float \([A-Za-z_][A-Za-z0-9_]*\) *=\s*\([0-9.]*\);/#define \1 \2/' "$syfile"
} > "$temp_c_file"
# 先编译C++运行时
# 使用ARM交叉编译器编译
if ! aarch64-linux-gnu-g++ -O2 -static -I./sylib -o "$gcc_output" "$temp_c_file" ./sylib/sylib_arm.o 2>/dev/null; then
rm -rf "$gcc_temp_dir"
echo "ERROR"
return 1
fi
# 测试运行时间(运行3次取平均值)
local total_time=0
local valid_runs=0
for i in {1..3}; do
local start_time=$(date +%s.%N)
if [ -f "$infile" ]; then
# 有输入文件
timeout 300 qemu-aarch64-static "$gcc_output" < "$infile" >/dev/null 2>&1 &
current_pid=$!
if wait $current_pid; then
local end_time=$(date +%s.%N)
local run_time=$(echo "$end_time - $start_time" | bc -l)
total_time=$(echo "$total_time + $run_time" | bc -l)
valid_runs=$((valid_runs + 1))
fi
current_pid=""
else
# 无输入文件
timeout 300 qemu-aarch64-static "$gcc_output" >/dev/null 2>&1 &
current_pid=$!
if wait $current_pid; then
local end_time=$(date +%s.%N)
local run_time=$(echo "$end_time - $start_time" | bc -l)
total_time=$(echo "$total_time + $run_time" | bc -l)
valid_runs=$((valid_runs + 1))
fi
current_pid=""
fi
done
rm -rf "$gcc_temp_dir"
if [ $valid_runs -eq 0 ]; then
echo "ERROR"
return 1
fi
# 计算平均时间
local avg_time=$(echo "scale=6; $total_time / $valid_runs" | bc -l)
# 缓存结果
echo "$basename:$avg_time" >> "$GCC_CACHE_FILE"
echo "$avg_time"
return 0
}
# GCC基准测试专用函数
run_gcc_benchmark() {
local syfile="$1"
local infile="$2"
local basename="$3"
local gcc_temp_dir=$(mktemp -d)
local gcc_output="$gcc_temp_dir/gcc_output"
local temp_c_file="$gcc_temp_dir/temp.c"
# 创建临时C文件,添加必要的函数声明和修复语法
{
echo '#include <stdio.h>'
echo '#include <sys/time.h>'
echo 'int getint();'
echo 'float getfloat();'
echo 'void putint(int a);'
echo 'void putfloat(float a);'
echo 'void putch(int a);'
echo 'void _sysy_starttime(int lineno);'
echo 'void _sysy_stoptime(int lineno);'
echo '#define starttime() _sysy_starttime(__LINE__)'
echo '#define stoptime() _sysy_stoptime(__LINE__)'
echo ''
# 将const声明转换为#define(支持等号前后的空格)
sed -e 's/const int \([A-Za-z_][A-Za-z0-9_]*\) *=\s*\([0-9]*\);/#define \1 \2/' \
-e 's/const float \([A-Za-z_][A-Za-z0-9_]*\) *=\s*\([0-9.]*\);/#define \1 \2/' "$syfile"
} > "$temp_c_file"
# 使用ARM交叉编译器编译
if ! aarch64-linux-gnu-g++ -O2 -static -march=armv8-a -I./sylib -o "$gcc_output" "$temp_c_file" ./sylib/sylib_arm.o 2>/dev/null; then
rm -rf "$gcc_temp_dir"
echo "COMPILE_ERROR"
return 1
fi
# 测试运行时间(运行3次取平均值)
local total_time=0
local valid_runs=0
for i in {1..3}; do
local start_time=$(date +%s.%N)
if [ -f "$infile" ]; then
# 有输入文件
timeout 300 qemu-aarch64 "$gcc_output" < "$infile" >/dev/null 2>&1 &
current_pid=$!
if wait $current_pid; then
local end_time=$(date +%s.%N)
local run_time=$(echo "$end_time - $start_time" | bc -l)
total_time=$(echo "$total_time + $run_time" | bc -l)
valid_runs=$((valid_runs + 1))
fi
current_pid=""
else
# 无输入文件
timeout 300 qemu-aarch64 "$gcc_output" >/dev/null 2>&1 &
current_pid=$!
if wait $current_pid; then
local end_time=$(date +%s.%N)
local run_time=$(echo "$end_time - $start_time" | bc -l)
total_time=$(echo "$total_time + $run_time" | bc -l)
valid_runs=$((valid_runs + 1))
fi
current_pid=""
fi
done
rm -rf "$gcc_temp_dir"
if [ $valid_runs -eq 0 ]; then
echo "EXEC_ERROR"
return 1
fi
# 计算平均时间
local avg_time=$(echo "scale=6; $total_time / $valid_runs" | bc -l)
# 保存到缓存文件
echo "$basename:$avg_time" >> "$GCC_CACHE_FILE"
echo "$avg_time"
return 0
}
# 递归查找所有.sy文件的函数
find_and_test_syfiles() {
local dir="$1"
# 检查当前目录是否应该被测试
if ! should_test_directory "$dir"; then
# 仍然需要递归检查子目录,因为子目录可能包含functional或h_functional
for subdir in "$dir"/*/; do
if [ -d "$subdir" ]; then
find_and_test_syfiles "$subdir"
fi
done
return
fi
# 遍历目录下的所有.sy文件
for syfile in "$dir"/*.sy; do
# 确保文件存在(防止通配符未匹配任何文件)
if [ ! -f "$syfile" ]; then
continue
fi
# 获取文件名(不含路径和扩展名)
basename=$(basename "$syfile" .sy)
dirname=$(dirname "$syfile")
outfile="${dirname}/${basename}.out"
infile="${dirname}/${basename}.in"
# 检查对应的.out文件是否存在
if [ ! -f "$outfile" ]; then
total=$((total + 1))
progress="[$total/$total_expected]"
log_echo "${YELLOW}${CROSS_MARK} $progress $basename\t\t\t(找不到输出文件)${NC}"
skipped=$((skipped + 1))
continue
fi
total=$((total + 1))
# 显示进度
progress="[$total/$total_expected]"
# GCC基准测试模式:跳过我们编译器的编译,直接用GCC
if [ "$GCC_ONLY" = true ]; then
# GCC基准测试模式:直接运行GCC基准测试
local gcc_result
if [ -f "$infile" ]; then
gcc_result=$(run_gcc_benchmark "$syfile" "$infile" "$basename")
else
gcc_result=$(run_gcc_benchmark "$syfile" "" "$basename")
fi
local gcc_exit_code=$?
case "$gcc_result" in
"COMPILE_ERROR")
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(GCC编译失败)${NC}"
failed=$((failed + 1))
continue
;;
"EXEC_ERROR")
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(GCC执行失败)${NC}"
failed=$((failed + 1))
continue
;;
esac
if [ $gcc_exit_code -eq 0 ]; then
log_echo "${GREEN}${CHECK_MARK} $progress $basename\t\t\t(GCC基准时间: ${gcc_result}s)${NC}"
passed=$((passed + 1))
total_gcc_time=$(echo "$total_gcc_time + $gcc_result" | bc -l)
performance_comparisons=$((performance_comparisons + 1))
else
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(GCC基准测试失败)${NC}"
failed=$((failed + 1))
fi
continue
fi
# 非GCC基准测试模式:编译我们的编译器 - 使用唯一文件名避免冲突
# 启动编译器进程并记录PID
timeout $TIMEOUT_SECONDS ./build/bin/compiler "$syfile" --enable-all-opt --no-output-dir -o temp_file/test_results/${basename}_test_output.s > /dev/null 2>&1 &
current_pid=$!
wait $current_pid
compile_exit_code=$?
current_pid="" # 清除PID记录
if [ $compile_exit_code -eq 124 ]; then
# timeout命令返回124表示超时
log_echo "${YELLOW}${CROSS_MARK} $progress $basename\t\t\t(编译超时 > ${TIMEOUT_SECONDS}s)${NC}"
timeout_count=$((timeout_count + 1))
failed=$((failed + 1))
continue
elif [ $compile_exit_code -eq 139 ]; then
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(编译段错误)${NC}"
failed=$((failed + 1))
continue
elif [ $compile_exit_code -ne 0 ]; then
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(编译失败)${NC}"
failed=$((failed + 1))
continue
fi
# 判断是否需要进行性能测试(性能测试模式 或 默认模式下的性能测试用例)
local is_performance_test=false
if [ "$PERFORMANCE_ONLY" = true ]; then
is_performance_test=true
elif [ "$FUNCTIONAL_ONLY" = false ] && [[ "$dirname" == *"/performance"* ]]; then
# 默认模式下的性能测试用例
is_performance_test=true
fi
if [ "$is_performance_test" = true ]; then
# 性能测试:完全按照run.sh的逻辑
# 再进行链接步骤
if ! aarch64-linux-gnu-g++ \
-static \
-march=armv8-a \
-I./sylib \
-o "temp_file/test_results/${basename}_test_output" \
"temp_file/test_results/${basename}_test_output.s" \
./sylib/sylib_arm.o \
2>/dev/null; then
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(链接失败)${NC}"
failed=$((failed + 1))
continue
fi
# 执行测试并测量时间 - 直接复制run.sh的逻辑
local actual_output_file="temp_file/test_results/${basename}.actual"
local start_time=$(date +%s.%N)
if [ -f "$infile" ]; then
# 如果有输入文件,使用输入文件 - 将stderr重定向到临时文件以分离TOTAL输出
qemu-aarch64-static "temp_file/test_results/${basename}_test_output" < "$infile" > "$actual_output_file" 2>"temp_file/test_results/${basename}_stderr.tmp" &
current_pid=$!
wait $current_pid
else
# 没有输入文件,直接运行 - 将stderr重定向到临时文件以分离TOTAL输出
qemu-aarch64-static "temp_file/test_results/${basename}_test_output" > "$actual_output_file" 2>"temp_file/test_results/${basename}_stderr.tmp" &
current_pid=$!
wait $current_pid
fi
local return_code=$?
current_pid="" # 清除PID记录
local end_time=$(date +%s.%N)
local our_time=$(echo "$end_time - $start_time" | bc -l)
# 将返回码追加到输出文件 - 按照run.sh的方式
echo "$return_code" >> "$actual_output_file"
# 结果对比 - 完全按照run.sh的逻辑
local actual_normalized="temp_file/test_results/${basename}.actual.norm"
local expected_normalized="temp_file/test_results/${basename}.expected.norm"
# 标准化处理:去除行尾空白,统一换行符
sed 's/[[:space:]]*$//' "$actual_output_file" | tr -d '\r' > "$actual_normalized"
sed 's/[[:space:]]*$//' "$outfile" | tr -d '\r' > "$expected_normalized"
# 使用项目中的 cmp.py 进行对比(注意:cmp.py返回1表示相同,0表示不同)
python3 ./temp_file/shells/cmp.py --f1 "$actual_normalized" --f2 "$expected_normalized" >/dev/null 2>&1
local comparison_result=$?
if [ $comparison_result -eq 1 ]; then
# 测试通过,进行性能分析
# 获取GCC的性能数据
local gcc_time
if [ -f "$infile" ]; then
gcc_time=$(get_gcc_performance "$syfile" "$infile" "$basename")
else
gcc_time=$(get_gcc_performance "$syfile" "" "$basename")
fi
if [ "$gcc_time" = "ERROR" ]; then
log_echo "${YELLOW}${CROSS_MARK} $progress $basename\t\t\t(GCC编译失败,无法比较性能)${NC}"
# 虽然GCC失败,但我们的编译器输出正确,仍算通过
log_echo "${GREEN}${CHECK_MARK} $progress $basename\t\t\t(输出正确,用时: ${our_time}s)${NC}"
passed=$((passed + 1))
else
# 计算性能得分:100 / (我们的时间 / GCC时间)
local performance_score=$(echo "scale=2; 100 / ($our_time / $gcc_time)" | bc -l)
# 累加时间统计和得分
total_our_time=$(echo "$total_our_time + $our_time" | bc -l)
total_gcc_time=$(echo "$total_gcc_time + $gcc_time" | bc -l)
total_performance_score=$(echo "$total_performance_score + $performance_score" | bc -l)
performance_comparisons=$((performance_comparisons + 1))
# 输出结果
log_echo "${GREEN}${CHECK_MARK} $progress $basename\t\t\t(我们: ${our_time}s, GCC: ${gcc_time}s, 得分: ${performance_score})${NC}"
passed=$((passed + 1))
fi
else
# 输出不匹配
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(输出不匹配)${NC}"
failed=$((failed + 1))
fi
rm -f "$actual_output_file" "$actual_normalized" "$expected_normalized"
else
# 普通测试模式:只检查输出正确性 - 使用与性能测试相同的逻辑
# 再进行链接步骤
if ! aarch64-linux-gnu-g++ \
-static \
-march=armv8-a \
-I./sylib \
-o "temp_file/test_results/${basename}_test_output" \
"temp_file/test_results/${basename}_test_output.s" \
./sylib/sylib_arm.o \
2>/dev/null; then
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(链接失败)${NC}"
failed=$((failed + 1))
continue
fi
# 执行测试
local actual_output_file="temp_file/test_results/${basename}.actual"
if [ -f "$infile" ]; then
# 如果有输入文件,使用输入文件 - 将stderr重定向到临时文件以分离TOTAL输出
qemu-aarch64-static "temp_file/test_results/${basename}_test_output" < "$infile" > "$actual_output_file" 2>"temp_file/test_results/${basename}_stderr.tmp" &
current_pid=$!
wait $current_pid
else
# 没有输入文件,直接运行 - 将stderr重定向到临时文件以分离TOTAL输出
qemu-aarch64-static "temp_file/test_results/${basename}_test_output" > "$actual_output_file" 2>"temp_file/test_results/${basename}_stderr.tmp" &
current_pid=$!
wait $current_pid
fi
local return_code=$?
current_pid="" # 清除PID记录
# 将返回码追加到输出文件 - 按照run.sh的方式
echo "$return_code" >> "$actual_output_file"
# 结果对比
local actual_normalized="temp_file/test_results/${basename}.actual.norm"
local expected_normalized="temp_file/test_results/${basename}.expected.norm"
# 标准化处理:去除行尾空白,统一换行符
sed 's/[[:space:]]*$//' "$actual_output_file" | tr -d '\r' > "$actual_normalized"
sed 's/[[:space:]]*$//' "$outfile" | tr -d '\r' > "$expected_normalized"
# 使用项目中的 cmp.py 进行对比(注意:cmp.py返回1表示相同,0表示不同)
python3 ./temp_file/shells/cmp.py --f1 "$actual_normalized" --f2 "$expected_normalized" >/dev/null 2>&1
local comparison_result=$?
if [ $comparison_result -eq 1 ]; then
log_echo "${GREEN}${CHECK_MARK} $progress $basename${NC}"
passed=$((passed + 1))
else
log_echo "${RED}${CROSS_MARK} $progress $basename\t\t\t(执行结果不匹配)${NC}"
failed=$((failed + 1))
fi
fi
done
# 递归处理子目录
for subdir in "$dir"/*/; do
if [ -d "$subdir" ]; then
find_and_test_syfiles "$subdir"
fi
done
}
# 开始递归查找和测试
find_and_test_syfiles "testdata"
# 清理临时文件将由EXIT trap自动处理
# 计算总耗时
end_time=$(date +%s)
total_duration=$((end_time - start_time))
minutes=$((total_duration / 60))
seconds=$((total_duration % 60))
# 计算分数(仅在功能测试模式下)
if [ "$FUNCTIONAL_ONLY" = true ]; then
# 功能测试分数计算:通过数量/140*100,取整数
score=$((passed * 100 / 140))
fi
# 输出总结
log_echo "${BLUE}====== 测试完成 ======${NC}"
log_echo "总共测试: $total"
log_echo "${GREEN}通过: $passed${NC}"
log_echo "${RED}失败: $failed${NC}"
log_echo "${YELLOW}跳过: $skipped${NC}"
if [ $timeout_count -gt 0 ]; then
log_echo "${YELLOW}超时: $timeout_count (超过 ${TIMEOUT_SECONDS}s)${NC}"
fi
# 显示分数(仅在功能测试模式下)
if [ "$FUNCTIONAL_ONLY" = true ]; then
log_echo "${GREEN}功能测试得分: $score/100 分${NC}"
fi
# 显示性能统计
if [ "$GCC_ONLY" = true ] && [ $performance_comparisons -gt 0 ]; then
# GCC基准测试模式
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
log_echo "${BLUE}====== GCC基准测试统计 ======${NC}"
log_echo "GCC基准测试用例数: $performance_comparisons"
log_echo "GCC编译器平均用时: ${avg_gcc_time}s"
log_echo "GCC总执行时间: ${total_gcc_time}s"
elif [ "$PERFORMANCE_ONLY" = true ] && [ $performance_comparisons -gt 0 ]; then
# 性能测试模式
avg_our_time=$(echo "scale=6; $total_our_time / $performance_comparisons" | bc -l)
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
avg_performance_score=$(echo "scale=2; $total_performance_score / $performance_comparisons" | bc -l)
log_echo "${BLUE}====== 性能统计 ======${NC}"
log_echo "性能比较用例数: $performance_comparisons"
log_echo "我们编译器平均用时: ${avg_our_time}s"
log_echo "GCC编译器平均用时: ${avg_gcc_time}s"
log_echo "${GREEN}性能总得分: ${avg_performance_score}${NC}"
log_echo "总执行时间 - 我们: ${total_our_time}s, GCC: ${total_gcc_time}s"
elif [ "$FUNCTIONAL_ONLY" = false ] && [ $performance_comparisons -gt 0 ]; then
# 默认模式下的性能统计
avg_our_time=$(echo "scale=6; $total_our_time / $performance_comparisons" | bc -l)
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
avg_performance_score=$(echo "scale=2; $total_performance_score / $performance_comparisons" | bc -l)
log_echo "${BLUE}====== 性能测试统计 ======${NC}"
log_echo "性能比较用例数: $performance_comparisons"
log_echo "我们编译器平均用时: ${avg_our_time}s"
log_echo "GCC编译器平均用时: ${avg_gcc_time}s"
log_echo "${GREEN}性能总得分: ${avg_performance_score}${NC}"
log_echo "总执行时间 - 我们: ${total_our_time}s, GCC: ${total_gcc_time}s"
fi
if [ $minutes -gt 0 ]; then
log_echo "${BLUE}总耗时: ${minutes}分${seconds}秒${NC}"
else
log_echo "${BLUE}总耗时: ${seconds}秒${NC}"
fi
log_echo "测试摘要保存在: $SUMMARY_LOG"
# 添加漂亮的摘要信息到日志文件
echo "" >> "$SUMMARY_LOG"
echo "=============================================" >> "$SUMMARY_LOG"
echo "测试统计:" >> "$SUMMARY_LOG"
echo "--------------------------------------------" >> "$SUMMARY_LOG"
echo "总共测试: $total 个测试用例" >> "$SUMMARY_LOG"
echo "通过: $passed" >> "$SUMMARY_LOG"
echo "失败: $failed" >> "$SUMMARY_LOG"
echo "跳过: $skipped" >> "$SUMMARY_LOG"
if [ $timeout_count -gt 0 ]; then
echo "超时: $timeout_count (超过 ${TIMEOUT_SECONDS}s)" >> "$SUMMARY_LOG"
fi
# 在日志中记录分数(仅在功能测试模式下)
if [ "$FUNCTIONAL_ONLY" = true ]; then
echo "功能测试得分: $score/100 分" >> "$SUMMARY_LOG"
fi
# 在日志中记录性能统计
if [ "$GCC_ONLY" = true ] && [ $performance_comparisons -gt 0 ]; then
# GCC基准测试统计
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
echo "--------------------------------------------" >> "$SUMMARY_LOG"
echo "GCC基准测试统计:" >> "$SUMMARY_LOG"
echo "GCC基准测试用例数: $performance_comparisons" >> "$SUMMARY_LOG"
echo "GCC编译器平均用时: ${avg_gcc_time}s" >> "$SUMMARY_LOG"
echo "GCC总执行时间: ${total_gcc_time}s" >> "$SUMMARY_LOG"
elif [ "$PERFORMANCE_ONLY" = true ] && [ $performance_comparisons -gt 0 ]; then
# 性能测试统计
avg_our_time=$(echo "scale=6; $total_our_time / $performance_comparisons" | bc -l)
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
avg_performance_score=$(echo "scale=2; $total_performance_score / $performance_comparisons" | bc -l)
echo "--------------------------------------------" >> "$SUMMARY_LOG"
echo "性能统计:" >> "$SUMMARY_LOG"
echo "性能比较用例数: $performance_comparisons" >> "$SUMMARY_LOG"
echo "我们编译器平均用时: ${avg_our_time}s" >> "$SUMMARY_LOG"
echo "GCC编译器平均用时: ${avg_gcc_time}s" >> "$SUMMARY_LOG"
echo "性能总得分: ${avg_performance_score}" >> "$SUMMARY_LOG"
echo "总执行时间 - 我们: ${total_our_time}s, GCC: ${total_gcc_time}s" >> "$SUMMARY_LOG"
elif [ "$FUNCTIONAL_ONLY" = false ] && [ $performance_comparisons -gt 0 ]; then
# 默认模式下的性能统计
avg_our_time=$(echo "scale=6; $total_our_time / $performance_comparisons" | bc -l)
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
avg_performance_score=$(echo "scale=2; $total_performance_score / $performance_comparisons" | bc -l)
echo "--------------------------------------------" >> "$SUMMARY_LOG"
echo "性能测试统计:" >> "$SUMMARY_LOG"
echo "性能比较用例数: $performance_comparisons" >> "$SUMMARY_LOG"
echo "我们编译器平均用时: ${avg_our_time}s" >> "$SUMMARY_LOG"
echo "GCC编译器平均用时: ${avg_gcc_time}s" >> "$SUMMARY_LOG"
echo "性能总得分: ${avg_performance_score}" >> "$SUMMARY_LOG"
echo "总执行时间 - 我们: ${total_our_time}s, GCC: ${total_gcc_time}s" >> "$SUMMARY_LOG"
fi
if [ $minutes -gt 0 ]; then
echo "总耗时: ${minutes}分${seconds}秒" >> "$SUMMARY_LOG"
else
echo "总耗时: ${seconds}秒" >> "$SUMMARY_LOG"
fi
echo "=============================================" >> "$SUMMARY_LOG"
echo "" >> "$SUMMARY_LOG"
echo "测试完成时间: $(date)" >> "$SUMMARY_LOG"
# 保存测试统计信息到CSV文件
if [ "$PERFORMANCE_ONLY" = true ]; then
CSV_FILE="temp_file/test_results/performance_statistics.csv"
elif [ "$GCC_ONLY" = true ]; then
CSV_FILE="temp_file/test_results/gcc_benchmark_statistics.csv"
else
CSV_FILE="temp_file/test_results/test_statistics.csv"
fi
CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')
# 如果CSV文件不存在,创建并添加表头
if [ ! -f "$CSV_FILE" ]; then
if [ "$FUNCTIONAL_ONLY" = true ]; then
echo "测试时间,总共测试,通过,失败,跳过,超时,功能测试得分" > "$CSV_FILE"
elif [ "$PERFORMANCE_ONLY" = true ]; then
echo "测试时间,总共测试,通过,失败,跳过,超时,性能比较用例数,我们平均用时,GCC平均用时,性能总得分" > "$CSV_FILE"
elif [ "$GCC_ONLY" = true ]; then
echo "测试时间,总共测试,通过,失败,跳过,超时,GCC基准用例数,GCC平均用时" > "$CSV_FILE"
else
echo "测试时间,总共测试,通过,失败,跳过,超时" > "$CSV_FILE"
fi
fi
# 添加当前测试结果到CSV文件
if [ "$FUNCTIONAL_ONLY" = true ]; then
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count,$score" >> "$CSV_FILE"
elif [ "$PERFORMANCE_ONLY" = true ]; then
if [ $performance_comparisons -gt 0 ]; then
avg_our_time=$(echo "scale=6; $total_our_time / $performance_comparisons" | bc -l)
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
avg_performance_score=$(echo "scale=2; $total_performance_score / $performance_comparisons" | bc -l)
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count,$performance_comparisons,$avg_our_time,$avg_gcc_time,$avg_performance_score" >> "$CSV_FILE"
else
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count,0,0,0,0" >> "$CSV_FILE"
fi
elif [ "$GCC_ONLY" = true ]; then
if [ $performance_comparisons -gt 0 ]; then
avg_gcc_time=$(echo "scale=6; $total_gcc_time / $performance_comparisons" | bc -l)
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count,$performance_comparisons,$avg_gcc_time" >> "$CSV_FILE"
else
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count,0,0" >> "$CSV_FILE"
fi
else
echo "$CURRENT_TIME,$total,$passed,$failed,$skipped,$timeout_count" >> "$CSV_FILE"
fi
log_echo "${BLUE}测试统计已保存到: $CSV_FILE${NC}"