Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created April 22, 2012 14:09
Show Gist options
  • Save andypetrella/2464268 to your computer and use it in GitHub Desktop.
Save andypetrella/2464268 to your computer and use it in GitHub Desktop.
Specs for using the Gatling Play 2.0 plugin
//creates a fake Play 2.0 server running on 3333 which defines the gatling plugin automatically
val server = Util.createServer(3333)
//function that starts the server, should be used in a specs Step BEFORE the whole specification starts
def startServer {
server.start()
}
//function that stops the server, should be used in a specs Step AFTER the whole specification has ran
def stopServer {
server.stop()
}
//function that cleans all gatling ressources used, including its Akka actor system (same remarks than for stopServer)
def cleanGatling {
// shut all actors down
system.shutdown
}
/* LOCAL MAVEN REPO */
val localMavenRepo = "Local Maven Repository" at file(Path.userHome.absolutePath+"/.m2/repository").toURI.toURL.toString
val gatlingPlugin = "be.nextlab" %% "gatling-play2-plugin" % "1.0-SNAPSHOT" % "test"
class TestStress extends Specification {
val baseUrl = "http://localhost:3333"
//creates a fake Play 2.0 server running on 3333 which defines the gatling plugin automatically
val server = Util.createServer(3333)
//function that starts the server, should be used in a specs Step BEFORE the whole specification starts
def startServer {
server.start()
}
//function that stops the server, should be used in a specs Step AFTER the whole specification has ran
def stopServer {
server.stop()
}
//function that cleans all gatling ressources used, including its Akka actor system (same remarks than for stopServer)
def cleanGatling {
// shut all actors down
system.shutdown
}
def is =
"stress play" ^ Step(startServer) ^ {
"test 1" ! GatlingApp(
new Simulation() {
def apply() = {}
}) {
//checks
}
} ^
Step(stopServer) ^
Step(cleanGatling) ^
end
}
"create a lot" ! GatlingApp(new Simulation() {
def apply() = {
val headers = fromToParams(
http("rest_create_stuff").post(routes.Stuffs.createStuff.url).headers(headers_2),
Stuffs.stuffForm,
Stuff()
)
//execute as many creation as possible within 10 seconds
val scn = scenario("rest create stuff").loop(
chain.exec(
headers
.check(status.is(200))
)) counterName("manyCreationIn10s") during(10, SECONDS)
val httpConf = httpConfig.baseURL(baseUrl)
Seq(scn.configure users 10 ramp 2 protocolConfig httpConf)
}
}) {
//checks TODO
}
def fromToParams[T](h: PostHttpRequestBuilder, f: Form[T], t: T) =
f.fill(t).data.foldLeft(h) {
(acc, e) => acc.param(e._1, e._2)
}
[...]
//1/ URL encoded
val headers_2 = headers_1 + (CONTENT_ENCODING -> "application/x-www-form-urlencoded")
"create" ! GatlingApp(new Simulation() {
def apply() = {
//2/ use the helper form -> params
val headers = fromToParams(
//3/ use the reverse router instead of hard coded paths
http("rest_create_stuff").post(routes.Stuffs.createStuff.url).headers(headers_2),
//4/ use the form defined in the controller
Stuffs.stuffForm,
//5/ use your model instead of creating the params your self
Stuff("data", 1, true)
)
val scn = scenario("rest create stuff").exec(headers.check(status.is(200)))
val httpConf = httpConfig.baseURL(baseUrl)
Seq(scn.configure users 10 ramp 2 protocolConfig httpConf)
}
}) {
//checks TODO
}
val headers_1 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Accept-Charset" -> "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
)
"root url" ! GatlingApp(
new Simulation() {
def apply() = {
val scn = scenario("root url")
.exec(
http("root_url_simple")
.get("")
.headers(headers_1)
.check(status.is(200))
)
val httpConf = httpConfig.baseURL(baseUrl)
Seq(scn.configure users 10 ramp 2 protocolConfig httpConf)
}
}) {
//checks TODO
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment