使用Jmeter进行分布式负载测试

・4 分钟阅读

在开始之前安装这些工具!


brew install ansible
brew install terraform
brew install jmeter

可以从你自己的机器上运行它们,完整的代码库可以在Github上访问risingStack/distributed-loadtests-jmeter

为使示例设置正常运行,您需要提供给Terraform api令牌,您可以从终端这样导出该变量:


export TF_VAR_do_token=DO_TOKEN

如果希望更改测试runner实例的数量,可以通过导出其他环境变量来实现:


export TF_VAR_instance_count=2

你需要生成两个ssh密钥对,一个用于root用户,另一个用于非权限用户,Ansible将使用这些工具,后者使用ssh部署测试基础架构,因为它是无代理的。在开始测试以复制文件并在主节点上执行命令时,我们还将使用非特权用户。

将权限设置为600或700,如下所示:


chmod 600 /path/to/folder/with/keys/*

Jmeter测试


#!/bin/bash

set -e

# Argument parsing, with options for long and short names
for i in "$@"
do
case $i in
 -o=*|--out-file=*)
 # i#*= This removes the shortest substring ending with
 # '=' from the value of variable i - leaving us with just the
 # value of the argument (i is argument=value)
 OUTDIR="${i#*=}"
 shift
 ;;
 -f=*|--test-file=*)
 TESTFILE="${i#*=}"
 shift
 ;;
 -i=*|--identity-file=*)
 IDENTITYFILE="${i#*=}"
 shift
 ;;
 -p=*|--primary-ip=*)
 PRIMARY="${i#*=}"
 shift
 ;;
esac
done

# Check if we got all the arguments we'll need
if [ -z "$TESTFILE" ] || [ ! -f "$TESTFILE" ]; then
 echo "Please provide a test file"
 exit 1
fi

if [ -z "$OUTDIR" ]; then
 echo "Please provide a result destination directory"
 exit 1
fi

if [ -z "$IDENTITYFILE" ]; then
 echo "Please provide an identity file for ssh access"
 exit 1
fi

if [ -z "$PRIMARY" ]; then
 PRIMARY=$(terraform output primary_address)
fi

# Copy the test file to the primary node
scp -i "$IDENTITYFILE" -o IdentitiesOnly=yes -oStrictHostKeyChecking=no "$TESTFILE" "runner@$PRIMARY:/home/runner/jmeter/test.jmx"
# Remove files from previous runs if any, then run the current test
ssh -i "$IDENTITYFILE" -o IdentitiesOnly=yes -oStrictHostKeyChecking=no "runner@$PRIMARY" << "EOF"
 rm -rf /home/runner/jmeter/result
 rm -f /home/runner/jmeter/result.log
 cd jmeter/bin ; ./jmeter -n -r -t ../test.jmx -l ../result.log -e -o ../result -Djava.rmi.server.hostname=$(hostname -I | awk ' {print $1}')
EOF
# Get the results
scp -r -i "$IDENTITYFILE" -o IdentitiesOnly=yes -oStrictHostKeyChecking=no "runner@$PRIMARY":/home/runner/jmeter/result "$OUTDIR"


运行脚本将需要非root ssh密钥的路径。调用将如下所示:


bash run.sh -i=/path/to/non-root/ssh/key -f=/path/to/test/file -o=/path/to/results/dir

Yanyan profile image