NEWS AND BLOGATAHON2021 BLOGS

Docker based Jmeter and Jmeter-Selenium Script Execution (Jmeter execution migration from Local to Docker)

Blogathon2021

Docker based Jmeter and Jmeter-Selenium Script Execution (Jmeter execution migration from Local to Docker)

By Vignesh M.

Now a days, every software development process moving to containerized platform including development, testing, continuous integration, and continuous deployment, etc. Even the Cloud platform providers like Amazon AWS, Google GCP, Microsoft Azure and others provides gives containerize option to make the cloud much flexible and user friendly for time and cost effectiveness.

Docker will be the best option to implement containerized platform to make our work much easier. We can see better memory usage, good performance, best support in application/docker container portability and quick boot-up time when compared with Virtual Machines. Caching the container creation steps, flexible resource sharing, fast deployment, fewer software dependency and strong security makes Docker more famous among other containerize platforms. Also, we can run docker in all the platforms like Linux, Windows, MAC and other Cloud platforms

We can make many customizations in our local Jmeter copy includes custom user and system properties to make our execution smooth and convenient. While migrating our execution from local to Docker Jmeter, it requires more time and customizations to run with exact setting and properties. Many articles available in online to guide Dockerized Jmeter execution from scratch. But this blog post explains more on Jmeter execution migration from Local to Docker platform with predefined Jmeter configuration files which we used for our local Jmeter testing.

Jmeter execution migration from Local to Docker

Below mentioned Docker file help us to run the Jmeter execution using our local Jmeter copy with all user defined and system properties.

Docker File
				
					FROM alpine

ARG JMETER_VERSION="5.4.1"
ENV JMETER_HOME /opt/apache-jmeter-${JMETER_VERSION}
ENV JMETER_BIN ${JMETER_HOME}/bin
ENV JAVA_OPTS -Dwebdriver.chrome.whitelistedIps
ENV PATH $PATH:${JMETER_BIN}

ARG TZ="Europe/London"
ENV TZ ${TZ}
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/* \
	&& apk add chromium \
	&& apk add chromium-chromedriver
	
#=================================
# Chrome Launch Script Wrapper
#=================================
COPY wrap_chromium_binary /opt/bin/wrap_chromium_binary
RUN chmod 777 /opt/bin/wrap_chromium_binary
RUN /opt/bin/wrap_chromium_binary
	
	
COPY ./apache-jmeter-5.4.1/. /opt/apache-jmeter-5.4.1/.
COPY entrypoint.sh /
RUN chmod 777 /usr/bin/chromedriver
RUN cp /usr/bin/chromedriver /opt/apache-jmeter-5.4.1/bin/.

WORKDIR ${JMETER_HOME}

RUN chmod 777 /entrypoint.sh

ENTRYPOINT ["/entrypoint.sh"]

				
			

Here we use the lightweight Linux distribution, alpine Linux to run our Jmeter test execution which helps fast booting and less system resource utilization. Next, we set the arguments and environment variables. After that, we update the required packages in the Linux distribution for Jmeter execution. Once we update the Linux, we copy the chromium wrap file (which helps to invoke the chromium driver for selenium based jmeter script execution), our local jmeter and supporting folder to container file system path. At the end, we use entrypoint.sh file as an entry point to run our container.

wrap_chromium_binary
				
					#!/bin/bash

WRAPPER_PATH=$(readlink -f /usr/bin/chromium-browser)
BASE_PATH="$WRAPPER_PATH-base"
mv "$WRAPPER_PATH" "$BASE_PATH"

cat > "$WRAPPER_PATH" <<_EOF
#!/bin/bash
# Note: exec -a below is a bashism.
exec -a "\$0" "$BASE_PATH" --no-sandbox "\$@"
_EOF
chmod +x "$WRAPPER_PATH"


				
			
entrypoint.sh
				
					#!/bin/bash
# Basically runs jmeter, assuming the PATH is set to point to JMeter bin-dir (see Dockerfile)
# This script expects the standdard JMeter command parameters.

# Install jmeter plugins available on /plugins volume
if [ -d /plugins ]
then
    for plugin in /plugins/*.jar; do
        cp $plugin $(pwd)/lib/ext
    done;
fi

# Execute JMeter command
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 "START Running Jmeter on `date`"
echo "JVM_ARGS=${JVM_ARGS}"
echo "jmeter args=$@"

# Keep entrypoint simple: we must pass the standard JMeter arguments
jmeter $@
echo "END Running Jmeter on `date`"

				
			

Once we have Docker file, wrap_chromium_binary and entrypoint.sh file, we can use docker build command to create our docker image with the specific image name. And we can run the container based on created image using the below mentioned commands.

docker build . -f dockerfile -t jmeter5

The above command will take the docker file from current working directory and create the image with tag name “jmeter5”.

docker run -v “$PWD/Scripts/”:”/mnt/jmeter” jmeter5 -n -t /mnt/jmeter/TestPlan.jmx

Docker run command will create the container using the image “jmeter5” and it bind our local script location to containers “/mnt/jmeter” file system location. After binding the script location, it starts executing the jmeter jmx performance test script from mapped location.

Jmeter-Selenium Script Execution

As we know, using WebDriver Sampler, we can execute our selenium scripts in jmeter which helps us to calculate the end user response time during the load. And this type of execution takes more system resources to stimulate the real browsers which affects the system performance. To over come this issue, we can run the selenium based jmeter execution in containers and we can distribute the load in other system.

Once we create the Docker image using above mention Docker file code, it will download the chromium browser and chromium web driver. Wrap_chromium_binary file helps us to run the chromium browser for selenium web driver execution.

Based on the requirements, we can install other browsers like Firefox and supporting browser drivers for our execution. If we run the execution in headless browser mode, it will consume less system resources compared with UI mode of selenium web driver execution. Please find the complete code in github repo -https://github.com/kaiservignesh/Jmeter_Selenium_Docker

References

About Author

Vignesh M. is  a Senior Associate in Cognizant technology solutions with more the 8+ years of experience in various application performance testing and engineering activities. He works in various performance testing and monitoring tools. Currently focused on DevOps and SRE practices to automate the process and operations.

Leave a Comment