Skip to content

Commit

Permalink
[SCALA-568] Added gatling simulation
Browse files Browse the repository at this point in the history
for testing against a server with a steady load
  • Loading branch information
bpstelios10 committed Jan 17, 2025
1 parent 918c612 commit 3d19540
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 0 deletions.
6 changes: 6 additions & 0 deletions scala-gatling/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
### Relevant Articles:
- [Testing With Gatling Using Scala]()

### Gatling Executions
compile tests: `sbt test:compile`
then run the simulation: `sbt 'Gatling/testOnly com.baeldung.gatling.PeakLoadSimulation'`
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.gatling

import io.gatling.core.Predef._
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef._
import io.gatling.http.request.builder.HttpRequestBuilder

object ChainRequestsProvider {

def simpleRequest(
requestName: String,
requestPath: String,
expectedResponseStatus: Int
): ChainBuilder = {
val request: HttpRequestBuilder = http(requestName)
.get(requestPath)
.check(status.is(expectedResponseStatus))
.check(bodyString.optional.saveAs("sBodyString"))

exec(session => session.markAsSucceeded)
.exec(request)
.doIf(_.isFailed) {
exec { session =>
println("***Failure on [" + requestPath + "] endpoint:")
print("Gatling Session Data: ")
println(session.attributes.get("sBodyString"))
session
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.gatling

import io.gatling.core.Predef.{details, _}
import com.baeldung.gatling.ChainRequestsProvider.simpleRequest
import com.baeldung.gatling.ScenariosProvider.getScenario

class PeakLoadSimulation extends Simulation {

setUp(
getScenario(
"getExistingEndpoint",
simpleRequest("request_status_endpoint", "/health/status", 200),
50,
10,
60
),
getScenario(
"nonExistingEndpoint",
simpleRequest("request_wrong_endpoint", "/health/status1", 200),
5,
10,
60
)
).assertions(
details("request_status_endpoint").successfulRequests.percent.gt(99.99),
details("request_status_endpoint").responseTime.percentile4.lt(20),
details("request_status_endpoint").requestsPerSec.gt(40),
details("request_wrong_endpoint").successfulRequests.percent.lt(1),
details("request_wrong_endpoint").responseTime.percentile4.lt(20)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.baeldung.gatling

import io.gatling.core.Predef._
import io.gatling.core.structure.{ChainBuilder, PopulationBuilder}
import io.gatling.http.Predef.http

import scala.language.postfixOps

object ScenariosProvider {

private val httpProtocol =
http.baseUrl("http://localhost:8080").disableCaching.disableFollowRedirect

def getScenario(
scenarioName: String,
request: ChainBuilder,
tps: Double,
rampUpSeconds: Int,
durationSeconds: Int
): PopulationBuilder = {
scenario(scenarioName)
.exec(request)
.inject(
rampUsersPerSec(0).to(tps).during(rampUpSeconds),
constantUsersPerSec(tps)
.during(durationSeconds - rampUpSeconds - rampUpSeconds),
rampUsersPerSec(tps).to(0).during(rampUpSeconds)
)
.protocols(httpProtocol)
}
}

0 comments on commit 3d19540

Please sign in to comment.