Skip to content

Instantly share code, notes, and snippets.

@vanclist
Created March 31, 2017 16:54
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save vanclist/2cd8914b95c102f773c2440615abf4d7 to your computer and use it in GitHub Desktop.
Gatling tests setup for common tasks
package com.example.loadtest
import io.gatling.core.Predef._
import io.gatling.core.controller.inject.InjectionStep
import io.gatling.http.Predef._
import scala.concurrent.duration._
sealed trait CommonVars {
val nominalResponseTime = Integer.getInteger("nominalResponseTime", 1200).toInt
val partDuration = Integer.getInteger("partDuration", 60).toInt seconds
val lowScenariosPerSecond = 50
val targetScenarios = Integer.getInteger("targetRPS", 100).toInt
val maxScenarios = targetScenarios * 2
val warmUpDuration = partDuration / 2
val mainLoadDuration = partDuration * 4
val maxConnections = Integer.getInteger("maxConnections", 100).toInt
val userAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 9_3_2 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13F69 Safari/601.1"
}
abstract class BaseLoadTest extends Simulation with CommonVars {
val url = ConfigLoader.Inventory.baseUrl
val httpConfig = http
.baseURL(s"http://$url")
.acceptHeader("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
.maxConnectionsPerHost(maxConnections)
.shareConnections
// use throttle only if you have very high peaks just after rampUsersPerSec
val injectionSteps: Seq[InjectionStep] = Seq(
rampUsersPerSec(1) to targetScenarios during partDuration, // warm up
constantUsersPerSec(targetScenarios) during mainLoadDuration
)
}
class RandomLoadTest extends BaseLoadTest {
val randomParamFeeder = Seq(13, 1337, 8888).toArray.map(r => Map("random_param" -> r)).random
val refererFeeder = Seq("http://washingtonpost.com", "http://song.urbanmusicdaily.me", "http://tvguide.com").toArray.map(r => Map("referer" -> r)).random
val ipFeeder = Seq("107.138.206.37", "66.55.206.35", "71.201.234.202", "52.36.133.112").toArray.map(r => Map("ip" -> r)).random
val scn = scenario("Random load")
.feed(aidFeeder)
.feed(refererFeeder)
.feed(ipFeeder)
.exec(
http("/path random query")
.get("/[ath")
.queryParamSeq(Seq(
("rand_param", "${randomParam}"), ("url", "${referer}"), ("uip", "${ip}"), ("ua", userAgent), ("static_param", "777")
))
.check(
status.is(200),
xpath("//div/span").count.greaterThan(0)
)
)
setUp(scn.inject(injectionSteps))
.protocols(httpConfig)
.assertions(
global.responseTime.percentile4.lessThan(nominalResponseTime), // percentile4 stands for 99th
global.successfulRequests.percent.greaterThan(99)
)
}
class LogBasedLoadTest extends BaseLoadTest {
val logFeeder = separatedValues("some_log_50k_samples.csv", separator = '|').circular
var scn = scenario("Log based load")
.feed(logFeeder)
.exec(
http("/path log based query")
.get("/path")
.queryParamSeq(Seq(
("rand_param", "${randomParam}"), ("url", "${referer}"), ("uip", "${ip}"), ("ua", userAgent), ("static_param", "777")
))
.check(
status.is(200)
)
)
setUp(scn.inject(injectionSteps))
.protocols(httpConfig)
.assertions(
global.responseTime.percentile4.lessThan(nominalResponseTime), // percentile4 stands for 99th
global.successfulRequests.percent.greaterThan(99)
)
}
class TransformationLoadTest extends BaseLoadTest {
val scn = scenario("Transformations load")
.exec(
http("/path query with subsequent redirection")
.get("/path")
.queryParamSeq(Seq(
("static_param", "22"), ("url", "http://cc.cc"), ("uip", "8.8.8.8"), ("ua", userAgent)
))
.check(
status.is(200),
xpath("//div/span").count.is(1),
xpath("//img")
.transform(someUri => someUri.replaceFirst("example.com", "mock.host"))
.saveAs("impUri")
)
)
.exec(
http("/img call")
.get("${impUri}")
.check(status.is(200))
)
setUp(scn.inject(injectionSteps))
.protocols(httpConfig)
.assertions(
global.responseTime.percentile4.lessThan(nominalResponseTime), // percentile4 stands for 99th
global.successfulRequests.percent.greaterThan(99)
)
}
class CustomLoadTest extends BaseLoadTest {
val query = System.getProperty("query")
val scn = scenario("Custom load")
.exec(
http("/path custom query")
.get(s"/path?$query")
.check(
status.is(200),
xpath("//div/span").count.greaterThan(0)
)
)
setUp(scn.inject(injectionSteps))
.protocols(httpConfig)
.assertions(
global.responseTime.percentile4.lessThan(nominalResponseTime), // percentile4 stands for 99th
global.successfulRequests.percent.greaterThan(99)
)
}
class UserLoadTest extends BaseLoadTest {
val query = System.getProperty("query")
val scn = scenario("User defined load")
.exec(
http("user defined query")
.get(query)
)
setUp(scn.inject(injectionSteps))
.throttle(
reachRps(targetScenarios) in warmUpDuration,
holdFor(mainLoadDuration)
)
.protocols(httpConfig)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment