Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
akoranne committed Sep 9, 2019
1 parent bd957d7 commit ce6da02
Show file tree
Hide file tree
Showing 9 changed files with 473 additions and 0 deletions.
38 changes: 38 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# inspired by https://github.com/hauptmedia/docker-jmeter and
# https://github.com/hhcordero/docker-jmeter-server/blob/master/Dockerfile
FROM alpine:3.9

MAINTAINER Just van den Broecke<just@justobjects.nl>

ARG JMETER_VERSION="5.1.1"
ENV JMETER_HOME /opt/apache-jmeter-${JMETER_VERSION}
ENV JMETER_BIN ${JMETER_HOME}/bin
ENV JMETER_DOWNLOAD_URL https://archive.apache.org/dist/jmeter/binaries/apache-jmeter-${JMETER_VERSION}.tgz

# Install extra packages
# See https://github.com/gliderlabs/docker-alpine/issues/136#issuecomment-272703023
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/dependencies \
&& curl -L --silent ${JMETER_DOWNLOAD_URL} > /tmp/dependencies/apache-jmeter-${JMETER_VERSION}.tgz \
&& mkdir -p /opt \
&& tar -xzf /tmp/dependencies/apache-jmeter-${JMETER_VERSION}.tgz -C /opt \
&& rm -rf /tmp/dependencies

# TODO: plugins (later)
# && unzip -oq "/tmp/dependencies/JMeterPlugins-*.zip" -d $JMETER_HOME

# Set global PATH such that "jmeter" command is found
ENV PATH $PATH:$JMETER_BIN

# Entrypoint has same signature as "jmeter" command
COPY entrypoint.sh /

WORKDIR ${JMETER_HOME}

ENTRYPOINT ["/entrypoint.sh"]
148 changes: 148 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# docker-jmeter
## Image on Docker Hub

Docker image for [Apache JMeter](http://jmeter.apache.org).
This Docker image can be run as the ``jmeter`` command.
Find Images of this repo on [Docker Hub](https://hub.docker.com/r/s1p19-cts/jmeter).

## Building

With the script [build.sh](build.sh) the Docker image can be build
from the [Dockerfile](Dockerfile) but this is not really necessary as
you may use your own ``docker build`` commandline. Or better: use one
of the pre-built Images from [Docker Hub](https://hub.docker.com/r/s1p19-cts/jmeter).

See end of this doc for more detailed build/run/test instructions (thanks to @wilsonmar!)

### Build Options

Build argumments (see [build.sh](build.sh)) with default values if not passed to build:

- **JMETER_VERSION** - JMeter version, default ``5.1.1``

## Running

The Docker image will accept the same parameters as ``jmeter`` itself, assuming
you run JMeter non-GUI with ``-n``.

There is a shorthand [run.sh](run.sh) command.
See [test.sh](test.sh) for an example of how to call [run.sh](run.sh).

## User Defined Variables

This is a standard facility of JMeter: settings in a JMX test script
may be defined symbolically and substituted at runtime via the commandline.
These are called JMeter User Defined Variables or UDVs.

See [test.sh](test.sh) and the [trivial test plan](tests/trivial/test-plan.jmx) for an example of UDVs passed to the Docker
image via [run.sh](run.sh).

See also: http://blog.novatec-gmbh.de/how-to-pass-command-line-properties-to-a-jmeter-testplan/

## Do it for real: detailed build/run/test

Contribution by @wilsonmar

1. In a Terminal/Command session, install Git, navigate/make a folder, then:

```
git clone https://github.com/s1p19-cts/docker-jmeter.git
cd docker-jmeter
```

1. Run the Build script to download dependencies, including the docker CLI:

```
./build.sh
```

If you view this file, the <strong>docker build</strong> command within the script is for a specific version of JMeter and implements the <strong>Dockerfile</strong> in the same folder.

If you view the Dockerfile, notice the `JMETER_VERSION` specified is the same as the one in the build.sh script. The FROM keyword specifies the Alpine operating system, which is very small (less of an attack surface). Also, no JMeter plug-ins are used.

At the bottom of the Dockerfile is the <strong>entrypoint.sh</strong> file. If you view it, that's where JVM memory settings are specified for <strong>jmeter</strong> before it is invoked. PROTIP: Such settings need to be adjusted for tests of more complexity.

The last line in the response should be:

<tt>Successfully tagged s1p19-cts/jmeter:5.1.1</tt>

1. Run the test script:

```
./test.sh
```

If you view the script, note it invokes the <strong>run.sh</strong> script file stored at the repo's root. View that file to see that it specifies docker image commands.

File and folder names specified in the test.sh script is reflected in the last line in the response for its run:

<pre>
==== HTML Test Report ====
See HTML test report in tests/trivial/report/index.html
</pre>

1. Switch to your machine's Folder program and navigate to the folder containing files which replaces files cloned in from GitHub:

```
cd tests/trivial
```

The files are:

* jmeter.log
* reports folder (see below)
* test-plan.jmx containing the JMeter test plan.
* test-plan.jtl containing statistics from the run displayed by the index.html file.


1. Navigate into the <strong>report</strong> folder and open the <strong>index.html</strong> file to pop up a browser window displaying the run report. On a Mac Terminal:

```
cd report
open index.html
```


## Specifics

The Docker image built from the
[Dockerfile](Dockerfile) inherits from the [Alpine Linux](https://www.alpinelinux.org) distribution:

> "Alpine Linux is built around musl libc and busybox. This makes it smaller
> and more resource efficient than traditional GNU/Linux distributions.
> A container requires no more than 8 MB and a minimal installation to disk
> requires around 130 MB of storage.
> Not only do you get a fully-fledged Linux environment but a large selection of packages from the repository."
See https://hub.docker.com/_/alpine/ for Alpine Docker images.

The Docker image will install (via Alpine ``apk``) several required packages most specificly
the ``OpenJDK Java JRE``. JMeter is installed by simply downloading/unpacking a ``.tgz`` archive
from http://mirror.serversupportforum.de/apache/jmeter/binaries within the Docker image.

A generic [entrypoint.sh](entrypoint.sh) is copied into the Docker image and
will be the script that is run when the Docker container is run. The
[entrypoint.sh](entrypoint.sh) simply calls ``jmeter`` passing all argumets provided
to the Docker container, see [run.sh](run.sh) script:

```
docker run --name ${NAME} -i -v ${WORK_DIR}:${WORK_DIR} -w ${WORK_DIR} ${IMAGE} $@
```

## X11 Error on Mac
If you are running on Mac, you will see following error
```
An error occurred:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
```

If you want to fix it on Mac, you need to do few things:

* Install the latest XQuartz X11 server and run it
* Activate the option ‘Allow connections from network clients’ in XQuartz settings
* Quit & restart XQuartz (to activate the setting)
* Run the docker script

## Credits

Thank you to all for their great contributions.
6 changes: 6 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash

JMETER_VERSION="5.1.1"

# Example build line
docker build --build-arg JMETER_VERSION=${JMETER_VERSION} -t "askxtreme/jmeter:${JMETER_VERSION}" .
28 changes: 28 additions & 0 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash
# Inspired from https://github.com/hhcordero/docker-jmeter-client
# 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 "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`"

# -n \
# -t "/tests/${TEST_DIR}/${TEST_PLAN}.jmx" \
# -l "/tests/${TEST_DIR}/${TEST_PLAN}.jtl"
# exec tail -f jmeter.log
# -D "java.rmi.server.hostname=${IP}" \
# -D "client.rmi.localport=${RMI_PORT}" \
# -R $REMOTE_HOSTS
39 changes: 39 additions & 0 deletions jmeter.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
2019-09-09 22:54:09,968 INFO o.a.j.u.JMeterUtils: Setting Locale to en_EN
2019-09-09 22:54:10,016 INFO o.a.j.JMeter: Loading user properties from: /opt/apache-jmeter-5.1.1/bin/user.properties
2019-09-09 22:54:10,019 INFO o.a.j.JMeter: Loading system properties from: /opt/apache-jmeter-5.1.1/bin/system.properties
2019-09-09 22:54:10,032 INFO o.a.j.JMeter: Copyright (c) 1998-2019 The Apache Software Foundation
2019-09-09 22:54:10,032 INFO o.a.j.JMeter: Version 5.1.1 r1855137
2019-09-09 22:54:10,033 INFO o.a.j.JMeter: java.version=1.8.0_212
2019-09-09 22:54:10,033 INFO o.a.j.JMeter: java.vm.name=OpenJDK 64-Bit Server VM
2019-09-09 22:54:10,033 INFO o.a.j.JMeter: os.name=Linux
2019-09-09 22:54:10,034 INFO o.a.j.JMeter: os.arch=amd64
2019-09-09 22:54:10,034 INFO o.a.j.JMeter: os.version=4.9.184-linuxkit
2019-09-09 22:54:10,034 INFO o.a.j.JMeter: file.encoding=UTF-8
2019-09-09 22:54:10,035 INFO o.a.j.JMeter: java.awt.headless=null
2019-09-09 22:54:10,035 INFO o.a.j.JMeter: Max memory =167772160
2019-09-09 22:54:10,036 INFO o.a.j.JMeter: Available Processors =2
2019-09-09 22:54:10,043 INFO o.a.j.JMeter: Default Locale=English (EN)
2019-09-09 22:54:10,044 INFO o.a.j.JMeter: JMeter Locale=English (EN)
2019-09-09 22:54:10,044 INFO o.a.j.JMeter: JMeterHome=/opt/apache-jmeter-5.1.1
2019-09-09 22:54:10,045 INFO o.a.j.JMeter: user.dir =/Users/m_677101/Downloads/cde/akoranne-docker-jmeter
2019-09-09 22:54:10,046 INFO o.a.j.JMeter: PWD =/Users/m_677101/Downloads/cde/akoranne-docker-jmeter
2019-09-09 22:54:10,047 INFO o.a.j.JMeter: IP: 172.17.0.2 Name: 73eaed8efd10 FullName: 73eaed8efd10
2019-09-09 22:54:10,087 ERROR o.a.j.JMeter: An error occurred:
java.awt.HeadlessException:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
at java.awt.GraphicsEnvironment.checkHeadless(GraphicsEnvironment.java:204) ~[?:1.8.0_212]
at java.awt.Window.<init>(Window.java:536) ~[?:1.8.0_212]
at java.awt.Frame.<init>(Frame.java:420) ~[?:1.8.0_212]
at java.awt.Frame.<init>(Frame.java:385) ~[?:1.8.0_212]
at javax.swing.SwingUtilities$SharedOwnerFrame.<init>(SwingUtilities.java:1762) ~[?:1.8.0_212]
at javax.swing.SwingUtilities.getSharedOwnerFrame(SwingUtilities.java:1837) ~[?:1.8.0_212]
at javax.swing.JWindow.<init>(JWindow.java:187) ~[?:1.8.0_212]
at javax.swing.JWindow.<init>(JWindow.java:139) ~[?:1.8.0_212]
at org.apache.jmeter.SplashScreen.<init>(SplashScreen.java:45) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.JMeter.startGui(JMeter.java:375) ~[ApacheJMeter_core.jar:5.1.1 r1855137]
at org.apache.jmeter.JMeter.start(JMeter.java:544) [ApacheJMeter_core.jar:5.1.1 r1855137]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
at org.apache.jmeter.NewDriver.main(NewDriver.java:253) [ApacheJMeter.jar:5.1.1 r1855137]
11 changes: 11 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash
#
# Run JMeter Docker image with options

NAME="jmeter"
IMAGE="askxtreme/jmeter:5.1.1"

# Finally run
docker stop ${NAME} > /dev/null 2>&1
docker rm ${NAME} > /dev/null 2>&1
docker run --name ${NAME} -i -v ${PWD}:${PWD} -w ${PWD} ${IMAGE} $@
35 changes: 35 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
#
# Test the JMeter Docker image using a trivial test plan.

# Example for using User Defined Variables with JMeter
# These will be substituted in JMX test script
# See also: http://stackoverflow.com/questions/14317715/jmeter-changing-user-defined-variables-from-command-line
export TARGET_HOST="www.map5.nl"
export TARGET_PORT="80"
export TARGET_PATH="/kaarten.html"
export TARGET_KEYWORD="Kaartdiensten"

T_DIR=tests/trivial

# Reporting dir: start fresh
R_DIR=${T_DIR}/report
rm -rf ${R_DIR} > /dev/null 2>&1
mkdir -p ${R_DIR}

/bin/rm -f ${T_DIR}/test-plan.jtl ${T_DIR}/jmeter.log > /dev/null 2>&1

./run.sh -Dlog_level.jmeter=DEBUG \
-JTARGET_HOST=${TARGET_HOST} -JTARGET_PORT=${TARGET_PORT} \
-JTARGET_PATH=${TARGET_PATH} -JTARGET_KEYWORD=${TARGET_KEYWORD} \
-n -t ${T_DIR}/test-plan.jmx -l ${T_DIR}/test-plan.jtl -j ${T_DIR}/jmeter.log \
-e -o ${R_DIR}

echo "==== jmeter.log ===="
cat ${T_DIR}/jmeter.log

echo "==== Raw Test Report ===="
cat ${T_DIR}/test-plan.jtl

echo "==== HTML Test Report ===="
echo "See HTML test report in ${R_DIR}/index.html"
Loading

0 comments on commit ce6da02

Please sign in to comment.