-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SCALA-568] Added gatling simulation
for testing against a server with a steady load
- Loading branch information
1 parent
918c612
commit 3d19540
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'` |
31 changes: 31 additions & 0 deletions
31
scala-gatling/src/test/scala/com/baeldung/gatling/ChainRequestsProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
scala-gatling/src/test/scala/com/baeldung/gatling/PeakLoadSimulation.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) | ||
} |
31 changes: 31 additions & 0 deletions
31
scala-gatling/src/test/scala/com/baeldung/gatling/ScenariosProvider.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |