Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Gateling for performance testing

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "how to use Gateling for performance testing". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use Gateling for performance testing".

What is Gatling?

Gatling is a powerful load testing tool written in Scala. It fully supports the HTTP protocol and can also be used to test JDBC connections and JMS. When using Gatling, you need to define the test scenario in Scala dsl code. It is worth mentioning that the HTML load report generated by Gatling is comprehensive and provides Gradle, Maven, and Jenkins plug-ins for easy integration.

Build a sample application

Before you start testing, you need to prepare the test application. The sample program is very simple, and the source code can be found on GitHub (github.com/piomin/sample-gatling-load-tests). It provides a set of RESTful HTTP API for CRUD operations to add and search for Entity in the available database. The database is built with Postgres, based on Spring Boot, and the persistence layer is implemented with Spring Data.

Plugins {

Id 'org.springframework.boot' version' 1.5.9.RELEASE'

}

Dependencies {

Compile group: 'org.springframework.boot', name:' spring-boot-starter-web'

Compile group: 'org.springframework.boot', name:' spring-boot-starter-data-jpa'

Compile group: 'org.postgresql', name:' postgresql', version: '42.1.4'

TestCompile group: 'org.springframework.boot', name:' spring-boot-starter-test'

}

Person entity maps to the person table.

@ Entity

@ SequenceGenerator (name = "seq_person", initialValue = 1, allocationSize = 1)

Public class Person {

@ Id

@ GeneratedValue (strategy = GenerationType.SEQUENCE, generator = "seq_person")

Private Long id

@ Column (name = "first_name")

Private String firstName

@ Column (name = "last_name")

Private String lastName

@ Column (name = "birth_date")

Private Date birthDate

@ Embedded

Private Address address

/ /...

}

The database connection settings and Hibernate properties are configured in application.yml.

Spring:

Application:

Name: gatling-service

Datasource:

Url: jdbc:postgresql://192.168.99.100:5432/gatling

Username: gatling

Password: gatling123

Jpa:

Properties:

Hibernate:

Hbm2ddl:

Auto: update

Server:

Port: 8090

As mentioned earlier, the sample program provides an API to add and search for person in the database, and here is the Spring REST controller implementation.

@ RestController

@ RequestMapping ("/ persons")

Public class PersonsController {

Private static final Logger LOGGER = LoggerFactory.getLogger (PersonsController.class)

@ Autowired

PersonsRepository repository

@ GetMapping function () {/ / Foreign Exchange tracking www.gendan5.com

Public List

FindAll () {

Return (List

) repository.findAll ()

}

@ PostMapping

Public Person add (@ RequestBody Person person) {

Person p = repository.save (person)

LOGGER.info ("add: {}", p.toString ())

Return p

}

@ GetMapping ("/ {id}")

Public Person findById (@ PathVariable ("id") Long id) {

LOGGER.info ("findById: id= {}", id)

Return repository.findOne (id)

}

}

Run the database

The next step in developing the sample program is to run the database, and the most appropriate way is Docker image. The following Docker command starts a Postgres container to complete the initialization of the gatling user and database.

Docker run-d-- name postgres-e POSTGRES_DB=gatling-e POSTGRES_USER=gatling-e POSTGRES_PASSWORD=gatling123-p 5432 POSTGRES_DB=gatling 5432 postgres

Design test scenarios

Each Gatling test suite inherits the Simulation class and declares a series of test scenarios using Gatling Scala DSL. Our goal is to start 30 clients and send 1000 requests at the same time. First, the client adds person to the database through the POST / persons method. Then, call GET / persons/ {id} to search for person. A total of 60, 000 requests were sent to applications: 30, 000 POST, 30, 000 GET. The following code shows the test scenario, which is very simple. You can find ApiGatlingSimulationTest in the src/test/scala directory.

Class ApiGatlingSimulationTest extends Simulation {

Val scn = scenario ("AddAndFindPersons"). Repeat (1000, "n") {

Exec (

Http ("AddPerson-API")

.post ("http://localhost:8090/persons")"

.header ("Content-Type", "application/json")

.body (StringBody ("" {"firstName": "John$ {n}", "lastName": "Smith$ {n}", "birthDate": "1980-01-01", "address": {"country": "pl", "city": "Warsaw", "street": "Test$ {n}", "postalCode": "02-200", "houseNo": ${n}} ")

.check (status.is)

) .pause (Duration.apply (5, TimeUnit.MILLISECONDS))

} .repeat (1000, "n") {

Exec (

Http ("GetPerson-API")

.get ("http://localhost:8090/persons/${n}")"

.check (status.is)

)

}

SetUp (scn.inject (atOnceUsers (30) .maxDuration (FiniteDuration.apply (10, "minutes"))

}

To enable the Gatling framework in your project, you also need to add dependencies to the Gradle build file.

TestCompile group: 'io.gatling.highcharts', name:' gatling-charts-highcharts', version: '2.3.0'

Run the test

Some Gradle plug-ins allow you to run tests during project construction. However, you can also use the io.gatling.app.Gatling class to define simple gradle tasks.

Task loadTest (type: JavaExec) {

DependsOn testClasses

Description = "Load Test With Gatling"

Group = "Load Test"

Classpath = sourceSets.test.runtimeClasspath

JvmArgs = [

"- Dgatling.core.directory.binaries=$ {sourceSets.test.output.classesDir.toString ()}"

]

Main = "io.gatling.app.Gatling"

Args = [

"--simulation", "pl.piomin.services.gatling.ApiGatlingSimulationTest"

"--results-folder", "${buildDir} / gatling-results"

"- binaries-folder", sourceSets.test.output.classesDir.toString ()

"--bodies-folder", sourceSets.test.resources.srcDirs.toList (). First (). ToString () + "/ gatling/bodies"

]

}

Use gradle loadTest to perform defined Gradle tasks. Of course, you need to start the application before running the test, start main class pl.piomin.services.gatling.ApiApplication in IDE or execute the java-jar build/libs/sample-load-test-gatling.jar command.

Test report

The report will be printed in text after the test is executed.

=

-Global Information

> request count 60000 (OK=60000 KO=0)

> min response time 2 (OK=2 KO=-)

> max response time 1338 (OK=1338 KO=-)

> mean response time 80 (OK=80 KO=-)

> std deviation 106 (OK=106 KO=-)

> response time 50th percentile 50 (OK=50 KO=-)

> response time 75th percentile 93 (OK=93 KO=-)

> response time 95th percentile 253 (OK=253 KO=-)

> response time 99th percentile 564 (OK=564 KO=-)

> mean requests/sec 319.149 (OK=319.149 KO=-)

-Response Time Distribution

> t

< 800 ms 59818 (100%) >

800 ms

< t < 1200 ms 166 ( 0%) >

T > 0 ms 16 (1200)

> failed 0 (0)

=

However, Gatling is best at reporting charts. The generated HTML report is in the build/gatling-results directory. The first report shows global information, including the total number of requests and the maximum response time (percentage). For example, 95% of GetPerson API requests have a maximum response time of 206ms.

Thank you for reading, the above is the content of "how to use Gateling for performance testing". After the study of this article, I believe you have a deeper understanding of how to use Gateling for performance testing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report