Prueba de carga con Gatling: la guía completa (Parte 2)

La traducción del material se preparó la víspera del inicio del curso en línea de pruebas de estrés.






4. Aplicación en prueba: base de datos de videojuegos

(Video Game Database). , , . API, Swagger, HTTP- (Get, Put, Update, Delete) XML JSON .





. ( ) , . :





  • Gradle./gradlew bootRun







  • Mavenmvn spring-boot:run







, : http://localhost:8080/swagger-ui/index.html#/





Swagger , - :





Base de datos de videojuegos
Video Game Database

API Swagger, XML JSON.





: ( ) , AWS. , , , , AWS , , , , !





, , Gatling.






5. Gatling

Gatling . scala , Gatling . simulations.





5.1   Gatling

Scala



MyFirstTest



. :





package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class MyFirstTest extends Simulation {
  
  // 1 Http Conf ( Http) 
  val httpConf = http.baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")
    .proxy(Proxy("localhost", 8888))
  
  // 2 Scenario Definition ( )
  val scn = scenario("My First Test")
    .exec(http("Get All Games")
      .get("videogames"))
  
  // 3 Load Scenario ( )
  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpConf)
}
      
      



Gatling, . http://localhost:8080/app/videogames.





:





5.1.1

:





import io.gatling.core.Predef._
import io.gatling.http.Predef._
      
      



Gatling - Gatling.





5.1.2 Extends Simulation

, Scala



Gatling Simulation



:





class MyFirstTest extends Simulation {
      
      



, Gatling, Simulation Gatling.





, 3 :





5.1.3 HTTP (Http Conf)

, , HTTP Gatling.





HTTP :





// 1 Http Conf
val httpConf = http.baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")
      
      



baseUrl



, API .





(header), , Accept -> application/json.





HTTP - Gatling HTTP.





5.1.4 (Scenario Definition)

Gatling, (user journey). , , :









  • 5









  • POST-





  • . .





GET videogames. , http://localhost:8080/app/videogames, baseUrl HTTP :





// 2 Scenario Definition ( )
val scn = scenario("My First Test")
          .exec(http("Get All Games")
              .get("videogames"))
      
      



5.1.5 -

Gatling — . (, , . .) Gatling. , , .





// 3 Load Scenario ( )
setUp(
   scn.inject(atOnceUsers(1))
   ).protocols(httpConf)
      
      



Gatling! , Engine



MyFirstTest



:





Mi primera prueba de gatling
My First Gatling Test

3 Gatling:





  • HTTP













, Gatling:





5.2

(pause) . HTTP.





Scala simulations CheckResponseCode



. :





package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._

import scala.concurrent.duration.DurationInt

class CheckResponseCode extends Simulation {

  val httpConf = http.baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")

  val scn = scenario("Video Game DB - 3 calls")

    .exec(http("Get all video games - 1st call")
      .get("videogames")
      .check(status.is(200)))
    .pause(5)

    .exec(http("Get specific game")
      .get("videogames/1")
      .check(status.in(200 to 210)))
    .pause(1, 20)

    .exec(http("Get all Video games - 2nd call")
      .get("videogames")
      .check(status.not(404), status.not(500)))
    .pause(3000.milliseconds)

  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpConf)

}
      
      



5.2.1

3 API. videogames, videogames/1, videogames.





.





18 .pause(5)



- 5 .





24 .pause(1, 20)



- 1 20 .





, 29 .pause(3000.milliseconds)



- , Gatling 3000 . : Gatling milliseconds, scala.concurrent.duration.DurationInt



.





5.2.2

API Gatling , . , Gatling .





17 .check(status.is(200)))



- , - 200.





23 .check(status.in(200 to 210)))



, , 200 210.





, 28 , - .check(status.not(404, status.not(500)))



- , 404 500.





5.3 Gatling Check API

Check API Gatling :





  • , .





  • .





JSONPath . JSONPath , . API.





(correlation) .





simulations CheckResponseBodyAndExtract



. :





package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class CheckResponseBodyAndExtract extends Simulation {

  val httpConf = http.baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")

  val scn = scenario("Check JSON Path")

      // First call - check the name of the game
      .exec(http("Get specific game")
      .get("videogames/1")
      .check(jsonPath("$.name").is("Resident Evil 4")))

      // Second call - extract the ID of a game and save it to a variable called gameId
      .exec(http("Get all video games")
      .get("videogames")
      .check(jsonPath("$[1].id").saveAs("gameId")))

      // Third call - use the gameId variable saved from the above call
      .exec(http("Get specific game")
      .get("videogames/${gameId}")
      .check(jsonPath("$.name").is("Gran Turismo 3"))

  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpConf)

}
      
      



3 API. JSONPath, , name JSON Resident Evil 4 :





  .exec(http("Get specific game")
  .get("videogames/1")
  .check(jsonPath("$.name").is("Resident Evil 4")))
      
      



. . id , JSON (.. 1):





  .exec(http("Get all video games")
  .get("videogames")
  .check(jsonPath("$[1].id").saveAs("gameId")))
      
      



, gameId



, URL- API:





  .exec(http("Get specific game")
  .get("videogames/${gameId}")
  .check(jsonPath("$.name").is("Gran Turismo 3"))
      
      



Gatling, .check()



, .saveAs







Check API Gatling.





5.4. Gatling

, , HTTP- . HTTP- , .





, .





simulations CodeReuseWithObjects



. :





package simulations

import io.gatling.core.Predef._
import io.gatling.http.Predef._

class CodeReuseWithObjects extends Simulation {

  val httpConf = http.baseUrl("http://localhost:8080/app/")
    .header("Accept", "application/json")


  def getAllVideoGames() = {
    repeat(3) {
      exec(http("Get all video games - 1st call")
        .get("videogames")
        .check(status.is(200)))
    }
  }

  def getSpecificVideoGame() = {
    repeat(5) {
      exec(http("Get specific game")
        .get("videogames/1")
        .check(status.in(200 to 210)))
    }
  }

  val scn = scenario("Code reuse")
      .exec(getAllVideoGames())
      .pause(5)
      .exec(getSpecificVideoGame())
      .pause(5)
      .exec(getAllVideoGames())

  setUp(
    scn.inject(atOnceUsers(1))
  ).protocols(httpConf)

}
      
      



API, . — getAllVideoGames()



:





  def getAllVideoGames() = {
    repeat(3) {
      exec(http("Get all video games - 1st call")
        .get("videogames")
        .check(status.is(200)))
    }
  }
      
      



, repeat(3)



- , 3 ( 3 HTTP-).





HTTP- - :





def getSpecificVideoGame() = {
    repeat(5) {
      exec(http("Get specific game")
        .get("videogames/1")
        .check(status.in(200 to 210)))
    }
  }
      
      



repeat(5)



, 5 .





, :





  val scn = scenario("Code reuse")
      .exec(getAllVideoGames())
      .pause(5)
      .exec(getSpecificVideoGame())
      .pause(5)
      .exec(getAllVideoGames())
      
      



, . , Gatling API.






« ».



- «LoadRunner: ».








All Articles