Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Created July 20, 2012 19:45
Show Gist options
  • Save andypetrella/3152811 to your computer and use it in GitHub Desktop.
Save andypetrella/3152811 to your computer and use it in GitHub Desktop.
Gatling SBT Test
galtlingConfFile in GatlingTest <<= baseDirectory { _ / "gatling-test" / "conf" / "galing.conf" },
scalaSource in GatlingTest <<= baseDirectory { _ / "gatling-test" / "simulations" },
sourceDirectories in GatlingTest <+= baseDirectory { _ / "gatling-test" / "simulations" }
import sbt._
import Keys._
import GatlingPlugin._
object MinimalBuild extends Build {
val SNAPSHOT = "-SNAPSHOT"
val appName = "gatling-sbt-sample"
val buildVersion = "0.0.1-SNAPSHOT"
val gatSbtTestVersion = "0.0.1-SNAPSHOT"
val cloudbees = "https://repository-andy-petrella.forge.cloudbees.com/"
val cloudbeesRepo = gatSbtTestVersion match {
case x if x.endsWith(SNAPSHOT) => x.toLowerCase at cloudbees + "snapshot" + "/"
case x => x.toLowerCase at cloudbees + "release" + "/"
}
val libDependencies = Seq(
//grrrr
"be.nextlab" %% "gatling-sbt-test-framework" % gatSbtTestVersion % "gatling-test"
)
lazy val allSettings =
Project.defaultSettings ++
GatlingPlugin.gatlingSettings ++
Seq(
version := buildVersion,
organization := "be.nextlab",
javacOptions += "-Xlint:unchecked",
libraryDependencies ++= libDependencies,
// grrr twice
commands ++= Seq(gatlingTakatak)
)
lazy val root = Project(id = appName, base = file(".")).configs(GatlingTest).settings(allSettings: _*)
}
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import play.api.test._
import play.api.test.Helpers.running
import play.api.libs.Files._
class CreateAndLogin() extends PlaySimulation {
val port = 3333
implicit val application = FakeApplication()
def apply = {
val baseUrl = "http://localhost:3333"
val headers_1 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8",
"Accept-Charset" -> "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
)
val form = controllers.Application.loginForm
val createForm = controllers.Application.createForm
val users = (1 to 10000) map {i => models.User("user"+i, "firstName"+i, "lastName"+i, i)}
val createFeed = simpleFeeder("createUsers", users map {u => createForm.fill(u).data}:_*)
val usersFeed = simpleFeeder("createdUsers", users.map(u => form.fill(u.userName).data):_*)
val existingFeed = simpleFeeder("allUsers", Global.testUsers.map(u => form.fill(u.userName).data):_*)
val scn = scenario("createAndLogin")
.exec(
http("login-page")
.get(controllers.routes.Application.login.url)
.headers(headers_1)
.check(status.is(200))
).pause(5)
.exec(
http("create-page")
.get(controllers.routes.Application.create.url)
.headers(headers_1)
.check(status.is(200))
).pause(30)
.feed(createFeed)
.exec(
http("submit-create")
.post(controllers.routes.Application.register.url)
/*TODO boilerplate that could be removed... using Form info*/
.param("userName", "${userName}")
.param("firstName", "${firstName}")
.param("lastName", "${lastName}")
.param("age", "${age}")
.headers(headers_1)
.check(status.is(200))
)
.pause(10)
.feed(usersFeed)
.exec(
http("loginCreated")
.post(controllers.routes.Application.submit.url)
.param("userName", "${userName}")
.headers(headers_1)
.check(status.is(200))
)
val sndScn = scenario("loginExistingUsers")
.exec(
http("login-page")
.get(controllers.routes.Application.login.url)
.headers(headers_1)
.check(status.is(200))
)
.pause(10)
.feed(existingFeed)
.exec(
http("loginCreated")
.post(controllers.routes.Application.submit.url)
.param("userName", "${userName}")
.headers(headers_1)
.check(status.is(200))
)
val httpConf = httpConfig.baseURL(baseUrl)
Seq(
scn.configure users 10000 ramp 10 protocolConfig httpConf,
sndScn.configure users (Global.testUsers.size) ramp 1 protocolConfig httpConf
)
}
}
me@mymachine$ sbt
> takatak
me@mymachine$ sbt #or -- play -- if you're not using the typesafe stack like me
[gatling-play-sample]$ takatak
addSbtPlugin("be.nextlab" % "gatling-sbt-plugin" % "0.0.1-SNAPSHOT")
gatling-project
- project
- Build.scala
- build.properties
- plugins.sbt
- src
- main
- test
- gatling-test
- conf
- gatling.conf
- simulations
import be.nextlab.gatling.sbt.plugin.Simulation
import com.excilys.ebi.gatling.core.Predef.{Simulation => GSimulation, _}
import com.excilys.ebi.gatling.http.Predef._
class SampleSimulation() extends Simulation {
def pre = {}
def post = {}
def apply = {
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"
)
val scn = scenario("GGL")
.exec(
http("request_1")
.get("/")
.headers(headers_1)
.check(status.is(200))
)
val httpConf = httpConfig.baseURL("http://www.google.com")
Seq(scn.configure users 10000 ramp 2 protocolConfig httpConf)
}
}
import com.excilys.ebi.gatling.core.Predef._
import com.excilys.ebi.gatling.http.Predef._
import play.api.test._
import play.api.test.Helpers.running
import play.api.libs.Files._
class Simple() extends PlaySimulation {
val port = 3333
implicit val application = FakeApplication()
def apply = {
val baseUrl = "http://localhost:3333"
val headers_1 = Map(
"Accept" -> "text/html,application/xhtml+xml,application/xml,application/json;q=0.9,*/*;q=0.8",
"Accept-Charset" -> "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
)
val scn = scenario("simple")
.exec(
http("index")
.get(controllers.routes.Application.index.url)
.headers(headers_1)
.check(status.is(200))
)
val httpConf = httpConfig.baseURL(baseUrl)
Seq(
scn.configure users 10000 ramp 10 protocolConfig httpConf,
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment