diff --git a/.github/workflows/gatling.yml b/.github/workflows/gatling.yml new file mode 100644 index 00000000..0b6bdda0 --- /dev/null +++ b/.github/workflows/gatling.yml @@ -0,0 +1,23 @@ +name: Gatling Load Test + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + distribution: 'zulu' + java-version: '21' + + - name: Load Test + run: ./mvnw gatling:test diff --git a/pom.xml b/pom.xml index e5e74636..d1cb4e15 100644 --- a/pom.xml +++ b/pom.xml @@ -23,6 +23,8 @@ UTF-8 jasper.JasperApplication -Djava.security.egd=file:/dev/./urandom -Xmx256m + 4.6.0 + 3.9.5 7.7.0 1.18.30 1.19.7 @@ -363,6 +365,12 @@ postgresql test + + io.gatling.highcharts + gatling-charts-highcharts + ${gatling.version} + test + com.tngtech.archunit archunit-junit5-api @@ -430,6 +438,28 @@ + + io.gatling + gatling-maven-plugin + + + + + io.gatling + gatling-maven-plugin + ${gatling-maven-plugin.version} + + true + ${project.basedir}/src/test/gatling/conf + ${project.basedir}/src/test/resources/gatling + ${project.basedir}src/test/java/simulations/computerdatabase + + simulations.computerdatabase.ComputerDatabaseSimulation + + + + + diff --git a/src/test/java/simulations/computerdatabase/ComputerDatabaseSimulation.java b/src/test/java/simulations/computerdatabase/ComputerDatabaseSimulation.java new file mode 100644 index 00000000..51e5a2a8 --- /dev/null +++ b/src/test/java/simulations/computerdatabase/ComputerDatabaseSimulation.java @@ -0,0 +1,89 @@ +package simulations.computerdatabase; + +import static io.gatling.javaapi.core.CoreDsl.*; +import static io.gatling.javaapi.http.HttpDsl.*; + +import io.gatling.javaapi.core.*; +import io.gatling.javaapi.http.*; + +import java.util.concurrent.ThreadLocalRandom; + +/** + * This sample is based on our official tutorials: + * + */ +public class ComputerDatabaseSimulation extends Simulation { + + HttpProtocolBuilder httpProtocol = + http.baseUrl("https://computer-database.gatling.io") + .acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") + .acceptLanguageHeader("en-US,en;q=0.5") + .acceptEncodingHeader("gzip, deflate") + .userAgentHeader( + "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/119.0"); + + FeederBuilder feeder = csv("search.csv").random(); + + ChainBuilder search = + // Execute actions sequentially + exec( + http("Home").get("/"), + pause(1), + feed(feeder), + http("Search") + .get("/computers?f=#{searchCriterion}") + .check(css("a:contains('#{searchComputerName}')", "href").saveAs("computerUrl")), + pause(1), + http("Select").get("#{computerUrl}").check(status().is(200)), + pause(1) + ); + + // Repeat is a loop resolved at RUNTIME + ChainBuilder browse = + // Note how we force the counter name, so we can reuse it + repeat(4, "i").on( + http("Page #{i}").get("/computers?p=#{i}"), + pause(1) + ); + + // Note we should be using a feeder here + // Let's demonstrate how we can retry: let's make the request fail randomly and retry a given + // number of times + ChainBuilder edit = + // Let's try at max 2 times + tryMax(2) + .on( + http("Form").get("/computers/new"), + pause(1), + http("Post") + .post("/computers") + .formParam("name", "Beautiful Computer") + .formParam("introduced", "2012-05-30") + .formParam("discontinued", "") + .formParam("company", "37") + .check( + status() + .is( + // We do a check on a condition that's been customized with + // a lambda. It will be evaluated every time a user executes + // the request. + session -> 200 + ThreadLocalRandom.current().nextInt(2) + ) + ) + ) + // If the chain didn't finally succeed, have the user exit the whole scenario + .exitHereIfFailed(); + + ScenarioBuilder users = scenario("Users").exec(search, browse); + ScenarioBuilder admins = scenario("Admins").exec(search, browse, edit); + + { + setUp( + users.injectOpen(rampUsers(10).during(10)), + admins.injectOpen(rampUsers(2).during(10)) + ).protocols(httpProtocol); + } +} diff --git a/src/test/resources/gatling/search.csv b/src/test/resources/gatling/search.csv new file mode 100644 index 00000000..fdeab9ed --- /dev/null +++ b/src/test/resources/gatling/search.csv @@ -0,0 +1,3 @@ +searchCriterion,searchComputerName +Macbook,MacBook Pro +eee,ASUS Eee PC 1005PE \ No newline at end of file