Files
udmin/scripts/run_stress_code_js1.sh
ayou a1b21e87b3 feat(scripts): 添加针对code_js1接口的压测脚本和结果
添加压测脚本run_stress_code_js1.sh和stress_code_js1.py,用于对code_js1接口进行并发压力测试
生成压测结果JSON文件,包含请求统计、延迟和吞吐量等指标
2025-10-10 23:23:02 +08:00

95 lines
2.8 KiB
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env bash
set -euo pipefail
# 默认参数
URL="http://127.0.0.1:9898/api/dynamic/code_js1"
METHOD="POST"
TOTAL=1000
CONCURRENCY=50
TIMEOUT=8
HEADERS='{"Content-Type":"application/json"}'
BODY='{}'
OUT_PREFIX="scripts/results/code_js1"
print_usage() {
cat <<EOF
用法:
./scripts/run_stress_code_js1.sh [选项]
选项:
-u, --url URL 压测目标地址(默认:${URL}
-m, --method METHOD 请求方法(默认:${METHOD}
-t, --total N 总请求数(默认:${TOTAL}
-c, --concurrency N 并发数(默认:${CONCURRENCY}
-T, --timeout SEC 单次请求超时秒数(默认:${TIMEOUT}
-H, --headers JSON 请求头 JSON 字符串(默认:${HEADERS}
-b, --body JSON 请求体 JSON 字符串(默认:${BODY}
-o, --out-prefix PREFIX 结果前缀(默认:${OUT_PREFIX},只保存 JSON
-h, --help 显示帮助
示例:
# 快速压测 3000 次,请求并发 150POST 空体,输出到 scripts/results/
./scripts/run_stress_code_js1.sh -t 3000 -c 150 -m POST -o scripts/results/code_js1
# 自定义 URL 与请求体
./scripts/run_stress_code_js1.sh \
--url http://127.0.0.1:9898/api/dynamic/code_js1 \
--method POST \
--body '{"foo":"bar"}' \
--headers '{"Content-Type":"application/json"}' \
--total 2000 --concurrency 100 --timeout 10 \
--out-prefix scripts/results/code_js1_run2
EOF
}
# 解析参数(支持短/长选项)
while [[ $# -gt 0 ]]; do
case "$1" in
-u|--url) URL="$2"; shift 2 ;;
-m|--method) METHOD="$2"; shift 2 ;;
-t|--total) TOTAL="$2"; shift 2 ;;
-c|--concurrency) CONCURRENCY="$2"; shift 2 ;;
-T|--timeout) TIMEOUT="$2"; shift 2 ;;
-H|--headers) HEADERS="$2"; shift 2 ;;
-b|--body) BODY="$2"; shift 2 ;;
-o|--out-prefix) OUT_PREFIX="$2"; shift 2 ;;
-h|--help) print_usage; exit 0 ;;
*) echo "未知参数:$1"; echo; print_usage; exit 1 ;;
esac
done
# 环境校验
if ! command -v python3 >/dev/null 2>&1; then
echo "错误:未找到 python3请先安装 Python。" >&2
exit 1
fi
if [[ ! -f "scripts/stress_code_js1.py" ]]; then
echo "错误:未找到 scripts/stress_code_js1.py请确认项目目录正确。" >&2
exit 1
fi
# 确保输出目录存在
mkdir -p "$(dirname "$OUT_PREFIX")"
echo "运行压测url=$URL method=$METHOD total=$TOTAL concurrency=$CONCURRENCY timeout=$TIMEOUT"
echo "请求头:$HEADERS"
echo "请求体:$BODY"
echo "结果前缀:$OUT_PREFIX (只保存 JSON)"
echo
set -x
python3 scripts/stress_code_js1.py \
--url "$URL" \
--method "$METHOD" \
--total "$TOTAL" \
--concurrency "$CONCURRENCY" \
--timeout "$TIMEOUT" \
--headers "$HEADERS" \
--body "$BODY" \
--out-prefix "$OUT_PREFIX"
set +x
echo
echo "完成。请在上方输出中查看保存的 JSON 结果路径。"