-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
176 additions
and
11 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.12.1") | ||
//The now built in dependencyTree task is usually enough | ||
//https://www.scala-sbt.org/1.x/docs/sbt-1.4-Release-Notes.html#sbt-dependency-graph+is+in-sourced | ||
//addDependencyTreePlugin | ||
//addDependencyTreePlugin | ||
|
||
addSbtPlugin("io.gatling" % "gatling-sbt" % "4.9.0") |
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
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
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,107 @@ | ||
import alpakka.file.uploader.DirectoryWatcher | ||
import io.gatling.core.Predef.* | ||
import io.gatling.core.action.Action | ||
import io.gatling.core.action.builder.ActionBuilder | ||
import io.gatling.core.session.Session | ||
import io.gatling.core.structure.{ScenarioBuilder, ScenarioContext} | ||
import io.gatling.core.util.NameGen | ||
|
||
import java.nio.file.{Files, Paths} | ||
import java.util.UUID | ||
import scala.concurrent.duration.* | ||
|
||
/** | ||
* Show the use of Gatling ActionBuilder to create a custom action | ||
* In our case to generate files to be picked up by the [[DirectoryWatcher]] | ||
* Since there are not HTTP requests issued by this simulation, | ||
* we do not get a report at the end | ||
* | ||
* Run from terminal: | ||
* sbt 'Gatling/testOnly DirectoryWatcherSimulation' | ||
*/ | ||
class DirectoryWatcherSimulation extends Simulation { | ||
private val rootDir = "/tmp/directory-watcher-simulation" | ||
private val uploadDir = Paths.get(rootDir, "upload") | ||
private val processedDir = Paths.get(rootDir, "processed") | ||
|
||
setupDirectories() | ||
val watcher = DirectoryWatcher(uploadDir, processedDir) | ||
|
||
private def setupDirectories(): Unit = { | ||
Files.createDirectories(Paths.get(rootDir)) | ||
|
||
if (!Files.exists(uploadDir)) { | ||
Files.createDirectories(uploadDir) | ||
println(s"Created temporary upload directory at: $uploadDir") | ||
} | ||
|
||
if (!Files.exists(processedDir)) { | ||
Files.createDirectories(processedDir) | ||
println(s"Created temporary processed directory at: $processedDir") | ||
} | ||
} | ||
|
||
// Custom action to generate unique files | ||
class FileGenerationAction(next: Action) extends Action with NameGen { | ||
|
||
override def name: String = genName("fileGeneration") | ||
|
||
override def execute(session: Session): Unit = { | ||
val uuid = UUID.randomUUID().toString | ||
val fileName = s"generated_file_$uuid.txt" | ||
val filePath = uploadDir.resolve(fileName) | ||
val content = "This is a generated file." | ||
|
||
Files.write(filePath, content.getBytes) | ||
println(s"Generated file: $filePath") | ||
|
||
next ! session | ||
} | ||
} | ||
|
||
// Custom action builder to wrap the FileGenerationAction | ||
class FileGenerationActionBuilder extends ActionBuilder { | ||
override def build(ctx: ScenarioContext, next: Action): Action = { | ||
new FileGenerationAction(next) | ||
} | ||
} | ||
|
||
class CountFilesProcessedAction(watcher: DirectoryWatcher, next: Action) extends Action { | ||
override def name: String = "CountFilesProcessedAction" | ||
|
||
override def execute(session: Session): Unit = { | ||
val processedFilesCount = watcher.countFilesProcessed() | ||
println(s"Total files processed: $processedFilesCount") | ||
next ! session | ||
} | ||
} | ||
|
||
class CountFilesProcessedActionBuilder(watcher: DirectoryWatcher) extends ActionBuilder { | ||
override def build(ctx: ScenarioContext, next: Action): Action = { | ||
new CountFilesProcessedAction(watcher, next) | ||
} | ||
} | ||
|
||
private val scn: ScenarioBuilder = scenario("DirectoryWatcher") | ||
.exec(new FileGenerationActionBuilder) | ||
.exec { session => | ||
println("File Generation completed") | ||
session | ||
} | ||
.exec(new CountFilesProcessedActionBuilder(watcher)) | ||
.exec { session => | ||
println("Scenario completed") | ||
session | ||
} | ||
|
||
setUp( | ||
scn.inject( | ||
nothingFor(2.seconds), | ||
atOnceUsers(2), | ||
rampUsers(10) during 10.seconds | ||
) | ||
).assertions( | ||
global.responseTime.max.lt(5000), | ||
global.successfulRequests.percent.is(100) | ||
).maxDuration(30.seconds) // For now stop like this | ||
} |
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,44 @@ | ||
|
||
import io.gatling.core.Predef.* | ||
import io.gatling.http.Predef.* | ||
|
||
import scala.concurrent.duration.* | ||
|
||
/** | ||
* Start [[akkahttp.ReverseProxy]] | ||
* Run this Simulation with: | ||
* sbt 'Gatling/testOnly ReverseProxySimulation' | ||
*/ | ||
class ReverseProxySimulation extends Simulation { | ||
val baseUrl = "http://127.0.0.1:8080" | ||
|
||
val httpProtocol = http | ||
.baseUrl(baseUrl) | ||
.acceptHeader("application/json") | ||
.userAgentHeader("Gatling") | ||
|
||
val scn = scenario("GatlingLocalClient") | ||
.exec(session => session.set("correlationId", 1)) | ||
.repeat(10) { | ||
exec( | ||
http("Local Mode Request") | ||
.get("/") | ||
.header("Host", "local") | ||
.header("X-Correlation-ID", session => s"1-${session("correlationId").as[Int]}") | ||
.check(status.is(200)) | ||
.check(header("X-Correlation-ID").saveAs("responseCorrelationId")) | ||
) | ||
.exec(session => { | ||
println(s"Got response for id: ${session("responseCorrelationId").as[String]}") | ||
session | ||
}) | ||
.exec(session => session.set("correlationId", session("correlationId").as[Int] + 1)) | ||
} | ||
|
||
setUp( | ||
scn.inject( | ||
atOnceUsers(10), | ||
rampUsers(50).during(30.seconds) | ||
) | ||
).protocols(httpProtocol) | ||
} |