import com.typesafe.scalalogging.LazyLogging | |
import io.gatling.commons.validation.Validation | |
import io.gatling.core.scenario.Simulation | |
import io.gatling.core.Predef._ | |
import io.gatling.http.Predef._ | |
import io.gatling.http.request.builder.HttpRequestBuilder | |
import scala.concurrent.duration._ | |
class ReportingServiceTest extends Simulation with LazyLogging { | |
val userId = "someUserId" | |
val reportParameter = "ALL" | |
val numberOfUsers = 100 | |
val httpConf = http.baseURL("https://localhost:9000") | |
logger.info(s"Requesting $numberOfUsers reports from ${httpConf.baseUrls.mkString}") | |
val withApiKey = Map("key" -> "apiToken") | |
def reportRequestBody(userId: String, paremeter: String): String = | |
s""" | |
|{ | |
| "userId" : "$userId", | |
| "parameter": "$parameter" | |
|} | |
""".stripMargin | |
private val requestReport: HttpRequestBuilder = http("Request report generation") | |
.post("/report") | |
.queryParamMap(withApiKey) | |
.body(StringBody(reportRequestBody(userId, reportParameter))) | |
.asJSON | |
.check(status is 202) | |
.check(jsonPath("$..reportId").ofType[String].exists.saveAs("reportId")) | |
private def notGenerated(sess: Session): Validation[Boolean] = { | |
val reportInProgress = 202 | |
val generationStatus = sess("generationStatus").asOption[Int].getOrElse(reportInProgress) | |
logger.debug(s"Report generation status: $generationStatus") | |
generationStatus == reportInProgress | |
} | |
private val pollReportGenerationState = asLongAs(notGenerated)( | |
pause(100.millis) | |
.exec( | |
http("Poll report generation state") | |
.get("/report/${reportId}") | |
.queryParamMap(withApiKey) | |
.asJSON | |
.check(status saveAs ("generationStatus")) | |
) | |
) | |
private val getGeneratedReport: HttpRequestBuilder = | |
http("Get generated report") | |
.get("/report/${reportId}") | |
.queryParamMap(withApiKey) | |
.asJSON | |
.check(status is 200) | |
val generateReport = scenario("Generate report") | |
.exec(requestReport) | |
.exec(pollReportGenerationState) | |
.exec(getGeneratedReport) | |
setUp(generateReport.inject(atOnceUsers(numberOfUsers)).protocols(httpConf)) | |
.assertions(global.failedRequests.percent.lt(2)) | |
.maxDuration(200.seconds) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment