본문 바로가기

소소한 꿀팁..?

[gatling] gatling 사용법 (with. scenario)

❗ gatling을 사용하며 반드시 알아야 하는 개념을 정리하는 포스팅입니다! gatling의 공식 doc에 더 자세히 설명되어 있으니 참고 부탁드립니다

 

공식 doc에는 다음과 같은 순서로 쓰여있습니다.

BootStrap

Structure Elements

-> exec

-> pause

-> Loop statements

-> Conditional statement

-> errors handling

-> groups


✅ 스크립트를 작성하려면 gatling측에서 제공해야 하는 Simulation 클래스를 상속받아야합니다

public class ComputerDatabaseSimulation extends Simulation {

}

 

protocol 클래스를 빌드해야 합니다.

public class ComputerDatabaseSimulation extends Simulation {

  // Add the HttpProtocolBuilder:
  HttpProtocolBuilder httpProtocol =
    http.baseUrl("https://computer-database.gatling.io")
      .acceptHeader("application/json")
      .contentTypeHeader("application/json");
}

 

유저가 웹 어플리케이션과 상호작용할 Scenario를 작성해주고 exec 해줍니다. (시나리오 == 일종의 테스트 스크립트 입니다.)

  • exec은 action을 실행시킵니다 여기서 인자에는 프로토콜이 주어집니다.(HTTP, WebSocket, JMS, MQTT.. )
  • 프로토콜 안에 인자로 주어지는 문자열은 각 프로토콜(요청)에 이름을 붙여 나중에 확인하기 편하도록 합니다.
public class ComputerDatabaseSimulation extends Simulation {

  ...
  // Scenario 빌더에 exec정보를 저장
  ScenarioBuilder myScenario = scenario("My Scenario")
    .exec(http("Request 1").get("/computers/"));
    
  // Scenario에 exec정보를 저장
  scenario("Scenario")
    .exec(http("Home").get("https://gatling.io"));

  // exec를 바로 생성하고 저장하기
  ChainBuilder chain = exec(http("Home").get("https://gatling.io"));

  // 요청을 순차적으로 실행되도록 저장하기
  exec(
    http("Home").get("https://gatling.io"),
    http("Enterprise").get("https://gatling.io/enterprise")
  );

  // 다른 exec에 chaining으로 가능
  exec(http("Home").get("https://gatling.io"))
    .exec(http("Enterprise").get("https://gatling.io/enterprise"));
  ...
  
}

sample로 주어지는 시나리오에 http 인자를 "테스트"로 바꿨습니다.
"테스트"라는 문자를 확인할 수 있습니다.

사용자가 특정 페이지에서 잠시 머무르는 것은 pausepace로 구현가능합니다. 이때 pause단순 gatling측에서 특정 시간동안 머무르는 것이라면 pace요청이 도착한 다음 시간부터 특정 시간이 지난뒤 수행합니다. 또한 pause와 pace는 둘 다 대기하는 시간의 min과 max를 설정할 수 있습니다.

 

✅ 반복은 repeat로 가능합니다. 이때 첫번째 인자는 반복횟수반드시 있어야 하는 인수이며 두번째는 반복문의 이름을 설정해주고 이는 필수가 아닙니다.

// with an Int times
repeat(5).on(
  http("name").get("/")
);
// with a Gatling EL string resolving an Int
repeat("#{times}").on(
  http("name").get("/")
);
// with a function times
repeat(session -> 5).on(
  http("name").get("/")
);
// with a counter name
repeat(5, "counter").on(
  http("name").get("/?counter=#{counter}")
);
// iterating over multiple actions sequentially
repeat(5).on(
  http("name1").get("/"),
  http("name2").get("/")
);

 

✅ 반복은 foreach로 가능합니다. 이때 첫번째 인자는 반복 가능한 인수이며 두번째는 반복가능한 인자에서 뽑아온 변수이며 첫번째와 두번째 인자는 필수 입니다. 마지막으로 3번째 인자는 반복문의 이름을 나타냅니다.

 

during으로 특정 시간동안 계속해서 반복할 수 있습니다. 첫번째 인자는 반복할 시간 gatling EL String 혹은 function을 나타내고 두번째 인자는 반복문의 이름을 나타냅니다. 마지막 인자는 boolean이며 해당 값이 True이면 각 반복문 안에 인자들이 참인지 거짓인지 확인하고 만약 참이면 during의 인자로 주어진 값까지 반복하기 전에 반복을 종료합니다.