首页 > 解决方案 > 如何按顺序而不是同时运行加特林场景?

问题描述

我创建了一个简单的 gatling maven 应用程序来测试性能,验证 API 速率限制(因此,如果发送的请求超过其速率限制,它应该会失败)。为了将每个 api 作为独立操作而不是同时运行,我为每个 API 调用创建了单独的模拟类以进行测试。为了按顺序运行它们,我启用了 runMultipleSimulations 。遵循例如配置:galting doc:https ://gatling.io/docs/3.0/extensions/maven_plugin/

但我不认为它们是按顺序运行的,我从日志中看到的一种情况主要是显示失败的用户请求和 500 个内部服务器错误,我也没有在日志中看到所有测试场景。除了这个依赖,我还需要其他什么吗?这是我的 pom.xml :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>gatling-test</groupId>
    <artifactId>gatling-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <gatling-plugin.version>3.0.1</gatling-plugin.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>io.gatling.highcharts</groupId>
            <artifactId>gatling-charts-highcharts</artifactId>
            <version>3.1.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <testSourceDirectory>src/test/scala</testSourceDirectory>
        <plugins>
            <plugin>
                <groupId>io.gatling</groupId>
                <artifactId>gatling-maven-plugin</artifactId>
                <version>${gatling-plugin.version}</version>

                <configuration>
                    <runMultipleSimulations>true</runMultipleSimulations>
                </configuration>

                <executions>
                    <execution>
                        <goals>
                            <goal>test</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

例如:一个场景文件:

class DeleteAPISimulation extends Simulation {
  var test = ""

  val preScenario = scenario(" DELETE API PRE-SCENARIO")
    .exec(CreateResource())
    .exec(session => {
      test = session("resourceId").as[String].trim
      println("%%%%%%%%%%% resource ID =====>>>>>>>>>> " + test)
      session}
    )
  val deleteResourceScenario = scenario("DELETE API TEST SCENARIO")
    // Set it here
    .exec(_.set("resourceId", test))
    .exec(DeleteResource())

  setUp(
    preScenario.inject(atOnceUsers(1)),
    deleteResourceScenario.inject(rampUsers(520) during(60))
  )

}

Actions 类具有 API 的 DSL http 请求实现。例如:

object Actions{

def DeleteResource():HttpRequestBuilder = {
  http("DELETE_RESOURCE_OPERATION")
    .delete(Host+ "/items/${resourceId}")
    .header("Authorization", "Bearer "+ Token)
    .check(status.is(200))
}
.
.
//so on
// similar api method requests
.
}

下面是我的项目结构的外观和动作类以及场景模拟类位于 src/test/scala 下:

gatlingtest 
  - src
     - test 
       - resources
       - scala
           - Actions
           - CreateAPISimulation
           - GetAPISimulation
           - DeleteAPISimulation

标签: scalamavengatlingscala-gatling

解决方案


从 Gatling 3.3 开始,没有真正的方法可以按顺序运行场景。唯一的解决方案是在延迟一段时间后启动其他场景,见nothingFor

Gatling 3.4 (2020 年 9 月发布)中将引入顺序场景。


推荐阅读