plp-test/scripts/run_test.sh
2025-04-22 16:53:06 +08:00

159 lines
4.1 KiB
Bash
Executable File

#!/bin/bash
# 检查是否已安装Open-CAS
check_opencas() {
if ! command -v casadm &> /dev/null; then
echo "错误: Open-CAS未安装或无法在PATH中找到。"
echo "请先安装Open-CAS: https://open-cas.github.io/"
exit 1
fi
echo "Open-CAS已安装"
}
# 检查设备是否存在
check_devices() {
local nvme_device=$(grep -oP 'devices_nvme: "\K[^"]+' config.yaml)
local hdd_device=$(grep -oP 'devices_hdd: "\K[^"]+' config.yaml)
if [ ! -b "$nvme_device" ]; then
echo "错误: NVMe设备 $nvme_device 不存在或不是块设备"
exit 1
fi
if [ ! -b "$hdd_device" ]; then
echo "错误: HDD设备 $hdd_device 不存在或不是块设备"
exit 1
fi
echo "设备检查通过: $nvme_device$hdd_device 存在"
}
# 编译项目
build_project() {
echo "编译项目..."
go build -o bin/server cmd/server/main.go
go build -o bin/client cmd/client/main.go
if [ $? -ne 0 ]; then
echo "编译失败"
exit 1
fi
echo "编译成功"
}
# 启动服务器
start_server() {
echo "启动服务器..."
mkdir -p logs
./bin/server -config config.yaml -log-level info > logs/server.log 2>&1 &
SERVER_PID=$!
echo "服务器进程ID: $SERVER_PID"
echo "等待服务器启动..."
sleep 3
# 检查服务器是否正常启动
if ! ps -p $SERVER_PID > /dev/null; then
echo "服务器启动失败,请检查日志: logs/server.log"
exit 1
fi
echo "服务器已成功启动"
}
# 运行指定测试
run_test() {
local test_type=$1
local data_size=$2
local block_size=$3
echo "运行测试: $test_type (数据大小: ${data_size}MB, 块大小: ${block_size}KB)"
./bin/client -config config.yaml -test "$test_type" -data-size "$data_size" -block-size "$block_size" > "logs/client_${test_type}.log" 2>&1
if [ $? -ne 0 ]; then
echo "测试 $test_type 失败,请检查日志: logs/client_${test_type}.log"
return 1
fi
echo "测试 $test_type 完成"
return 0
}
# 运行所有测试
run_all_tests() {
local data_size=$1
local block_size=$2
local concurrent=$3
if [ "$concurrent" = "true" ]; then
echo "并发运行所有测试 (数据大小: ${data_size}MB, 块大小: ${block_size}KB)"
./bin/client -config config.yaml -test all -data-size "$data_size" -block-size "$block_size" -concurrent > logs/client_all.log 2>&1
else
echo "顺序运行所有测试 (数据大小: ${data_size}MB, 块大小: ${block_size}KB)"
./bin/client -config config.yaml -test all -data-size "$data_size" -block-size "$block_size" > logs/client_all.log 2>&1
fi
if [ $? -ne 0 ]; then
echo "运行所有测试失败,请检查日志: logs/client_all.log"
return 1
fi
echo "所有测试完成"
return 0
}
# 停止服务器
stop_server() {
if [ -n "$SERVER_PID" ]; then
echo "停止服务器 (PID: $SERVER_PID)..."
kill $SERVER_PID
wait $SERVER_PID 2>/dev/null
echo "服务器已停止"
fi
}
# 清理资源
cleanup() {
echo "清理资源..."
stop_server
# 检查是否有遗留的缓存实例
local cache_id=$(grep -oP 'cache_instance_id: "\K[^"]+' config.yaml)
if casadm -L | grep -q "Cache Instance $cache_id"; then
echo "停止缓存实例 $cache_id..."
casadm -T -i $cache_id -f
fi
echo "清理完成"
}
# 主函数
main() {
local test_type=${1:-"all"}
local data_size=${2:-100} # 默认100MB
local block_size=${3:-4} # 默认4KB
local concurrent=${4:-"false"} # 默认非并发
mkdir -p bin logs
# 注册清理函数
trap cleanup EXIT
check_opencas
check_devices
build_project
start_server
if [ "$test_type" = "all" ]; then
run_all_tests "$data_size" "$block_size" "$concurrent"
else
run_test "$test_type" "$data_size" "$block_size"
fi
echo "测试执行完毕"
}
# 执行主函数,参数: 测试类型 数据大小(MB) 块大小(KB) 是否并发
main "$@"