Performance testing of the Gatling

Before an application on-line, how many people have done performance testing?

It estimated that most developers pay more attention to functional testing, and will provide some unit testing and integration testing use cases. Sometimes, however, affect the performance of loopholes lead to more serious business than the undiscovered vulnerabilities, vulnerabilities that affect performance because the entire system, not just a business process.

Many of you may have heard the JMeter , but today will introduce competitive solutions - Gatling . It can generate a variety of reports, including all test cases indicators collected. This feature seems to be better than JMeter.

Before discussing Gatling, first understand the two types in the theory of knowledge, performance testing, load testing and stress testing:

  • Load testing (the Load Testing) : Load testing is a major software system in order to test whether the target design requirements documents, such as software in a given period, the maximum number of concurrent user support, software request error rate, etc., mainly software testing performance of the system.
  • Stress test (Stress Testing) : stress testing is mainly to test whether the system hardware design to achieve the performance goals of requirements documents, such as cpu utilization in a given period, the system, memory usage, disk I / O throughput, network throughput etc., stress testing and load testing biggest difference is that different testing purposes.

Gatling Profile

Gatling is a powerful load testing tool. It is for ease of use, maintainability and high performance design.

Out of the box, Gatling with excellent support for the HTTP protocol, making it the tool of choice for load test any HTTP server. Since the core engine is actually a protocol agnostic, so can implement support for other protocols, for example, Gatling JMS support is also provided.

As long as the underlying protocol (such as HTTP) can be implemented in a non-blocking manner, Gatling architecture is asynchronous. This architecture can be used as a virtual user message instead of a dedicated thread to achieve. Therefore, running thousands of concurrent virtual users is not a problem.

Gatling Quick Start Practice

  1. Create a Spring Boot application that provides RESTful API, for testing

    https://github.com/ChinaSilence/gatling-test.git

    If you have your own test Web applications can ignore this step!

  2. Start the database

    The sample code in dependence of the of PostgresSQL Github, so the first start the database, the easiest way is of course slightly Docker:

     docker run -d \
       --name postgres \
       -e POSTGRES_DB=gatling \
       -e POSTGRES_USER=gatling \
       -e POSTGRES_PASSWORD=gatling123 \
       -p 5432:5432 \
       postgres
  3. Install scala environment in IDEA

    Scala plugin installation

    Installation scala SDK

  4. Write performance test scripts

    Each test should inherit a Gatling Simulationclass, in which you can use to declare a Gatling Scala DSL scene list. The goal here is to run 30 clients, while sending 1,000 requests. First, the client by calling POST /personsthe method add data to the database; then, by calling the attempt GET /persons/{id}to query the data using the id method.

     class ApiGatlingSimulationTest extends Simulation {
    
       val scn = scenario("AddAndFindPersons").repeat(1000, "n") {
         exec(
           http("AddPerson-API")
             .post("http://localhost:8080/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(200))
         ).pause(Duration.apply(5, TimeUnit.MILLISECONDS))
       }.repeat(1000, "n") {
         exec(
           http("GetPerson-API")
             .get("http://localhost:8080/persons/${n}")
             .check(status.is(200))
         )
       }
    
       setUp(scn.inject(atOnceUsers(30))).maxDuration(FiniteDuration.apply(10, "minutes"))
     }
  5. Run Spring Boot application

  6. Run test scripts

    Maven plug-in configuration parameters

     <build>
         <plugins>
             <plugin>
                 <groupId>io.gatling</groupId>
                 <artifactId>gatling-maven-plugin</artifactId>
                 <version>${gatling-plugin.version}</version>
                 <configuration>
                     <!-- 测试脚本 -->
                     <simulationClass>com.anoyi.test.ApiGatlingSimulationTest</simulationClass>
                     <!-- 结果输出地址 -->
                     <resultsFolder>/Users/admin/code/gatling</resultsFolder>
                 </configuration>
             </plugin>
         </plugins>
     </build>

    Test execution

     mvn gatling:execute

  7. View test report

    Global Report

    Single interface details report

Acknowledgments

Links: https://www.jianshu.com/p/cdd9d29256c0

** If you want to know more, please pay attention to micro-channel public number **
![](https://img2018.cnblogs.com/blog/1821244/201910/1821244-20191001100059208-159680660.jpg)

Guess you like

Origin www.cnblogs.com/mrChangChang/p/11614859.html