#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
ASCEND_HOME_DIR="${ASCEND_HOME_DIR:-/usr/local/Ascend/ascend-toolkit/latest}"
if [ ! -f "${ASCEND_HOME_DIR}/set_env.sh" ]; then
echo "Error: Ascend environment file not found: ${ASCEND_HOME_DIR}/set_env.sh"
exit 1
fi
source "${ASCEND_HOME_DIR}/set_env.sh"
TOOLS_PATH="${PROJECT_ROOT}/examples"
LOG_PATH="${PROJECT_ROOT}/tmplog_tools"
if [ ! -d "${TOOLS_PATH}" ]; then
echo "Error: Examples directory not found: ${TOOLS_PATH}"
exit 1
fi
EXAMPLE_LIST=(
01_show_kernel_debug_data
02_cpudebug
)
format_duration() {
local seconds=$1
if [ "${seconds}" -eq 0 ]; then
echo "0s"
return
fi
echo "${seconds}" | awk '{
t=split("60 s 60 m 24 h 999 d", a)
for(n=1; n<t; n+=2) {
if($1==0) break
s=$1%a[n] a[n+1] s
$1=int($1/a[n])
}
print s
}'
}
run_example1() {
local base_path=$1
local example_name=$2
cd "${base_path}/${example_name}/" || return 1
rm -rf build
cmake -B build -DCMAKE_ASC_RUN_MODE=cpu -DCMAKE_ASC_ARCHITECTURES=dav-2201 || return 1
cmake --build build || return 1
./build/add || return 1
}
run_example2() {
local base_path=$1
local example_name=$2
cd "${base_path}/${example_name}/" || return 1
rm -rf build
mkdir -p build && cd build || return 1
cmake .. || return 1
make -j || return 1
./demo || return 1
}
run_test_case() {
local tools_path=$1
local example_name=$2
local log_path=$3
local case_name="${example_name}"
local start_time end_time duration elapsed
start_time=$(date +%s)
echo ">>>>>>>>>>>>>>>>>>>>> $(date '+%Y-%m-%d %H:%M:%S') run ${case_name} start! <<<<<<<<<<<<<<<<<<<<<"
if [ "${case_name}" == "01_show_kernel_debug_data" ]; then
run_example2 "${tools_path}" "${example_name}" 2>&1 | tee "${log_path}/${case_name}.log"
else
run_example1 "${tools_path}" "${example_name}" 2>&1 | tee "${log_path}/${case_name}.log"
fi
local test_result=${PIPESTATUS[0]}
end_time=$(date +%s)
duration=$((end_time - start_time))
elapsed=$(format_duration "${duration}")
echo "test case ${case_name} duration: ${elapsed}"
echo ">>>>>>>>>>>>>>>>>>>>> $(date '+%Y-%m-%d %H:%M:%S') run ${case_name} finished! <<<<<<<<<<<<<<<<<<<<<"
return ${test_result}
}
main() {
echo "Current directory: ${PWD}"
echo "Tools path: ${TOOLS_PATH}"
echo "Log path: ${LOG_PATH}"
local start_time end_time total_duration total_elapsed
start_time=$(date +%s)
echo "=== $(date '+%Y-%m-%d %H:%M:%S') ==="
mkdir -p "${LOG_PATH}"
rm -rf "${LOG_PATH:?}"/*
rm -f result_tools.txt tools_cases.txt
for example_name in "${EXAMPLE_LIST[@]}"; do
run_test_case "${TOOLS_PATH}" "${example_name}" "${LOG_PATH}"
done
cd "${LOG_PATH}"
ls * > ../tools_cases.txt
sed -i 's/.log//g' ../tools_cases.txt
cd ..
while read -r line; do
if [ -z "${line}" ]; then
continue
fi
prf=$(grep -E "test pass|passed|\[Block \(5\/6\)\]: OUTPUT = 24" "${LOG_PATH}/${line}.log" || true)
if [ -n "$prf" ]; then
echo "${line} pass" >> result_tools.txt
else
echo "${line} fail" >> result_tools.txt
fi
done < tools_cases.txt
prf2=$(grep -E "fail" result_tools.txt || true)
if [ -n "$prf2" ]; then
echo "execute samples failed"
end_time=$(date +%s)
total_duration=$((end_time - start_time))
total_elapsed=$(format_duration "${total_duration}")
echo "=== $(date '+%Y-%m-%d %H:%M:%S') ==="
echo "test cases all duration: ${total_elapsed}"
exit 1
else
echo "execute samples success"
fi
end_time=$(date +%s)
total_duration=$((end_time - start_time))
total_elapsed=$(format_duration "${total_duration}")
echo "=== $(date '+%Y-%m-%d %H:%M:%S') ==="
echo "test cases all duration: ${total_elapsed}"
}
main