#!/bin/bash # 资金服务平台性能测试脚本 set -e echo "========================================" echo " 资金服务平台 JMeter 性能测试" echo "========================================" # 颜色定义 RED='\033[0;31m' GREEN='\033[0;32m' YELLOW='\033[1;33m' BLUE='\033[0;34m' NC='\033[0m' print_info() { echo -e "${GREEN}[INFO]${NC} $1" } print_warn() { echo -e "${YELLOW}[WARN]${NC} $1" } print_error() { echo -e "${RED}[ERROR]${NC} $1" } print_title() { echo -e "${BLUE}[TEST]${NC} $1" } # 检查JMeter if ! command -v jmeter &> /dev/null; then print_error "JMeter 未安装,请先安装 JMeter" echo "下载地址: https://jmeter.apache.org/download_jmeter.cgi" exit 1 fi # 创建结果目录 mkdir -p results reports # 解析参数 TEST_TYPE=${1:-all} BASE_URL=${2:-localhost} PORT=${3:-8080} print_info "测试目标: http://${BASE_URL}:${PORT}" # 函数:运行测试 run_test() { local test_name=$1 local thread_count=$2 local duration=$3 print_title "开始测试: ${test_name}" print_info "并发数: ${thread_count}, 持续时间: ${duration}s" jmeter -n -t fundplatform-test-plan.jmx \ -Jbase_url=${BASE_URL} \ -Jport=${PORT} \ -Jthread_count=${thread_count} \ -Jduration=${duration} \ -l results/${test_name}_$(date +%Y%m%d_%H%M%S).jtl \ -e -o reports/${test_name}_$(date +%Y%m%d_%H%M%S) print_info "测试完成: ${test_name}" } # 根据测试类型执行 case $TEST_TYPE in login) print_title "执行登录接口压测" run_test "login" 50 60 ;; customer) print_title "执行客户列表查询压测" run_test "customer" 100 120 ;; dashboard) print_title "执行仪表盘统计压测" run_test "dashboard" 200 180 ;; all) print_title "执行全部性能测试" print_warn "这将执行所有测试场景,耗时较长..." # 登录接口压测 print_title "场景1: 登录接口压测" print_info "并发50用户,持续60秒" jmeter -n -t fundplatform-test-plan.jmx \ -Jbase_url=${BASE_URL} \ -Jport=${PORT} \ -l results/login_test_$(date +%Y%m%d_%H%M%S).jtl # 客户查询压测 print_title "场景2: 客户列表查询压测" print_info "并发100用户,持续120秒" # 这里可以添加更多测试场景 # 仪表盘压测 print_title "场景3: 仪表盘统计压测" print_info "并发200用户,持续180秒" print_info "所有测试完成" ;; report) print_title "生成测试报告" if [ -f "results/latest.jtl" ]; then jmeter -g results/latest.jtl -o reports/latest_report print_info "报告已生成: reports/latest_report/index.html" else print_error "没有找到测试结果文件" exit 1 fi ;; *) echo "用法: $0 {login|customer|dashboard|all|report} [base_url] [port]" echo "" echo "测试类型:" echo " login - 登录接口压测 (50并发)" echo " customer - 客户列表查询压测 (100并发)" echo " dashboard - 仪表盘统计压测 (200并发)" echo " all - 执行全部测试" echo " report - 生成测试报告" echo "" echo "示例:" echo " $0 all localhost 8080" echo " $0 login 192.168.1.100 8080" exit 1 ;; esac echo "" echo "========================================" echo " 性能测试执行完成" echo "========================================" echo "" echo "测试结果:" echo " - JTL文件: results/" echo " - HTML报告: reports/" echo "" echo "查看报告:" echo " open reports/*/index.html"