Files
os-autotest/testcases/performance_test/openruyi_unixbench/test_perf_unixbench.sh
2026-04-14 17:59:51 +08:00

130 lines
3.7 KiB
Bash

#!/usr/bin/bash
# Copyright (c) 2024 ISCAS .ALL rights reserved.
# This program is licensed under Mulan PSL v2.
# You can use it according to the terms and conditions of the Mulan PSL v2.
# http://license.coscl.org.cn/MulanPSL2
# THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
# #############################################
# @Author : honghua
# @Contact : honghua@iscas.ac.cn
# @Date : 2026-01-07
# @License : Mulan PSL v2
# @Desc : 执行 unixbench 跑分工具,评估。
# ############################################
source "$OET_PATH/libs/locallibs/common_lib.sh"
function pre_test() {
LOG_INFO "Start to prepare the test environment."
DNF_INSTALL "unixbench"
LOG_INFO "End to prepare the test environment."
}
function get_system_info() {
os_name=$(grep "^PRETTY_NAME=" /etc/os-release | cut -d'"' -f2 | awk '{print $1}')
os_version=$(grep "^VERSION=" /etc/os-release | cut -d'"' -f2)
os_arch=$(uname -m)
cpu=$(nproc)
memory=$(free -g | awk '/^Mem:/{print $2}')
}
function escape_json_string() {
local str="$1"
str="${str//\\/\\\\}"
str="${str//\"/\\\"}"
str="${str//$'\n'/\\n}"
str="${str//$'\r'/\\r}"
str="${str//$'\t'/\\t}"
printf '%s' "$str"
}
function parse_unixbench_result() {
local log_content="$1"
score=$(echo "$log_content" | grep "System Benchmarks Index Score" | awk '{print $NF}')
[ -z "$score" ] && score="unknown"
}
function send_result() {
local api_url="https://data-baicao-verify.verify.oepkgs.net/api/v1/unixbench"
local parallel="$1"
local detail="$2"
local json_data=$(cat <<EOF
{
"os_name": "${os_name}",
"os_version": "${os_version}",
"os_arch": "${os_arch}",
"cpu": ${cpu},
"memory": ${memory},
"parallel": ${parallel},
"score": "${score}",
"detail": "${detail}"
}
EOF
)
local response=$(curl -X POST "${api_url}" \
-H "Content-Type: application/json" \
-d "${json_data}" \
-w "%{http_code}" \
-o /tmp/curl_response.txt \
-s 2>&1)
local curl_exit_code=$?
local http_code="${response: -3}"
if [ $curl_exit_code -ne 0 ] || [ "$http_code" -lt 200 ] || [ "$http_code" -ge 300 ]; then
LOG_INFO "发送测试结果到 API 失败 (HTTP 状态码:${http_code}, curl 退出码:${curl_exit_code}),但不影响测试流程"
return 0
else
LOG_INFO "测试结果已成功发送到 API"
return 0
fi
}
function run_test() {
LOG_INFO "基础环境准备..."
EXECUTE_T="60m"
cd /opt/unixbench
LOG_INFO "开始执行 unixbench 工具 ..."
LOG_INFO "开始执行 ./Run -c 1"
local result_c1=$(./Run -c 1 2>&1 | tee /dev/tty)
if [ $? -ne 0 ]; then
LOG_ERROR "执行 ./Run -c 1 测试失败"
exit 255
fi
get_system_info
parse_unixbench_result "$result_c1"
local escaped_detail=$(escape_json_string "$result_c1")
send_result "1" "$escaped_detail"
LOG_INFO "开始执行 ./Run -c $(getconf _NPROCESSORS_ONLN)"
local result_multi=$(./Run -c $(getconf _NPROCESSORS_ONLN) 2>&1 | tee /dev/tty)
if [ $? -ne 0 ]; then
LOG_ERROR "执行 ./Run -c $(getconf _NPROCESSORS_ONLN) 测试失败"
exit 255
fi
parse_unixbench_result "$result_multi"
escaped_detail=$(escape_json_string "$result_multi")
send_result "$(getconf _NPROCESSORS_ONLN)" "$escaped_detail"
}
function post_test() {
LOG_INFO "Start to restore the test environment."
DNF_REMOVE "$@"
LOG_INFO "Finish restoring the test environment."
}
main "$@"