下面的 shell 脚本可直接使用,IP 列表替换一下即可。我这里都是内网 IP,写几个实例也无所谓。前提所有机器有一个相同的用户名和密码。然后根据提示输入密码即可如果提示 id_rsa.pub 文件不存在 运行下面命令即可。
ssh-keygen -t rsa
#!/bin/bash
ip="192.168.251.167
192.168.251.219
192.168.251.208
192.168.251.223
192.168.251.180
192.168.251.93
192.168.251.226
192.168.251.160"
read -p "请输入密码:" userpassword
for i in $ip
do
sshpass -p ${userpassword} ssh-copy-id -o StrictHostKeyChecking=no -i ~/.ssh/id_rsa.pub $i
done
Jenkins 新建普通项目,添加构建参数 1 个文件参数,2 个字符串参数。可以见截图
添加构建步骤 Execute shell
cat > $WORKSPACE/ip.txt << EOF
192.168.251.139
192.168.251.218
192.168.251.217
192.168.251.183
EOF
while read ip
do
scp $WORKSPACE/$JOB_NAME root@$ip:$filePath$SeverFile_Name &
done < ip.txt
wait
echo -e "time-consuming: $SECONDS seconds"
Jenkins 新建普通项目,添加构建参数 1 个文件参数,2 个字符串参数。可以见截图
添加构建步骤 Execute shell
ip=(192.168.251.217
192.168.251.183
192.168.251.222
192.168.251.96
192.168.251.140
192.168.251.153
192.168.251.123
192.168.251.250
192.168.251.38
192.168.251.175
192.168.251.229
192.168.251.105
192.168.251.76
192.168.251.230
192.168.251.60
192.168.251.205
192.168.251.240
192.168.251.193
#192.168.251.139
#192.168.251.218
)
fileNum=${#ip[@]}
mv file3 files.csv
cp files.csv $fileName_New.csv
# 此处需要python环境。把FileSplit.py放到当前workspace路径下。
python FileSplit.py $fileNum $fileRows fileF files.csv
# 此处0 19 因为有20台服务器。服务器数量暂时固定所以没有动态计算上面服务器个数。
for i in `seq 0 19 `
do
scp $WORKSPACE/fileF$i.csv root@${ip[$i]}:/MCD/Jmeter/Product/$fileName_New.csv &
done
wait
echo -e "time-consuming: $SECONDS seconds"
# FileSplit.py
# coding:utf-8
from datetime import datetime
import sys
from os import path
def Main():
# source_dir = 'C:/Users/WL/Desktop/code/file/fileTest.csv'
# target_dir = 'C:/Users/WL/Desktop/code/file/'
# print(path.sep, sys.path[0])
target_dir = sys.path[0]+path.sep
# 获取文件数,每个文件行数,文件名字,原文件名称
files, rows, name, source_dir = int(sys.argv[1]), int(sys.argv[2]), target_dir+sys.argv[3], sys.argv[4]
# 计数器,文件后缀,存放数据
flag, index, dataList = 0, 0, []
print("切割为{0}个文件,每个文件{1}行".format(files, rows))
print("Start {}".format("="*20), datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
with open(source_dir, 'r') as f_source:
for line in f_source:
flag += 1
dataList.append(line)
if flag == rows:
with open(name + str(index) + ".csv", 'w+') as f_target:
for data in dataList:
f_target.write(data)
print(name + str(index) + ".csv")
index += 1
flag = 0
dataList = []
if index == files:
break
print("finish {}".format("="*20), datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
if __name__ == "__main__":
Main()
cat > $WORKSPACE/ip.txt << EOF
192.168.251.139
192.168.251.218
192.168.251.217
192.168.251.183
192.168.251.222
192.168.251.96
192.168.251.140
192.168.251.153
192.168.251.123
192.168.251.250
192.168.251.38
192.168.251.175
192.168.251.229
192.168.251.105
192.168.251.76
192.168.251.230
192.168.251.60
192.168.251.205
192.168.251.240
192.168.251.193
EOF
while read ip
do
# 重启Jmeter,需要服务器存在AutoJmeter.sh文件,在下面提供
ssh -n -o StrictHostKeyChecking=no root@$ip "sh /data/shell/AutoJmeter.sh restart" &
# 释放linux缓存占用内存,在下面提供
# ssh -n -o StrictHostKeyChecking=no root@$ip "sh /data/shell/dropCaches.sh" &
# 更改 内核信息-不了解勿用,需重启or重载生效,此处不提供
# ssh -n -o StrictHostKeyChecking=no root@$ip "echo 15 > /proc/sys/net/ipv4/tcp_fin_timeout
# 同上
# echo 15 > /proc/sys/net/ipv4/tcp_tw_timeout
done < ip.txt
wait
echo -e "time-consuming: $SECONDS seconds"
# dropCaches.sh
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
# AutoJmeter.sh
# description: jmeter agent
myip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
cmd="/MCD/Jmeter/bin/jmeter-server -Djava.rmi.server.hostname=$myip"
start(){
echo "Start" $myip
$cmd > /dev/null &
}
stop(){
jmeter_pid=`ps aux | grep jmeter-server | grep -v grep | awk '{print $2}'`
for pid in $jmeter_pid;do
kill -9 $pid
echo "Stop"$myip:$pid
done
}
act=$1
case $act in
'start')
start;;
'stop')
stop;;
'restart')
stop
sleep 2
start;;
*)
echo '[start|stop|restart]';;
esac
Jenkins 新增普通项目,增加构建 1 个文件构建参数,2 个字符串参数,见下图
添加构建步骤 Execute shell
mv file $JENKINS_HOME/workspace/$filePath/$fileName
wait
echo -e "time-consuming: $SECONDS seconds"
echo 生成当前日期
date=$(date +%Y%m%d%H%M)
echo 当前时间: $date
if [ ! -d "$JENKINS_HOME/workspace/$JOB_NAME/Report" ];then
mkdir $JENKINS_HOME/workspace/$JOB_NAME/Report
else
echo "Skip"
fi
echo 配置地址
# Jenkins程序在服务器上面的路径
jmxPath=/root/.jenkins/workspace/$JOB_NAME/pickup.jmx
# 生成的报告路径,可在Jenkins对应项目工作空间内直接打开
ReportPath=$JENKINS_HOME/workspace/$JOB_NAME/Report/$date
# Jmeter运行结果文件。比较大。建议服务器重新挂个ssd磁盘专门保存。也可定期清理
jtlPath=/data2/jtl/$JOB_NAME$date.jtl
echo 创建日期文件夹
mkdir $ReportPath
# Jmeter非GUI启动命令 -nt
# -l 指定日志路径,-R 负载机IP这里直接指定。不需要更改.propreties文件
# 重点是 -G 参数,对应上面Jenkins所有的构建参数,格式:-GGetDish=$GetDish 这是一个参数
# filePath:这里如果脚本有参数化文件。要用这个参数。-GfilePath=服务器保存参数化文件路径
echo 执行Jmeter
cd /MCD/Jmeter/bin
sh jmeter.sh -nt $jmxPath -l $jtlPath -GoffLineRedeem=$offLineRedeem -GGetCouponDetail=$GetCouponDetail -GGetStoresExtra=$GetStoresExtra -GGetDynamicBaseMenuInfo=$GetDynamicBaseMenuInfo -Ghost=$host -Gport=$port -GfilePath=/MCD/Jmeter/Product/ -GGetdish=$Getdish -GOrderReview=$OrderReview -GOrderSubmit=$OrderSubmit -GOrderDetail=$OrderDetail -GonePopUp=$onePopUp -GonGoingOrder=$onGoingOrder -GGetPackageDish=$GetPackageDish -GGetSingleDish=$GetSingleDish -GGetDishToShopCar=$GetDishToShopCar -GLogin=$Login -GRamp_Up=$Ramp_Up -GRunTime=$RunTime -GhomePage=${homePage} -GLoggerTest=${LoggerTest} -GnewOrderSubmit=$newOrderSubmit -GnewOrderReview=$newOrderReview -R 192.168.251.218,192.168.251.139,192.168.251.217,192.168.251.183,192.168.251.222,192.168.251.96,192.168.251.140,192.168.251.193 -e -o $ReportPath
# 输出报告路径
#echo ${JENKINS_URL}view/MCD58/job/$JOB_NAME/ws/Report/$date/index.html
import java.util.Date;
import java.text.SimpleDateFormat;
// 获取请求Data
String requestData = prev.getSamplerData();
// 截取requestBody
requestData = requestData.replaceAll("data:","");
requestData = requestData.replaceAll("no cookies","");
requestData = requestData.replaceAll("[\n\\s?]","");
// 获取响应结果
String lableName = prev.getSampleLabel();
String responseData = prev.getResponseDataAsString().replaceAll("[\\n\\s]","");
String runTime = "/MCD/Jmeter/Product/log/Pickup"+bsh.args[0]+".csv";
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");//设置日期格式
String nowTime = df.format(new Date()); // new Date()为获取当前系统时间
FileWriter fstream = new FileWriter(runTime, true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(nowTime + lableName + "requestData==" + requestData + "responseData==" + responseData);
out.write(System.getProperty("line.separator"));
out.close();
fstream.close();
cat > $WORKSPACE/ip.txt << EOF
$SlaveIP
EOF
while read ip
do
ssh -n -o StrictHostKeyChecking=no root@$ip "tail -n 10000 /MCD/Jmeter/Product/log/$Project$date.csv"
done < ip.txt
wait
echo -e "time-consuming: $SECONDS seconds"
cat > $WORKSPACE/ip.txt << EOF
#$SlaveIP
192.168.251.139
192.168.251.218
192.168.251.217
192.168.251.183
192.168.251.222
192.168.251.96
192.168.251.140
192.168.251.153
192.168.251.123
192.168.251.250
192.168.251.38
192.168.251.175
192.168.251.229
192.168.251.105
192.168.251.76
192.168.251.230
192.168.251.60
192.168.251.205
192.168.251.240
192.168.251.193
EOF
echo 生成当前日期
date=$(date +%Y%m%d%H%M)
echo 当前时间: $date
mkdir $WORKSPACE/log/$date
while read ip
do
scp root@$ip:/root/jmeter-server.log $WORKSPACE/log/$date/${ip}_Jmeter_Server.log &
done < ip.txt
cp /MCD/Jmeter/bin/jmeter.log $WORKSPACE/log/$date/MasterJmeter.log
wait
echo -e "time-consuming: $SECONDS seconds"
System.setProperty("hudson.model.DirectoryBrowserSupport.CSP", "")
# 我的Jenkins启动命令。war包形式
nohup java -Xms2g -Xmx2g -Dhudson.model.DirectoryBrowserSupport.CSP="sandbox; default-src 'self';" -jar jenkins.war --httpPort=8002 >jenkins.log 2>jenkins.log &