Spring Boot + Gatling
Goal
The goal of this entry is to make project Spring Boot Web Application with Gatling.
I explained about Gatling in the last entry.()
Write web application codes and do load test in
Gatling
Gatling is based on Scala, so we can combine with Java Project.
Env
- IntelliJ(IDE)
- Spring Boot, Spring Boot Web
- Gatling
Project
Create project directory
mkdir Project cd Project mkdir -p src/main/java mkdir -p src/main/resources mkdir -p src/test/java mkdir -p src/test/scala
build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")
}
}
apply plugin: "java"
apply plugin: "spring-boot"
apply plugin: "scala"
apply plugin: "idea"
jar {
baseName = "Project"
version = "0.0.1-SNAPSHOT"
}
repositories {
mavenCentral()
}
dependencies {
compile("org.springframework.boot:spring-boot-starter-web")
testCompile("org.springframework.boot:spring-boot-starter-test")
testCompile("io.gatling.highcharts:gatling-charts-highcharts:2.2.3")
}
task loadTest(type: JavaExec) {
dependsOn testClasses
description = "load test with Gatling"
group = "Load Test"
classpath = sourceSets.test.runtimeClasspath
main = "io.gatling.app.Gatling"
args = [
"--simulation", "com.atmarkplant.project.gatling.MainSimulation",
"--results-folder", "${buildDir}/gatling-results",
"--bodies-folder", sourceSets.test.resources.srcDirs.toList().first().toString() + "/gatling/bodies",
]
}
Build and Import Project
Open Project directory from IntelliJ
Spring Boot Controller
BasicController.java
@RestController
public class BasicController {
@RequestMapping(value="/")
public String root() {
return "Hello";
}
}
Main.java
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
Gatling Test
test/scala/com.atmarkplant.project.gatling.MainSimulation.scala
class MainSimulation extends Simulation {
val httpConf = http
.baseURL("http://localhost:8080") // Here is the root for all relative URLs
val scn = scenario("Sample")
.exec(http("request1")
.get("/"))
.pause(100 milliseconds)
setUp(scn.inject(rampUsers(20) over(5 seconds)) .protocols(httpConf))
}
Run Test
Start Spring Boot Application -> http://localhost:8080 is enable
Run Test, execute gradle task
gradle loadTest
Run Gatling and results are under build/gatling-results/
