Gatling for Performance Testing

How many people did performance testing before the application went live?

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

Maybe many of you have heard of it. JMeter But today we will introduce competitive solutions—— Gatling . It can generate rich and colorful reports, including all the indicators collected in the test case. This functionality seems to be better than JMeter.

Before discussing Gatling, let's first understand the theoretical knowledge, two types of performance testing, load testing and stress testing.

  • Load Testing: Load Testing is to test whether the software system achieves the goal of requirement document design, such as how many concurrent users the software can support in a certain period of time, the error rate of software requests, etc. The main purpose of testing is the performance of the software system.
  • Stress Testing: The main purpose of stress testing is to test whether the hardware system achieves the performance goal of requirement document design. For example, in a certain period of time, the system's cpu utilization, memory utilization, disk I/O throughput, network throughput and so on. The biggest difference between stress testing and load testing is the different purpose of testing.

Introduction to Gatling

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

Out of the box, Gatling has excellent support for HTTP protocol, making it the preferred tool for load testing any HTTP server. Because the core engine is actually protocol agnostic, it can fully support other protocols, for example, Gatling currently provides JMS support.

Gatling's architecture is asynchronous as long as underlying protocols such as HTTP can be implemented in a non-blocking manner. This architecture allows virtual users to be implemented as messages rather than as dedicated threads. Therefore, running thousands of concurrent virtual users is not a problem.

Gatling Quick Start Practice

  1. Create Spring Book applications and provide RESTful API s for testing

    https://github.com/ChinaSilen...

    If you have your own Web application you can ignore this step!

  2. Start the database

    The sample code in Github relies on PostgresSQL, so the easiest way to start a database is, of course, with Docker:

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

    Install scala plug-in

    Install scala SDK

  4. Write performance test scripts

    Each Gatling test inherits the Simulation class, where you can use Gatling Scala DSL to declare a list of scenarios. The goal here is to run 30 clients and send 1000 requests at the same time. First, the client adds data to the database by calling POST/persons method; then, it tries to query data by calling GET/persons/{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. Running Spring Boot applications

  6. Running test scripts

    Configure Maven plug-in parameters

    <build>
        <plugins>
            <plugin>
                <groupId>io.gatling</groupId>
                <artifactId>gatling-maven-plugin</artifactId>
                <version>${gatling-plugin.version}</version>
                <configuration>
                    <!-- Test script -->
                    <simulationClass>com.anoyi.test.ApiGatlingSimulationTest</simulationClass>
                    <!-- Result Output Address -->
                    <resultsFolder>/Users/admin/code/gatling</resultsFolder>
                </configuration>
            </plugin>
        </plugins>
    </build>
    

    Execution testing

    mvn gatling:execute
    

  7. View test reports

    Global Report

    Single interface detail report

Acknowledgement

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

If you want to know more, please pay attention to the public address.

Keywords: Java Scala Database Spring github

Added by pmt2k on Thu, 03 Oct 2019 04:29:44 +0300