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

It is expected that most developers will focus more on functional testing and will provide some use cases for unit and integration testing. However, performance vulnerabilities can sometimes have a more serious impact than undiscovered business vulnerabilities because they affect the entire system, not just one business process.

Many of you have probably heard of JMeter, but today we will introduce Gatling, a competitive solution. It produces a rich and colorful report containing all the metrics collected in the test case. This feature seems to be better than JMeter.

Before discussing Gatling, let’s take a look at the theoretical knowledge. There are two types of performance test, load test and pressure test:

  • Load Testing: Load Testing is mainly to test whether the software system meets the requirements of the document design objectives, such as the maximum number of concurrent users supported by the software in a certain period of time, software request error rate, etc., Testing is mainly the performance of the software system.
  • Stress Testing: The main purpose of Stress Testing is to test whether the hardware system meets the performance objectives designed by the requirements document, such as the SYSTEM’s CPU utilization, memory utilization, disk I/O throughput, network throughput, etc., in a certain period of time. The biggest difference between Stress Testing and load Testing lies in the different Testing purposes.

Gatling profile

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

Out of the box, Gatling comes with excellent support for the HTTP protocol, making it the preferred tool for load testing any HTTP server. Since the core engine is effectively protocol agnostic, it is perfectly possible to implement support for other protocols, for example, Gatling currently also provides JMS support.

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

Gatling Quick Start practice

1. Create a Spring Boot application and provide RESTful apis for testing

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

Skip this step if you have your own Web application testing!

2. Start the database

The Github sample code relies on PostgresSQL, so the easiest way to start the database is to use Docker:

docker run -d \
  --name postgres \
  -e POSTGRES_DB=gatling \
  -e POSTGRES_USER=gatling \
  -e POSTGRES_PASSWORD=gatling123 \
  -p 5432:5432 \
  postgresCopy the code

3. Install the Scala environment in IDEA

Installing the Scala plug-in

Install scala SDK

4. Write performance test scripts

Each Gatling test inherits from the Simulation class, where you can use the Gatling Scala DSL to declare a list of scenarios. The goal here is to run 30 clients and send 1000 requests simultaneously. First, the client adds data to the database by calling the POST/Persons method. Then, try to query the data using id by calling GET /persons/{id}.

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"))

}Copy the code

5. Run the Spring Boot application

6. Run the test script

Configure Maven plug-in parameters

<build> <plugins> <plugin> <groupId>io.gatling</groupId> <artifactId>gatling-maven-plugin</artifactId> <version>${gatling-plugin.version}</version> <configuration> <! - the test script - > < simulationClass > com. Anoyi. Test. ApiGatlingSimulationTest < / simulationClass > <! - results output address - > < resultsFolder > / Users/admin/code/gatling < / resultsFolder > < / configuration > < / plugin > < / plugins > < / build >Copy the code

Perform the test

mvn gatling:executeCopy the code

7. View the test report

Global report

Single interface details report