通过连续的 http 请求在 Gatling 中实现用户行为
Implement user behavior in Gatling via sequential http requests
有没有办法在一种情况下实现顺序 http 请求?
很清楚我想要什么:用“模拟”用户行为在 REST API 上进行负载测试。
1.1。用户转到 /a
1.2。获取一些信息
2.1。然后用一些以前获取的信息转到 /b
2.2。 ...等等
而且这个场景必须同时由一定数量的VU执行。
但是我在图形上看到所有 VU 多次同时执行请求 1,然后都执行请求 2,依此类推:
- requests to /a goes for 10s
- then requests to /b goes for 10s
但是每次rout响应大约在20-30ms,所以不会因为等待响应而延迟。
这就是现在用户的行为方式,而不是我想要的测试结果。
我在我的项目中做了什么:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>b</artifactId>
<version>1.0.0</version> <!-- build_version -->
<properties>
<java.version>1.8</java.version>
<scala.version>2.12</scala.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.maven.plagin.version>4.4.0</scala.maven.plagin.version>
<gatling.maven.plagin.version>3.0.5</gatling.maven.plagin.version>
<gatling.version>3.3.1</gatling.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling.maven.plagin.version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala.maven.plagin.version}</version>
<configuration>
<jvmArgs>
<jvmArg>-Xss100M</jvmArg>
</jvmArgs>
<args>
<arg>-target:jvm-${java.version}</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
.scala 文件:
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import scala.concurrent.duration._
class SimpleScenario extends Simulation {
val users = 200
val maxRPS = 200
val rampUp: FiniteDuration = 1 minutes
val duration: FiniteDuration = 1 minutes
val httpProtocol: HttpProtocolBuilder = http
.baseUrl("http://some.site")
val scn0: ScenarioBuilder = scenario("ASimulation")
.exec(
Seq(
exec(
http("a")
.post("/a")
.body(StringBody("""{"username": "a", "password": "b"}"""))
.check(status.is(200))
)
.exec(
http("b")
.get("/b")
.check(status.is(200))
)
// ... and so on
)
)
setUp(
scn0
.inject(
constantConcurrentUsers(users) during(rampUp + duration)
)
.throttle(
reachRps(maxRPS) in (rampUp),
holdFor(duration)
)
).protocols(httpProtocol)
}
我运行执行测试的命令:
mvn gatling:test
我已经试过了:
.repeat(1)
.exec
没有 Seq
-
.exec
的链条
so its not delaying by waiting response
是的。每个虚拟用户在发送请求“b”之前将等待响应“a”。
有没有办法在一种情况下实现顺序 http 请求?
很清楚我想要什么:用“模拟”用户行为在 REST API 上进行负载测试。
1.1。用户转到 /a
1.2。获取一些信息
2.1。然后用一些以前获取的信息转到 /b
2.2。 ...等等
而且这个场景必须同时由一定数量的VU执行。
但是我在图形上看到所有 VU 多次同时执行请求 1,然后都执行请求 2,依此类推:
- requests to /a goes for 10s
- then requests to /b goes for 10s
但是每次rout响应大约在20-30ms,所以不会因为等待响应而延迟。 这就是现在用户的行为方式,而不是我想要的测试结果。
我在我的项目中做了什么:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>a</groupId>
<artifactId>b</artifactId>
<version>1.0.0</version> <!-- build_version -->
<properties>
<java.version>1.8</java.version>
<scala.version>2.12</scala.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<scala.maven.plagin.version>4.4.0</scala.maven.plagin.version>
<gatling.maven.plagin.version>3.0.5</gatling.maven.plagin.version>
<gatling.version>3.3.1</gatling.version>
</properties>
<dependencies>
<dependency>
<groupId>io.gatling.highcharts</groupId>
<artifactId>gatling-charts-highcharts</artifactId>
<version>${gatling.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>sonatype</id>
<url>https://oss.sonatype.org/content/repositories/releases/</url>
</repository>
</repositories>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<groupId>io.gatling</groupId>
<artifactId>gatling-maven-plugin</artifactId>
<version>${gatling.maven.plagin.version}</version>
<executions>
<execution>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>${scala.maven.plagin.version}</version>
<configuration>
<jvmArgs>
<jvmArg>-Xss100M</jvmArg>
</jvmArgs>
<args>
<arg>-target:jvm-${java.version}</arg>
<arg>-deprecation</arg>
<arg>-feature</arg>
<arg>-unchecked</arg>
<arg>-language:implicitConversions</arg>
<arg>-language:postfixOps</arg>
</args>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
.scala 文件:
import io.gatling.core.Predef._
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import scala.concurrent.duration._
class SimpleScenario extends Simulation {
val users = 200
val maxRPS = 200
val rampUp: FiniteDuration = 1 minutes
val duration: FiniteDuration = 1 minutes
val httpProtocol: HttpProtocolBuilder = http
.baseUrl("http://some.site")
val scn0: ScenarioBuilder = scenario("ASimulation")
.exec(
Seq(
exec(
http("a")
.post("/a")
.body(StringBody("""{"username": "a", "password": "b"}"""))
.check(status.is(200))
)
.exec(
http("b")
.get("/b")
.check(status.is(200))
)
// ... and so on
)
)
setUp(
scn0
.inject(
constantConcurrentUsers(users) during(rampUp + duration)
)
.throttle(
reachRps(maxRPS) in (rampUp),
holdFor(duration)
)
).protocols(httpProtocol)
}
我运行执行测试的命令:
mvn gatling:test
我已经试过了:
.repeat(1)
.exec
没有Seq
-
.exec
的链条
so its not delaying by waiting response
是的。每个虚拟用户在发送请求“b”之前将等待响应“a”。