-
-
Save jebuselwyn/c6b30cfd6e8c5a84e7d8 to your computer and use it in GitHub Desktop.
Gatling Simulation snippet
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
class SampleSimulation extends Simulation { | |
val config = ConfigFactory.load().getConfig("airres") | |
val envConfig = config.getConfig("env." + System.getProperty("env")) | |
def setupSimulation(mode : String): SetUp = { | |
val testConfigs = config.getConfigList(mode) | |
val common_header = Map("SOAPAction" -> """""""") | |
val scenarioList = mutable.MutableList[PopulatedScenarioBuilder]() | |
for (i <- 0 to testConfigs.size - 1) { | |
val scenarioConfig = testConfigs.get(i) | |
val httpProtocol = http | |
.baseURL(envConfig.getString(scenarioConfig.getString("baseUrl"))) | |
.inferHtmlResources() | |
val contentType = scenarioConfig.getString("contentType") | |
val scenarioName = scenarioConfig.getString("name") | |
val postUrl = scenarioConfig.getString("endPoint") | |
val feedData = scenarioConfig.getString("feed-data") | |
val template = scenarioConfig.getString("template") | |
val userDistPct = Float.valueOf(scenarioConfig.getString("user-distribution-pct")) | |
val totalUsers = Integer.valueOf(envConfig.getString("ramp-up-users")) | |
var usersForScenario = Math.round((userDistPct / 100) * totalUsers) | |
usersForScenario = if (usersForScenario == 0) 1 else usersForScenario | |
val richConfig = Utils.RichConfig(scenarioConfig) | |
val scnTestDuration = if (richConfig.getOptionalString("test-duration") != None) richConfig.getOptionalString("test-duration").get else envConfig.getString("test-duration") | |
val scnPace = if (richConfig.getOptionalString("pace") != None) richConfig.getOptionalString("pace").get else envConfig.getString("pace") | |
val checks = mutable.MutableList[HttpCheck]() | |
checks += status.is(200) | |
populateAdditionalChecks(scenarioConfig, checks) | |
var scenarioBuilder: PopulatedScenarioBuilder = null | |
scenarioBuilder = scenario(scenarioName) | |
.feed(csv(feedData)) | |
.during(Integer.valueOf(scnTestDuration) seconds)( | |
pace(Integer.valueOf(scnPace) seconds) | |
.exec( | |
http(scenarioName) | |
.post(postUrl) | |
.body(ELFileBody(template)) | |
.headers(common_header) | |
.header("Content-Type", contentType) | |
.check(checks: _*) | |
) | |
).pause(Integer.valueOf(envConfig.getString("pause")) seconds).inject( | |
rampUsers(usersForScenario) | |
over (Integer.valueOf(envConfig.getString("ramp-up-duration")) seconds) | |
).protocols(httpProtocol) | |
scenarioList += scenarioBuilder | |
} | |
setUp(scenarioList.toList) | |
} | |
def populateAdditionalChecks(scenarioConfig: Config, checks: mutable.MutableList[HttpCheck]): Any = { | |
val shouldAssert = envConfig.getBoolean("enable-assertions") | |
val scnEnableAssertions = scenarioConfig.getBoolean("enable-assertions") | |
if (shouldAssert && scnEnableAssertions) { | |
val assertType = scenarioConfig.getString("assert-type") | |
if(assertType.equalsIgnoreCase("xpath")){ | |
val assertXpath = "" + scenarioConfig.getString("assert-xpath") + "" | |
val assertXpathNSKeyList = scenarioConfig.getString("assert-xpath-namespaces-key").split(",").toList | |
val assertXpathNSValList = scenarioConfig.getString("assert-xpath-namespaces-val").split(",").toList | |
val assertXpathNSList = assertXpathNSKeyList zip assertXpathNSValList | |
val assertValue = "" + scenarioConfig.getString("assert-value") + "" | |
if(assertValue.contains("|")){ | |
val assertValues = scenarioConfig.getString("assert-value").split("\\|").toList | |
checks += xpath(assertXpath, assertXpathNSList).in(assertValues) | |
} | |
else{ | |
checks += xpath(assertXpath, assertXpathNSList).is(assertValue) | |
} | |
} | |
else if (assertType.equalsIgnoreCase("xpath-count")){ | |
val assertXpath = "" + scenarioConfig.getString("assert-xpath") + "" | |
val assertXpathNSKeyList = scenarioConfig.getString("assert-xpath-namespaces-key").split(",").toList | |
val assertXpathNSValList = scenarioConfig.getString("assert-xpath-namespaces-val").split(",").toList | |
val assertXpathNSList = assertXpathNSKeyList zip assertXpathNSValList | |
val assertCount = scenarioConfig.getInt("assert-count") | |
checks += xpath(assertXpath, assertXpathNSList).count.greaterThanOrEqual(assertCount) | |
} | |
else if (assertType.equalsIgnoreCase("jsonp")){ | |
val assertJsonp = "" + scenarioConfig.getString("assert-jsonp") + "" | |
val assertValue = "" + scenarioConfig.getString("assert-value") + "" | |
if(assertValue.contains("|")){ | |
val assertValueList = scenarioConfig.getString("assert-value").split("\\|").toList | |
checks += jsonPath(assertJsonp).in(assertValueList) | |
} | |
else{ | |
checks += jsonPath(assertJsonp).is(assertValue) | |
} | |
} | |
else if (assertType.equalsIgnoreCase("jsonp-count")){ | |
val assertJsonp = "" + scenarioConfig.getString("assert-jsonp") + "" | |
val assertCount = scenarioConfig.getInt("assert-count") | |
checks += jsonPath(assertJsonp).count.greaterThanOrEqual(assertCount) | |
} | |
else{ | |
throw new Exception ("Unsupported assertion type =>"+ assertType) | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment