Skip to content

Instantly share code, notes, and snippets.

@Ivan-Shestakov
Created December 31, 2019 12:58
Show Gist options
  • Save Ivan-Shestakov/79f4deb293750733c94eb3ad0c24a119 to your computer and use it in GitHub Desktop.
Save Ivan-Shestakov/79f4deb293750733c94eb3ad0c24a119 to your computer and use it in GitHub Desktop.
Ad-hoc performance testing a web service with Ammonite and Gatling (Scala)
/**
* Load Test an example web service using Gatling
*
* Based on https://gist.github.com/dacr/88c797b05ee968be145eccc77e498888
*
* ## Prerequisites:
* 1. Ammonite installed (http://ammonite.io/)
* 2. users-query.csv file in same folder as the script. Its' structure is:
* user, query,
* Bob, beavers,
* Alice, airbrush,
* 3. Asset classifier webservice running on localhost:9900 (otherwise modify the baseUrl param below)
*
* ## Running:
* 1. Execute `amm ./sample-load-test.sc`
* 2. Open the /tmp/SamplePerformanceTest-20190801114719686/index.html to review the report (of course the timestamp will differ)
*/
import $ivy.`io.gatling:gatling-http:3.2.0`
import $ivy.`io.gatling:gatling-app:3.2.0`
import $ivy.`io.gatling.highcharts:gatling-charts-highcharts:3.2.0`
import $ivy.`ch.qos.logback:logback-classic:1.2.3`
import io.gatling.app.Gatling
import io.gatling.core.config.GatlingPropertiesBuilder
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
import org.slf4j.{LoggerFactory, Logger}
val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME).asInstanceOf[ch.qos.logback.classic.Logger]
rootLogger.setLevel(ch.qos.logback.classic.Level.ERROR)
class SamplePerformanceTest extends Simulation {
def config = {
http
.baseUrl("http://localhost:9900")
}
def assertions = List(
global.responseTime.mean.lt(20),
global.successfulRequests.percent.gt(95)
)
def scn =
scenario("Simple load").during(10 minutes) {
feed(csv("./users-query.csv").random)
.exec(http("search")
.post("/search?${uuid}&${query}")
.body(StringBody("""{"user":"${user}","searchQuery":"${query}"}""")).asJson
.check(status.is(200)) )
}
setUp(scn.inject(atOnceUsers(1)))
.throttle(jumpToRps(200),holdFor(10 minutes)) //This line limits the max requests per second to 200
.protocols(config)
.assertions(assertions)
}
val props =
new GatlingPropertiesBuilder()
.simulationClass(classOf[SamplePerformanceTest].getName)
.resultsDirectory("/tmp")
.build
Gatling.fromMap(props)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment