Skip to content

Instantly share code, notes, and snippets.

@mburrows02
Last active April 18, 2023 23:23
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mburrows02/b3b98687ac8acdded39e to your computer and use it in GitHub Desktop.
Save mburrows02/b3b98687ac8acdded39e to your computer and use it in GitHub Desktop.
Gatling simulation with dynamic scenarios and injection profiles
package test
import io.gatling.core.Predef._
import io.gatling.core.structure.PopulatedScenarioBuilder
import io.gatling.core.controller.inject.InjectionStep
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.concurrent.duration._
import scala.collection.mutable.ArraySeq
import org.json.JSONArray;
import org.json.JSONObject;
class CustomSimulation extends Simulation {
val baseURL = "http://www.example.com"
val httpConf = http.baseURL(baseURL)
val rawTestList = System.getProperty("testCases")
val loginScn = scenario("Login").exec(session => {
println("Logging in...")
session
})
val registerScn = scenario("Register").exec(session => {
println("Registering...")
session
})
val scenarioNames = Map (
"Login" -> loginScn,
"Register" -> registerScn
)
def scnList() : Seq[PopulatedScenarioBuilder] = {
var testList = new JSONArray(rawTestList)
var scnList = new ArraySeq[PopulatedScenarioBuilder](testList.length())
for(i <- 0 until testList.length()) {
//For each scenario
var testCase = testList.getJSONObject(i)
val injectionSteps = testCase.getJSONArray("inject")
var injectStepList = new ArraySeq[InjectionStep](injectionSteps.length())
for(j <- 0 until injectionSteps.length()) {
//For each injection step
var step = injectionSteps.getJSONObject(j)
var stepType = step.getString("type")
var stepArgs = step.getJSONArray("args")
injectStepList(j) = stepType match {
case "rampUsers" => rampUsers(stepArgs.getInt(0)) over(stepArgs.getInt(1))
case "heavisideUsers" => heavisideUsers(stepArgs.getInt(0)) over(stepArgs.getInt(1))
case "atOnceUsers" => atOnceUsers(stepArgs.getInt(0))
case "constantUsersPerSec" => constantUsersPerSec(stepArgs.getInt(0)) during(stepArgs.getInt(1))
case "rampUsersPerSec" => rampUsersPerSec(stepArgs.getInt(0)) to(stepArgs.getInt(1)) during(stepArgs.getInt(2))
case "nothingFor" => nothingFor(stepArgs.getInt(0))
}
}
scnList(i) = scenarioNames(testCase.getString("case")).inject(injectStepList:_*)
}
scnList
}
setUp(scnList:_*).protocols(httpConf)
}
package test
import io.gatling.core.Predef._
import io.gatling.core.structure.PopulatedScenarioBuilder
import io.gatling.core.controller.inject.InjectionStep
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import scala.concurrent.duration._
import scala.collection.mutable.ArraySeq
import org.json.JSONArray;
import org.json.JSONObject;
class CustomSimulation extends Simulation {
val baseURL = "http://www.example.com"
val httpConf = http.baseURL(baseURL)
val rawTestList = System.getProperty("testCases")
val loginScn = scenario("Login").exec(session => {
println("Logging in...")
session
})
val registerScn = scenario("Register").exec(session => {
println("Registering...")
session
})
val scenarioNames = Map (
"Login" -> loginScn,
"Register" -> registerScn
)
def getInjectionStep(stepType:String, stepArgs:JSONArray) : InjectionStep = {
stepType match {
case "rampUsers" => rampUsers(stepArgs.getInt(0)) over(stepArgs.getInt(1))
case "heavisideUsers" => heavisideUsers(stepArgs.getInt(0)) over(stepArgs.getInt(1))
case "atOnceUsers" => atOnceUsers(stepArgs.getInt(0))
case "constantUsersPerSec" => constantUsersPerSec(stepArgs.getInt(0)) during(stepArgs.getInt(1))
case "rampUsersPerSec" => rampUsersPerSec(stepArgs.getInt(0)) to(stepArgs.getInt(1)) during(stepArgs.getInt(2))
case "nothingFor" => nothingFor(stepArgs.getInt(0))
}
}
def scnList() : Seq[PopulatedScenarioBuilder] = {
var testList = new JSONArray(rawTestList)
var scnList = new ArraySeq[PopulatedScenarioBuilder](testList.length())
for(i <- 0 until testList.length()) {
//For each scenario
var testCase = testList.getJSONObject(i)
val injectionSteps = testCase.getJSONArray("inject")
var injectStepList = new ArraySeq[InjectionStep](injectionSteps.length())
for(j <- 0 until injectionSteps.length()) {
//For each injection step
var step = injectionSteps.getJSONObject(j)
var stepType = step.getString("type")
var stepArgs = step.getJSONArray("args")
injectStepList(j) = getInjectionStep(stepType, stepArgs)
}
scnList(i) = scenarioNames(testCase.getString("case")).inject(injectStepList:_*)
}
scnList
}
setUp(scnList:_*).protocols(httpConf)
}
@yoeunes
Copy link

yoeunes commented Dec 21, 2017

This is so cool, how do you pass the testCases property to my script ? whats it look like ? thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment