Ubuntu 下 Tomcat 性能测试与监控实操指南
一 测试准备与关键指标
二 快速上手 两种常用工具与命令
jmeter -n -t benchmark.jmx -Jthreads=100 -Jduration=300 -Jurl=http://localhost:8080/app/api -l results/result_100u.jtl
说明:线程数 100、持续时间 300s,结果写入 JTL 文件便于后续聚合分析。ab -n 10000 -c 200 http://localhost:8080/
ab -n 1000 -c 100 -p data.json -T application/json http://localhost:8080/add
三 启用 JMX 监控 Tomcat 与 JVM
export CATALINA_OPTS="$CATALINA_OPTS \
-Dcom.sun.management.jmxremote \
-Dcom.sun.management.jmxremote.port=1099 \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.authenticate=false \
-Djava.rmi.server.hostname=127.0.0.1"
重启 Tomcat 后用 JConsole 连接 localhost:1099 查看 MBean。四 监控与结果分析要点
五 自动化脚本与持续化压测
#!/usr/bin/env bash
set -euo pipefail
TOMCAT_URL="http://localhost:8080"
TEST_DURATION=300
CONCURRENT_USERS=(50 100 200)
RESULT_DIR="./results/$(date +%Y%m%d_%H%M%S)"
mkdir -p "$RESULT_DIR"
check_env() {
if ! curl -s "$TOMCAT_URL/manager/status" | grep -q "OK"; then
echo "Tomcat 未就绪"; exit 1
fi
}
run_load_test() {
local users=$1
jmeter -n -t ./benchmark/template.jmx \
-Jthreads=$users -Jduration=$TEST_DURATION -Jurl=$TOMCAT_URL \
-l "$RESULT_DIR/jmeter_${users}u.jtl"
}
collect_metrics() {
local users=$1
# 伪采集示例:线程、会话、空闲内存(实际可用 JMXterm/jmxquery 等)
echo "{\"threads\":\"$(curl -s "$TOMCAT_URL/manager/status/all" | awk '/Max threads/{print $4}')}\"" \
> "$RESULT_DIR/metrics_${users}u.json"
}
main() {
check_env
for u in "${CONCURRENT_USERS[@]}"; do
run_load_test "$u"
collect_metrics "$u"
done
# 可调用 JMeter 报告生成或 Python 聚合脚本
}
main