如何为同一个虚拟用户执行具有不同动态更新值的每个 Java Gadling 请求?

问题描述 投票:0回答:1

我有这个 Java 加特林模拟:

package com.mycompany.amlar.performancetests;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mycompany.amlar.openapi.Notification;
import io.gatling.javaapi.core.ChainBuilder;
import io.gatling.javaapi.core.ScenarioBuilder;
import io.gatling.javaapi.core.Simulation;
import io.gatling.javaapi.http.HttpProtocolBuilder;

import static io.gatling.javaapi.core.CoreDsl.*;
import static io.gatling.javaapi.http.HttpDsl.*;

public class AmlarSimulation extends Simulation {
    private final HttpProtocolBuilder httpProtocol = http
            .disableFollowRedirect()
            .baseUrl(BASE_URL)
            .acceptHeader("application/json")
            .header("X-AmlArReporting-CID", CID)
            .header("X-AmlArReporting-ICAs", ICAS);
       
    private final ScenarioBuilder mainScn = scenario("MainScenario")
            .exec(getStatus())
            .exec(postStatus())
            );

     private static ChainBuilder getStatus() {
        return exec(http("GetStatus")
                .get(session -> API_PATH + "/notifications/00000000-0000-6000-7000-000000000000/status")
                .check(status().is(200))
                .check(jsonPath("$[0].id").notNull())
                .check(jsonPath("$[0].modelVersion").notNull().saveAs("modelVersion"))
        );
    }

    private static ChainBuilder postStatus() {
        return exec(session -> {
            // Retrieve the current modelVersion from the session
            String currentModelVersion = session.getString("modelVersion");
            // Increment the modelVersion by 1
            int incrementedModelVersion = Integer.parseInt(currentModelVersion) + 1;
            // Update the session with the incremented modelVersion
            session.set("modelVersion", String.valueOf(incrementedModelVersion));
            return session;
        }).exec(http("PostStatus")
                .post(session -> API_PATH + "/notifications/00000000-0000-6000-7000-000000000000/status")
                .header("Content-Type", "application/json")
                .header("Accept", "application/json")
                .body(StringBody(session -> """
                {
                   "comment":"",
                   "fiFlaggedStatus":"FLAGGED",
                   "investigationAssessment":"WORTHY",
                   "investigationStatus":"COMPLETED",
                   "panRiskScore":986,
                   "modelVersion":"%s",
                   "created":"2026-07-02T08:20:38.814Z",
                   "id":""
                }
                """.formatted(session.getString("modelVersion"))))
                .check(status().is(200))
                .check(jsonPath("$.modelVersion").saveAs("modelVersion")) // Add this line
                .check(jsonPath("$.id").notNull()));
    }

    {
        setUp(mainScn.injectOpen(atOnceUsers(2))).protocols(httpProtocol); 
    }
}

生成以下curl请求序列:

curl -X GET "https://aml-ar-reporting.apps.stl.bnw-dev-pas.mycompany.int/notifications/00000000-0000-6000-7000-000000000000/status" \
 -H "accept: application/json" \
 -H "X-AmlArReporting-CID: 125283" \
 -H "X-AmlArReporting-ICAs: 31931,5655,20929,9529,31627"

curl -X POST "https://aml-ar-reporting.apps.stl.bnw-dev-pas.mycompany.int/notifications/00000000-0000-6000-7000-000000000000/status" \
 -H "accept: application/json" \
 -H "X-AmlArReporting-CID: 125283" \
 -H "X-AmlArReporting-ICAs: 31931,5655,20929,9529,31627" \
 -H "Content-Type: application/json" \
 --data-binary '{"comment":"","fiFlaggedStatus":"FLAGGED","investigationAssessment":"WORTHY","investigationStatus":"COMPLETED","panRiskScore":986,"modelVersion":"1","created":"2026-07-02T08:20:38.814Z","id":""}'

curl -X POST "https://aml-ar-reporting.apps.stl.bnw-dev-pas.mycompany.int/notifications/00000000-0000-6000-7000-000000000000/status" \
 -H "accept: application/json" \
 -H "X-AmlArReporting-CID: 125283" \
 -H "X-AmlArReporting-ICAs: 31931,5655,20929,9529,31627" \
 -H "Content-Type: application/json" \
 --data-binary '{"comment":"","fiFlaggedStatus":"FLAGGED","investigationAssessment":"WORTHY","investigationStatus":"COMPLETED","panRiskScore":986,"modelVersion":"1","created":"2026-07-02T08:20:38.814Z","id":""}'

但是,我需要使用不同的模型版本执行发布状态(在请求之间增加 1 个整数),这样它就不会因 409 冲突而失败。 对于初始值,它应该在数据库中已有的值的基础上加 1,该值作为获取状态 api 的一部分进行检索。该值随后也是发布状态请求的响应的一部分。然而,从curl请求可以看出,两个post请求都是使用相同的模型版本执行的,我不知道如何绕过它。

谢谢你。

java gatling
1个回答
0
投票

我对你的场景中的流程和步骤有疑问:

  1. mainScn.injectOpen(atOnceUsers(2))
    这意味着它将启动两个独立的用户,每个用户都会发出两个请求:
    GET
    POST
    。所以,它并不像您在示例中使用curl 所示的那样。
  2. 您必须重新考虑您的场景以及它应如何加载系统。
  3. 如果
    GET
    请求始终返回 modelVersion
    1
    ,则提取此值没有意义。
  4. 尝试定义一个 global
    AtomicInteger
    并每次为 POST 请求获取它的值:
AtomicInteger counter = new AtomicInteger(1);
counter.getAndIncrement();
© www.soinside.com 2019 - 2024. All rights reserved.