base test framework

This commit is contained in:
Greg Gauthier 2024-10-25 23:12:59 +01:00
parent 1d2b605062
commit 19b58a3fd1
21 changed files with 619 additions and 0 deletions

8
.gitignore vendored
View File

@ -1,3 +1,11 @@
.idea/
.vscode/
.metals/
target/
dist/
log/
out/*.zip
out/*/**
*.iml
src/test/scala/com/williamslea/engage/recordings/*.scala

19
Dockerfile Normal file
View File

@ -0,0 +1,19 @@
FROM maven:3.9.8-eclipse-temurin-17-alpine
RUN apk add -Uuv python3 less py-pip openssl tzdata zip
RUN apk list -I
RUN cp /usr/share/zoneinfo/Europe/London /etc/localtime
RUN apk --purge -v del py-pip && rm /var/cache/apk/*
WORKDIR /build
COPY pom.xml .
COPY src/ /build/src
COPY docker-entrypoint-script.sh .
RUN mvn clean install
ENTRYPOINT ["./docker-entrypoint-script.sh"]

76
README.md Normal file
View File

@ -0,0 +1,76 @@
# Simple Gatling Load Test Framework
This repository is a proof-of-concept demonstration of the Gatling load testing tool, in a standard Java/Scala test framework.
Here is what you need to run one of the load simulations provided in this demo:
## Requirements
* Java 17 or better
* Apache Maven 3.9 or better
* Docker 27 or better (for the docker image runs)
The maven tool will install all other necessary dependencies from Maven Central, including:
* Scala 3 Libraries
* Gatling SDK
* Scala and Gatling runner plugins
## Running The Tests
There are two ways to run a test:
* As a docker container
* On the native environment of your local machine
### Docker Runs
To run a test as a docker container, use the `docker-run` scripts. A version has been
created for both Windows and Linux/Mac host environments.
The `docker-run` script takes arguments that allow you to control the load profile
of the test. It also takes arguments that enable the use of different test users.
Here is example usage:
Powershell
```powershell
.\docker-run.ps1 -test "TestClassName" -user "testuser" -pswd "password1234" -count 10 -ramp_in_minutes 5 -duration_in_minutes 15
```
Bash
```bash
./docker-run.sh -test "TestClassName" -user "testuser" -pswd "password1234" -count 10 -ramp_in_minutes 5 -duration_in_minutes 15
```
### Local Runs
To run a test from your workstation, use the `local-run` scripts. A version has been created for both Windows and Linux/Mac host environments.
The `local-run` script takes arguments that allow you to control the load profile of the test. It also takes arguments that enable the use of different test users. Here is example usage:
Powershell
```powershell
.\local-run.ps1 -test "TestClassName" -user "testuser" -pswd "password1234" -count 10 -ramp_in_minutes 5 -duration_in_minutes 15
```
Bash
```bash
./local-run.sh -test "TestClassName" -user "testuser" -pswd "password1234" -count 10 -ramp_in_minutes 5 -duration_in_minutes 15
```
Arguments for both the docker and local scripts:
* The `-test` argument requires you to supply the classname of the test you want to execute.
These can be found in the `com.organization.tests` classpath in your source directory. The
basic naming convention is Java-style. However, an arbitrary test id has been attached to the
beginning of the test name, to make it easier to locate in the test plan document.
* `-user` and `-pswd` should just be whatever test user credentials will be used for the test session user population.
* `-count` is the total number of test users to be instantiated during the test. It should be
noted that the total number of users *may not be* the *peak* number of users. That will
depend upon the next two arguments.
* `-ramp_in_minutes` is the number of minutes over which your user population will inject itself into the scenario
* `-duration_in_minutes` is the number of minutes the scenario should sustain the total population in the test

13
aws-run.ps1 Normal file
View File

@ -0,0 +1,13 @@
param ($test, $user, $pswd, $count, $ramp_in_minutes, $duration_in_minutes)
$env:TEST = $test
$env:TEST_USER = $user
$env:TEST_PWD = $pswd
$env:USER_COUNT = $count
$env:RAMP_DURATION = $ramp_in_minutes
$env:TEST_DURATION = $duration_in_minutes
$classname="com.organization.tests.${test}"
mvn install
mvn gatling:test "-DclassName=${classname}"

25
buildspec.yml Normal file
View File

@ -0,0 +1,25 @@
version: 0.2
env:
shell: powershell.exe
secrets-manager:
test_user: "aws-secrets:test_user"
test_pswd: "aws:test_pswd"
git-credential-helper: no
proxy:
upload-artifacts: no
logs: yes
phases:
build:
on-failure: ABORT
commands:
- $env:BASE_URL=${BASE_URL}
- .\aws-run.ps1 -test "${TEST_NAME}" -user ${test_user} -pswd ${test_pswd} -count ${USER_COUNT} -ramp_in_minutes ${RAMP} -duration_in_minutes ${DURATION}
artifacts:
files:
- 'target/gatling/**/*'
name: gatling-test-report-$(date +%Y-%m-%d)
discard-paths: no
enable-symlinks: yes

View File

@ -0,0 +1,26 @@
#!/usr/bin/env sh
# NOTE: This script is meant to run as the entry point script for the docker container.
# Running this directly from the host system may produce unexpected results
#pipx install awscli --force
export PATH="${PATH}":/root/.local/bin
#which aws
# mvn clean install
mvn gatling:test '-DclassName=com.organization.tests.'"${TEST}"''
cid="$(awk -F/ '{print $NF}' /proc/1/cpuset)"
#cid=$(cat /dev/urandom |tr -dc 'a-zA-Z0-9'|fold -w 20|head -n 1)
echo "CID: ${cid}"
# REPORT_BUCKET="REPORT_BUCKET"
for _dir in /build/target/gatling/*/
do
zip -r test_report_"${TEST}"_"${cid}".zip "${_dir}"
# zip -r test_report.zip "${_dir}" # will be copied to /out later
# aws s3 cp --recursive --dryrun "${_dir}" s3://"${REPORT_BUCKET}"/test_report_"${TEST}"_"${cid}"/
done

11
docker-run.ps1 Normal file
View File

@ -0,0 +1,11 @@
param ($test, $user, $pswd, $count, $ramp_in_minutes, $duration_in_minutes)
$env:BASE_URL="subdomain.organization.com"
$env:DOCKER_IMAGE="gatling-load-test"
$env:DOCKER_CONTAINER="gatling_load_test"
docker rm ${DOCKER_CONTAINER}
docker build -t ${DOCKER_IMAGE}:1.0 .
docker run -e TEST="$test" -e TEST_USER="$user" -e TEST_PWD="$pswd" -e BASE_URL=$env:BASE_URL -e USER_COUNT="$count" -e RAMP_DURATION="$ramp_in_minutes" -e TEST_DURATION="$duration_in_minutes" --name ${DOCKER_CONTAINER} -p 8000:8000 ${DOCKER_IMAGE}:1.0
$cid = docker ps --no-trunc -aqf name=${DOCKER_CONTAINER}
docker cp ${cid}:/build/test_report_"${test}"_"${cid}".zip .\out\

65
docker-run.sh Normal file
View File

@ -0,0 +1,65 @@
#!/usr/bin/env sh
# ############################################
# This is the unix/linux version of the windows script for running the load tests
# via the docker container
# ############################################
# Initialize variables
export URL="subdomain.organization.com"
DOCKER_IMAGE="gatling-load-test"
DOCKER_CONTAINER="gatling_load_test"
test=""
user=""
pswd=""
count=""
ramp=""
duration=""
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-test)
test="$2"
shift 2
;;
-user)
user="$2"
shift 2
;;
-pswd)
pswd="$2"
shift 2
;;
-count)
count="$2"
shift 2
;;
-ramp_in_minutes)
ramp="$2"
shift 2
;;
-duration_in_minutes)
duration="$2"
shift 2
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
# Check mandatory arguments
if [ -z "$test" ] || [ -z "$user" ] || [ -z "$pswd" ] || [ -z "$count" ] || [ -z "$ramp" ] || [ -z "$duration" ]; then
echo "Usage: $0 -test \"testname\" -user \"userid\" -pswd \"password\" -count n -ramp_in_minutes n -duration_in_minutes n"
exit 1
fi
docker rm ${DOCKER_CONTAINER}
docker build -t ${DOCKER_IMAGE}:1.0 .
docker run -e TEST="${test}" -e TEST_USER="${user}" -e TEST_PWD="${pswd}" -e BASE_URL="${URL}" -e USER_COUNT="$count" -e RAMP_DURATION="$ramp" -e TEST_DURATION="$duration" --name ${DOCKER_CONTAINER} -p 8000:8000 ${DOCKER_IMAGE}:1.0
cid=$(docker ps --no-trunc -aqf name=${DOCKER_CONTAINER})
docker cp "${cid}":/build/test_report.zip ./out/test_report_"${test}"_"${cid}".zip

15
local-run.ps1 Normal file
View File

@ -0,0 +1,15 @@
param ($test, $user, $pswd, $count, $ramp_in_minutes, $duration_in_minutes)
$env:BASE_URL="subdomain.organization.com"
$env:TEST = $test
$env:TEST_USER = $user
$env:TEST_PWD = $pswd
$env:USER_COUNT = $count
$env:RAMP_DURATION = $ramp_in_minutes
$env:TEST_DURATION = $duration_in_minutes
$classname="com.organization.tests.${test}"
mvn install
mvn gatling:test "-DclassName=${classname}"

63
local-run.sh Normal file
View File

@ -0,0 +1,63 @@
#!/usr/bin/env bash
# Initialize variables
url="https://subdomain.organization.com"
test=""
user=""
pswd=""
count=""
ramp=""
duration=""
# Parse arguments
while [ $# -gt 0 ]; do
case "$1" in
-test)
test="$2"
shift 2
;;
-user)
user="$2"
shift 2
;;
-pswd)
pswd="$2"
shift 2
;;
-count)
count="$2"
shift 2
;;
-ramp_in_minutes)
ramp="$2"
shift 2
;;
-duration_in_minutes)
duration="$2"
shift 2
;;
*)
echo "Unknown argument: $1"
exit 1
;;
esac
done
echo $test
# Check mandatory arguments
if [ -z "$test" ] || [ -z "$user" ] || [ -z "$pswd" ] || [ -z "$count" ] || [ -z "$ramp" ] || [ -z "$duration" ]; then
echo "Usage: $0 -test \"testname\" -user \"userid\" -pswd \"password\" -count n -ramp_in_minutes n -duration_in_minutes n"
exit 1
fi
export BASE_URL="${url}"
export TEST_USER="${user}"
export TEST_PWD="${pswd}"
export USER_COUNT="${count}"
export RAMP_DURATION="${ramp}"
export TEST_DURATION="${duration}"
mvn clean install
mvn gatling:test "-DclassName=com.organization.tests.${test}"

1
out/placeholder.txt Normal file
View File

@ -0,0 +1 @@
PRESERVE THE OUT DIRECTORY, BUT NOT THE CONTENTS

114
pom.xml Normal file
View File

@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.organization</groupId>
<artifactId>gatling-sample-framework</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<encoding>UTF-8</encoding>
<!-- minimum SCALA version -->
<scala.version>3.5.1</scala.version>
<scala-maven-plugin.version>4.9.2</scala-maven-plugin.version>
<!-- minimum GATLING version -->
<gatling.version>3.6.1</gatling.version>
<gatling-maven-plugin.version>4.10.0</gatling-maven-plugin.version>
<!-- provides simulation classname on command line, for gatling -->
<className></className>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala3-library -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-library_3</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala3-compiler -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-compiler_3</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.scala-lang/scala3-sbt-bridge -->
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala3-sbt-bridge</artifactId>
<version>3.5.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/net.alchim31.maven/scala-maven-plugin -->
<dependency>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
</dependency>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-app</artifactId>
<version>${gatling.version}</version>
</dependency>
<dependency>
<groupId>io.gatling</groupId>
<artifactId>gatling-recorder</artifactId>
<version>${gatling.version}</version>
</dependency>
</dependencies>
<build>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<pluginManagement>
<plugins>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<!-- https://mvnrepository.com/artifact/net.alchim31.maven/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala-maven-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling-maven-plugin.version}</version>
<configuration>
<simulationClass>${className}</simulationClass>
</configuration>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,127 @@
#########################
# Gatling Configuration #
#########################
# This file contains all the settings configurable for Gatling with their default values
gatling {
core {
#outputDirectoryBaseName = "" # The prefix for each simulation result folder (then suffixed by the report generation timestamp)
#runDescription = "" # The description for this simulation run, displayed in each report
#encoding = "utf-8" # Encoding to use throughout Gatling for file and string manipulation
#simulationClass = "" # The FQCN of the simulation to run (when used in conjunction with noReports, the simulation for which assertions will be validated)
#elFileBodiesCacheMaxCapacity = 200 # Cache size for request body EL templates, set to 0 to disable
#rawFileBodiesCacheMaxCapacity = 200 # Cache size for request body Raw templates, set to 0 to disable
#rawFileBodiesInMemoryMaxSize = 1000 # Below this limit, raw file bodies will be cached in memory
#pebbleFileBodiesCacheMaxCapacity = 200 # Cache size for request body Peeble templates, set to 0 to disable
#feederAdaptiveLoadModeThreshold = 100 # File size threshold (in MB). Below load eagerly in memory, above use batch mode with default buffer size
#shutdownTimeout = 10000 # Milliseconds to wait for the actor system to shutdown
extract {
regex {
#cacheMaxCapacity = 200 # Cache size for the compiled regexes, set to 0 to disable caching
}
xpath {
#cacheMaxCapacity = 200 # Cache size for the compiled XPath queries, set to 0 to disable caching
}
jsonPath {
#cacheMaxCapacity = 200 # Cache size for the compiled jsonPath queries, set to 0 to disable caching
}
css {
#cacheMaxCapacity = 200 # Cache size for the compiled CSS selectors queries, set to 0 to disable caching
}
}
directory {
#simulations = "" # If set, directory where simulation classes are located
#resources = "" # If set, directory where resources, such as feeder files and request bodies, are located
#reportsOnly = "" # If set, name of report folder to look for in order to generate its report
#binaries = "" # If set, name of the folder where compiles classes are located: Defaults to GATLING_HOME/target.
#results = results # Name of the folder where all reports folder are located
}
}
socket {
#connectTimeout = 10000 # Timeout in millis for establishing a TCP socket
#tcpNoDelay = true
#soKeepAlive = false # if TCP keepalive configured at OS level should be used
#soReuseAddress = false
}
netty {
#useNativeTransport = true # if Netty native transport should be used instead of Java NIO
#allocator = "pooled" # switch to unpooled for unpooled ByteBufAllocator
#maxThreadLocalCharBufferSize = 200000 # Netty's default is 16k
}
ssl {
#useOpenSsl = true # if OpenSSL should be used instead of JSSE (only the latter can be debugged with -Djava.net.debug=ssl)
#useOpenSslFinalizers = false # if OpenSSL contexts should be freed with Finalizer or if using RefCounted is fine
#handshakeTimeout = 10000 # TLS handshake timeout in millis
#useInsecureTrustManager = true # Use an insecure TrustManager that trusts all server certificates
#enabledProtocols = [] # Array of enabled protocols for HTTPS, if empty use Netty's defaults
#enabledCipherSuites = [] # Array of enabled cipher suites for HTTPS, if empty enable all available ciphers
#sessionCacheSize = 0 # SSLSession cache size, set to 0 to use JDK's default
#sessionTimeout = 0 # SSLSession timeout in seconds, set to 0 to use JDK's default (24h)
#enableSni = true # When set to true, enable Server Name indication (SNI)
keyStore {
#type = "" # Type of SSLContext's KeyManagers store
#file = "" # Location of SSLContext's KeyManagers store
#password = "" # Password for SSLContext's KeyManagers store
#algorithm = "" # Algorithm used SSLContext's KeyManagers store
}
trustStore {
#type = "" # Type of SSLContext's TrustManagers store
#file = "" # Location of SSLContext's TrustManagers store
#password = "" # Password for SSLContext's TrustManagers store
#algorithm = "" # Algorithm used by SSLContext's TrustManagers store
}
}
charting {
#noReports = false # When set to true, don't generate HTML reports
#maxPlotPerSeries = 1000 # Number of points per graph in Gatling reports
#useGroupDurationMetric = false # Switch group timings from cumulated response time to group duration.
indicators {
#lowerBound = 800 # Lower bound for the requests' response time to track in the reports and the console summary
#higherBound = 1200 # Higher bound for the requests' response time to track in the reports and the console summary
#percentile1 = 50 # Value for the 1st percentile to track in the reports, the console summary and Graphite
#percentile2 = 75 # Value for the 2nd percentile to track in the reports, the console summary and Graphite
#percentile3 = 95 # Value for the 3rd percentile to track in the reports, the console summary and Graphite
#percentile4 = 99 # Value for the 4th percentile to track in the reports, the console summary and Graphite
}
}
http {
#fetchedCssCacheMaxCapacity = 200 # Cache size for CSS parsed content, set to 0 to disable
#fetchedHtmlCacheMaxCapacity = 200 # Cache size for HTML parsed content, set to 0 to disable
#perUserCacheMaxCapacity = 200 # Per virtual user cache size, set to 0 to disable
#warmUpUrl = "https://gatling.io" # The URL to use to warm-up the HTTP stack (blank means disabled)
#enableGA = true # Very light Google Analytics (Gatling and Java version), please support
#pooledConnectionIdleTimeout = 60000 # Timeout in millis for a connection to stay idle in the pool
requestTimeout = 240000
#enableHostnameVerification = false # When set to true, enable hostname verification: SSLEngine.setHttpsEndpointIdentificationAlgorithm("HTTPS")
dns {
#queryTimeout = 5000 # Timeout in millis of each DNS query in millis
#maxQueriesPerResolve = 6 # Maximum allowed number of DNS queries for a given name resolution
}
}
jms {
#replyTimeoutScanPeriod = 1000 # scan period for timedout reply messages
}
data {
#writers = [console, file] # The list of DataWriters to which Gatling write simulation data (currently supported : console, file, graphite)
console {
#light = false # When set to true, displays a light version without detailed request stats
#writePeriod = 5 # Write interval, in seconds
}
file {
#bufferSize = 8192 # FileDataWriter's internal data buffer size, in bytes
}
leak {
#noActivityTimeout = 30 # Period, in seconds, for which Gatling may have no activity before considering a leak may be happening
}
graphite {
#light = false # only send the all* stats
#host = "localhost" # The host where the Carbon server is located
#port = 2003 # The port to which the Carbon server listens to (2003 is default for plaintext, 2004 is default for pickle)
#protocol = "tcp" # The protocol used to send data to Carbon (currently supported : "tcp", "udp")
#rootPathPrefix = "gatling" # The common prefix of all metrics sent to Graphite
#bufferSize = 8192 # Internal data buffer size, in bytes
#writePeriod = 1 # Write period, in seconds
}
}
}

View File

@ -0,0 +1,12 @@
import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder
object Engine extends App {
val props = new GatlingPropertiesBuilder()
.resourcesDirectory(IDEPathHelper.mavenResourcesDirectory.toString)
.resultsDirectory(IDEPathHelper.resultsDirectory.toString)
.binariesDirectory(IDEPathHelper.mavenBinariesDirectory.toString)
Gatling.fromMap(props.build)
}

View File

@ -0,0 +1,14 @@
import java.nio.file.{Path, Paths}
object IDEPathHelper {
private val projectRootDir = Paths.get(getClass.getClassLoader.getResource("gatling.conf").toURI).getParent.getParent.getParent
private val mavenTargetDirectory = projectRootDir.resolve("target")
private val mavenSrcTestDirectory = projectRootDir.resolve("src").resolve("test")
val mavenSourcesDirectory: Path = mavenSrcTestDirectory.resolve("scala")
val mavenResourcesDirectory: Path = mavenSrcTestDirectory.resolve("resources")
val mavenBinariesDirectory: Path = mavenTargetDirectory.resolve("test-classes")
val resultsDirectory: Path = mavenTargetDirectory.resolve("gatling")
val recorderConfigFile: Path = mavenResourcesDirectory.resolve("recorder.conf")
}

View File

@ -0,0 +1,12 @@
import io.gatling.recorder.GatlingRecorder
import io.gatling.recorder.config.RecorderPropertiesBuilder
object Recorder extends App {
val props = new RecorderPropertiesBuilder()
.simulationsFolder(IDEPathHelper.mavenSourcesDirectory.toString)
.resourcesFolder(IDEPathHelper.mavenResourcesDirectory.toString)
.simulationPackage("com.organization")
GatlingRecorder.fromMap(props.build, Some(IDEPathHelper.recorderConfigFile))
}

View File

@ -0,0 +1,14 @@
package com.organization
import scala.util.Properties.envOrElse
object Environment {
val letsEncryptUrl: String = "http://r10.o.lencr.org"
val baseUrl: String = envOrElse("BASE_URL", "subdomain.organization.com")
val testUser: String = envOrElse("TEST_USER", "undefined")
val testPwd: String = envOrElse("TEST_PWD", "undefined")
val fullUrl: String = "https://"+baseUrl
val userCount: String = envOrElse("USER_COUNT", "1")
val rampDuration: String = envOrElse("RAMP_DURATION", "1")
val testDuration: String = envOrElse("TEST_DURATION", "1")
}

View File

@ -0,0 +1 @@
# preserve directory in git

View File

@ -0,0 +1 @@
# preserve directory in git

View File

@ -0,0 +1 @@
# preserve directory in git

View File

@ -0,0 +1 @@
# preserve directory in git