JMeter性能监控
1、安装
1.1安装JDK
- 解压安装包
tar -zxvf jdk-8u181-linux-x64.tar.gz
查看解压路径
cd java-se-8u41-ri/
pwd
/app/java-se-8u41-ri ----》》》解压路径
- 配置环境变量
vim /etc/profile
按i进入编辑,在profile文件尾部添加如下内容
export JAVA_HOME=/app/java-se-8u41-ri/
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:$*{JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
export PATH=$PATH:${JAVA_PATH}*
按ESC,退出编辑模式
输入冒号wq(:wq)保存并退出vim
- 验证
刷新配置文件
source /etc/profile
验证是否安装成功
java -version
1.2安装JMeter
- 准备JMeter及插件
JMeter下载地址
jmeter-plugins-manager下载地址
JMeterPlugins-Standard和JMeterPlugins-Extras下载地址
ServerAgent下载地址
其余插件:网上自己查询一些性能插件
解压JMeter压缩包
除了ServerAgent一外,都放入压缩包下的\lib\ext中
ServerAgent解压在与JMeter同级目录后一起打包压缩
然后上传到服务器中
2. 脚本安装
脚本如下
#!/bin/bash
#杩欓噷鍙浛鎹负浣犺嚜宸辩殑鎵ц绋嬪簭锛屽叾浠栦唬鐮佹棤闇€鏇存敼
JMETER_ZIP_NAME=apache-jmeter-5.4.1.zip
APP_NAME=jmeter
APP_VERSION=apache-jmeter-5.4.1
APP_HOME=/app/jmeter/${APP_VERSION}/${APP_VERSION}
ServerAgent=ServerAgent-2.2.3
APP_PORT=44445
REPORT_HOME=/app/jmeter/report
JMX_HOME=/app/jmeter/jmx
JMX="$2"
DATE=`date +%Y-%m-%d-%H-%M-%S` # mkdir `date +%Y-%m-%d-%H-%M-%S`
#浣跨敤璇存槑锛岀敤鏉ユ彁绀鸿緭鍏ュ弬鏁?
usage() {
echo "Usage: sh demo.sh [start|stop|restart|report|install|serverAgent|status]"
exit 1
}
#妫€鏌ョ▼搴忔槸鍚﹀湪杩愯
is_exist() {
pid=`ps -ef | grep $APP_NAME | grep -v grep | awk '{print $2}' `
#濡傛灉涓嶅瓨鍦ㄨ繑鍥?锛屽瓨鍦ㄨ繑鍥?
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#鍚姩鏂规硶
start() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -Dserver.port=${APP_PORT} -Dspring.config.location=${APP_HOME}/config/ -Dlogging.config=${APP_HOME}/config/logback-spring.xml -jar ${APP_HOME}/lib/${APP_NAME} > /dev/null 2>&1 &
# nohup java -Dlogging.file.path=${APP_HOME}/log -Dfile.encoding=UTF-8 -server -Xms8g -Xmx8g -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=1024m -Dspring.profiles.active=online -Dserver.port=${APP_PORT} -XX:+UseG1GC -Dcom.sun.management.jmxremote.port=6666 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=127.0.0.1 -jar ${APP_HOME}/lib/${APP_NAME} > /dev/null 2>&1 &
echo "[start] 鍚姩 $APP_NAME 瀹屾垚"
fi
}
#鍋滄鏂规硶
stop() {
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#杈撳嚭杩愯鐘舵€?
status() {
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is not running."
fi
}
#閲嶅惎
restart() {
stop
start
}
#鐢熸垚鎶ュ憡
report() {
mkdir ${REPORT_HOME}/${DATE}
jmeter -n -t ${JMX_HOME}/${JMX} -l ${REPORT_HOME}/${DATE}/${DATE}.jtl -e -o ${REPORT_HOME}/${DATE}
stop
}
#解压jmeter安装包,压缩包名(放在/app下)
install() {
cd /app
if [ ! -d "${APP_NAME}" ]; then
mkdir ${APP_NAME}
mv /app/${JMETER_ZIP_NAME} /app/${APP_NAME}
mkdir /app/${APP_NAME}/data
mkdir /app/${APP_NAME}/report
mkdir /app/${APP_NAME}/report/jvm
mkdir /app/${APP_NAME}/jmx
cd /app/${APP_NAME}
for file in /app/${APP_NAME}/*
do
sname=$(basename $file)
if [ "${JMETER_ZIP_NAME}" == "$sname" ];then
echo "${JMETER_ZIP_NAME} is exist"
unzip -o ${JMETER_ZIP_NAME}
mv /app/${APP_NAME}/${JMETER_ZIP_NAME} /app
echo "${JMETER_ZIP_NAME} unzip ok!"
cd ${APP_HOME}/bin
chmod 777 jmeter
echo "#jmeter path
export JMETER_HOME=${APP_HOME}
export PATH=\${JMETER_HOME}/bin:\$PATH
export CLASSPATH=\$JMETER_HOME/lib/ext/ApacheJMeter_core.jar:\$JMETER_HOME/lib/jorphan.jar:\$CLASSPATH" >>/etc/profile
source /etc/profile
echo "jmeter path is ok!"
cd /
jmeter --version
fi
done
else
echo "${APP_NAME} directory is already exist"
fi
}
#serverAgent安装包放在jmeter压缩包内
serverAgent() {
for dir in /app/${APP_NAME}/${APP_VERSION}/*
do
sname=$(basename $dir)
if [ "${ServerAgent}" == "$sname" ];then
echo "${ServerAgent} is exist"
cd /app/${APP_NAME}/${APP_VERSION}/${ServerAgent}
chmod 777 startAgent.sh
nohup ./startAgent.sh >/dev/null 2>&1 &
ps -aux | grep startAgent
echo "startAgent.sh start over!"
fi
done
}
#鏍规嵁杈撳叆鍙傛暟锛岄€夋嫨鎵ц瀵瑰簲鏂规硶锛屼笉杈撳叆鍒欐墽琛屼娇鐢ㄨ鏄?
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
"report")
report
;;
"install")
install
;;
"serverAgent")
serverAgent
;;
*)
usage
;;
esac
执行脚本sh app.sh install
如下安装完成
3. 启动ServerAgent
cd jmeter/apache-jmeter-5.4.1/ServerAgent-2.2.3/
sh startAgent.sh
假如不想用默认的4444端口可以修改
java -jar ./CMDRunner.jar --tool PerfMonAgent --udp-port 7000 --tcp-port 7000
2、执行
2.1准备
- 准备JMX脚本
先在widows调试号脚本,修改CSV保存路径后,上传至jmx目录下
替换文件保存路径
C:\Users\jinxiao.lu\Desktop\JMeter\report\jvm\1.csv
改为
/app/jmeter/report/jvm/1.csv
-
上传jmx
-
启动ServerAgent
cd jmeter/apache-jmeter-5.4.1/ServerAgent-2.2.3/
java -jar ./CMDRunner.jar --tool PerfMonAgent --udp-port 7000 --tcp-port 7000
2.2执行
sh app.sh report baidu.jmx
2.3打包下载报告
cd /app/jmeter
zip -r report.zip report
2.4查看报告
2.5处理CSV
脚本处理
隐藏H-K列,切换为折线图
3、备注
服务器资源有限,本次用执行机并发访问百度,监控执行机性能