一、构建JMeter基础镜像包
官网link : ''' https://jmeterenespanol.org/blog/2020-05-15-docker-antonio/ '''
Dockerfile
# 1 From Apline Linux
FROM alpine:3.11
# 2 Maintainer name
LABEL maintainer="summer.gan@ringcentral.com"
# 3 Set JMeter version 5.2.1 for downloading and some ENV variables
ENV JMETER_VERSION 5.2.1
ENV JMETER_FOLDER /home/jmeter/
ENV JMETER_HOME /home/jmeter/apache-jmeter-${JMETER_VERSION}
ENV JMETER_BIN ${JMETER_HOME}/bin
ENV JMETER_DOWNLOAD https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-${JMETER_VERSION}.tgz
# 4 Update, upgrade, download Java JRE and JMeter then decompress it
RUN apk update \
&& apk upgrade \
&& apk add ca-certificates \
&& update-ca-certificates \
&& apk add --update openjdk8-jre tzdata curl unzip bash \
&& apk add --no-cache nss \
&& rm -rf /var/cache/apk/* \
&& mkdir -p /tmp/jmeter/ \
&& curl -L --silent ${JMETER_DOWNLOAD} > /tmp/jmeter/apache-jmeter-${JMETER_VERSION}.tgz \
&& mkdir -p ${JMETER_FOLDER} \
&& tar -xzf /tmp/jmeter/apache-jmeter-${JMETER_VERSION}.tgz -C ${JMETER_FOLDER} \
&& rm -rf /tmp/jmeter
# 5 Copy entrypoint to the container
COPY entrypoint.sh /
# 6 Set environment variables
ENV PATH $PATH:$JMETER_BIN
# 7 Set Working Dir
WORKDIR ${JMETER_HOME}
# 8 Launch entrypoint
ENTRYPOINT ["/entrypoint.sh"]
entrypoint.sh
#!/bin/bash
# Inspired from https://github.com/justb4/docker-jmeter
# Basically runs jmeter, assuming the PATH is set to point to JMeter bin-dir (see Dockerfile)
#
# This script expects the standdard JMeter command parameters.
#
set -e
freeMem=`awk '/MemFree/ { print int($2/1024) }' /proc/meminfo`
s=$(($freeMem/10*8))
x=$(($freeMem/10*8))
n=$(($freeMem/10*2))
export JVM_ARGS="-Xmn${n}m -Xms${s}m -Xmx${x}m"
echo " "
echo " "
echo "---- STARTING JMeter Test ---- `date`"
echo "JVM_ARGS=${JVM_ARGS}"
echo "jmeter args=$@"
jmeter $@
echo "---- ENDING JMeter Test ---- `date`"
echo " "
echo " "
通过github的hook 将image推到docker Hub
build.main.yml
name: Build jmeter-docker
on:
push:
branches:
- main
paths:
# '*' matches any character except '/'
- 'image/*'
- '.github/workflows/*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- name: Docker login
env:
DOCKER_HUB_USERNAME: ${{ secrets. DOCKER_HUB_USERNAME }}
DOCKER_HUB_PASSWORD: ${{ secrets. DOCKER_HUB_PASSWORD }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
echo $DOCKER_HUB_PASSWORD | docker login --username $DOCKER_HUB_USERNAME --password-stdin
echo $GITHUB_TOKEN | docker login docker.pkg.github.com --username $GITHUB_ACTOR --password-stdin
- name: Build & Push Image
run: |
cd image
IMAGE_NAME=ringcentral/jmeter-docker
VERSION="$(awk -F ' ' '/ENV JMETER_VERSION/ {print $3}' Dockerfile)"
docker build --tag ${IMAGE_NAME} --file Dockerfile .
docker tag "${IMAGE_NAME}" "${IMAGE_NAME}:${VERSION}"
docker push "${IMAGE_NAME}:${VERSION}"
docker tag "${IMAGE_NAME}" "${IMAGE_NAME}:latest"
docker push "${IMAGE_NAME}:latest"
二、构建带有Plugin的镜像
FROM ringcentral/jmeter-docker:5.2.1
# https://jmeter-plugins.org/wiki/TestPlanCheckTool/
ENV PLAN_CHECK_PLUGIN_VERSION=2.4
RUN wget https://jmeter-plugins.org/files/packages/jpgc-plancheck-${PLAN_CHECK_PLUGIN_VERSION}.zip
RUN unzip -o jpgc-plancheck-${PLAN_CHECK_PLUGIN_VERSION}.zip -d ${JMETER_HOME}
# https://jmeter-plugins.org/wiki/UltimateThreadGroup/
ENV CUSTOM_PLUGIN_VERSION=2.9
RUN wget https://jmeter-plugins.org/files/packages/jpgc-casutg-${CUSTOM_PLUGIN_VERSION}.zip
RUN unzip -o jpgc-casutg-${CUSTOM_PLUGIN_VERSION}.zip -d ${JMETER_HOME}
EXPOSE 1099
ENTRYPOINT ["/entrypoint.sh"]
本地build命令: docker build -t "jmeter/docker-plugin:5.2.1" .
三、执行测试
run.sh
#!/bin/bash
#
# Run JMeter Docker image with options
NAME="jmeter"
IMAGE="jmeter/docker-plugin:5.2.1"
# Finally run
sudo docker stop ${NAME} > /dev/null 2>&1
sudo docker rm ${NAME} > /dev/null 2>&1
sudo docker run --name ${NAME} -i -v ${PWD}:${PWD} -w ${PWD} ${IMAGE} $@
执行测试:
export SCRIPT="test/***_Test_Plan.jmx"
export JTL="test/***_Test_Plan.jtl"
export LOG="test/***_Test_Plan.log"
./run.sh \
-n -t ${SCRIPT} \
-l ${JTL} \
-j ${LOG} \