diff --git a/Makefile b/Makefile index 1219cf1..1870970 100644 --- a/Makefile +++ b/Makefile @@ -76,6 +76,11 @@ all: make generate; make wire; +.PHONY: test +# test +test: + sh ./stress_test.sh $(t) + .DEFAULT_GOAL := help # show help help: diff --git a/gorm.sh b/gorm.sh index 1775528..c631c69 100755 --- a/gorm.sh +++ b/gorm.sh @@ -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代码,其字段和模型字段保持一致,并定义相关的转换等方法(简单示例)。 diff --git a/stress_test.sh b/stress_test.sh new file mode 100644 index 0000000..b175a82 --- /dev/null +++ b/stress_test.sh @@ -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 </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" \ No newline at end of file