This commit is contained in:
李子铭 2025-03-10 14:52:01 +08:00
parent 3e876ed49a
commit a603f7d955
3 changed files with 145 additions and 0 deletions

View File

@ -76,6 +76,11 @@ all:
make generate;
make wire;
.PHONY: test
# test
test:
sh ./stress_test.sh $(t)
.DEFAULT_GOAL := help
# show help
help:

View File

@ -1,5 +1,9 @@
#!/bin/bash
# 定义接口 1 和接口 2 的 URL
API_1_URL="https://gateway.dev.cdlsxd.cn/voucher/cmb/v1/orderMock"
API_2_URL="https://gateway.dev.cdlsxd.cn/voucher/cmb/v1/order"
# 脚本功能:
# 1. 根据传入的表名和应用名连接指定数据库生成对应表的模型代码使用gentool工具需确保该工具已正确安装并可用
# 2. 生成对应表的领域实体Bo代码其字段和模型字段保持一致并定义相关的转换等方法简单示例

136
stress_test.sh Normal file
View File

@ -0,0 +1,136 @@
#!/bin/bash
# 定义接口 1 和接口 2 的 URL
API_1_URL="https://gateway.dev.cdlsxd.cn/voucher/cmb/v1/orderMock"
API_2_URL="https://gateway.dev.cdlsxd.cn/voucher/cmb/v1/order"
# 定义接口 1 请求的公共请求头
API_1_HEADERS=(
"User-Agent: Apifox/1.0.0 (https://apifox.com)"
"Content-Type: application/json"
"Accept: */*"
"Host: 127.0.0.1:8000"
"Connection: keep-alive"
)
# 定义接口 2 请求的公共请求头
API_2_HEADERS=(
"User-Agent: Apifox/1.0.0 (https://apifox.com)"
"Content-Type: application/json"
"Accept: */*"
"Host: 127.0.0.1:8000"
"Connection: keep-alive"
)
# 压测参数设置
CONCURRENCY=2 # 并发数
QPS=5 # 每秒请求数
DURATION=2 # 压测持续时间(秒)
TOTAL_REQUESTS=$((CONCURRENCY * DURATION)) # 总请求次数
# 计算每次请求的间隔时间(秒)
REQUEST_INTERVAL=$(echo "scale=3; 1 / $QPS" | bc)
# 创建日志目录(自动忽略已存在的目录)
mkdir -p "./log/stress"
# 初始化统计变量
success_count=0
failure_count=0
total_response_time=0
log_file="./log/stress/$(date +%Y%m%d%H%M%S)_stress.log"
# 检查是否安装jq
if ! command -v jq &> /dev/null; then
echo "错误未安装jq工具请先执行 'brew install jq' 安装"
exit 1
fi
# 定义高精度时间获取函数
get_time() {
local t=$(date +%s%N)
echo $((10#${t:0:10} * 1000000000 + 10#${t:10:9}))
}
# 循环执行压测
for ((i = 0; i < TOTAL_REQUESTS; i++)); do
# 生成随机 transactionId
transaction_id=$(date +%s)$RANDOM
# 构建接口 1 请求体
API_1_BODY=$(cat <<EOF
{
"transactionId": "$transaction_id",
"activityId": "001",
"cmbUid": "oO3vO5K2nE131-9uMoeYymLhlbYk",
"cmbUidType": "1",
"timestamp": "19780107024253",
"appId": "wx9ed74283ad25bca1"
}
EOF
)
# 测量接口 1 响应时间(微秒)
start_time_1=$(get_time)
response_1=$(curl -s -X POST "${API_1_HEADERS[@]/#/-H }" -d "$API_1_BODY" "$API_1_URL")
end_time_1=$(get_time)
duration_1=$(( (end_time_1 - start_time_1) / 1000 )) # 转换为微秒
# 解析接口 1 响应
if [ -z "$response_1" ]; then
echo "接口 1 请求失败(无响应)" | tee -a "$log_file"
((failure_count++))
continue
fi
# 验证JSON格式
if ! echo "$response_1" | jq -e . >/dev/null 2>&1; then
echo "接口 1 返回无效JSON" | tee -a "$log_file"
((failure_count++))
continue
fi
# 提取code字段
api1_code=$(echo "$response_1" | jq -r '.code')
if [ "$api1_code" != "200" ]; then
echo "接口 1 返回错误code=$api1_code, 响应=$response_1" | tee -a "$log_file"
((failure_count++))
continue
fi
# 测量接口 2 响应时间
start_time_2=$(get_time)
response_2=$(curl -s -X POST "${API_2_HEADERS[@]/#/-H }" -d "$response_1" "$API_2_URL")
end_time_2=$(get_time)
duration_2=$(( (end_time_2 - start_time_2) / 1000 ))
# 记录日志
echo "[$(date +%Y-%m-%dT%H:%M:%S)] 请求 $((i+1))" | tee -a "$log_file"
echo "接口 1 响应时间:$duration_1 μs" | tee -a "$log_file"
echo "接口 2 响应时间:$duration_2 μs" | tee -a "$log_file"
echo "接口 2 响应内容:$response_2" | tee -a "$log_file"
echo "----------------------------------------" | tee -a "$log_file"
# 统计成功次数
((success_count++))
total_response_time=$((total_response_time + duration_2))
# 控制请求频率
sleep $REQUEST_INTERVAL
done
# 输出统计结果
echo -e "\n压测统计"
echo "总请求次数: $TOTAL_REQUESTS"
echo "成功次数: $success_count"
echo "失败次数: $failure_count"
if [ $success_count -gt 0 ]; then
avg_response=$((total_response_time / success_count))
echo "平均响应时间: $avg_response μs (成功请求)"
else
echo "平均响应时间: 无成功请求,无法计算"
fi
echo "日志文件: $log_file"