创建一个动态的POST请求,使用Session API以编程方式。

huangapple go评论72阅读模式
英文:

Gatling :Create a POST Request Dynamically using Session API programatically

问题

以下是翻译好的部分:

我正在对一个API进行负载测试,并希望在不同的POST请求中注入不同的数据。我查看了Gatling的Session API,并尝试以下代码。但我一直在收到错误消息:11:49:30.717

[gatling-1-16] ERROR io.gatling.http.action.HttpRequestAction - 'create-profiles' 执行失败:未定义名为 'createProfile' 的属性

会话代码:

if (WRITES_PER_SECOND > 0) {
   scenarios.add(CoreDsl.scenario(CREATE_PROFILE)
            .feed(listFeeder(getFeed(noOfEntityIdsNeeded)).circular())
            .feed(listFeeder(TKN_FEED).circular())
            .exec(session -> {
                session.set("createProfile", PayloadFeeder.getPayload());
                return session;
            })
            .exec(http("create-profiles").post(PROFILE_API_URL)
                    .header(CONTENT_TYPE_HEADER, MIME_TYPE_APPLICATION_JSON)
                    .header(AUTHORIZATION_HEADER, TOKENIZED_ACCESS_TOKEN)
                    .header(X_APP_KEY_HEADER, X_APP_KEY_123_REG)
                    .header(IDEMPOTENCY_KEY_HEADER, ENTITY_ID_PLACEHOLDER)
                    .body(StringBody("${createProfile.jsonStringify()}")))
                    //.body(StringBody(session -> session.getString("postData"))))
            .injectClosed(
                    constantConcurrentUsers(WRITES_PER_SECOND).during(Duration.ofSeconds(duration)))
            .throttle(jumpToRps(WRITES_PER_SECOND), holdFor(Duration.ofSeconds(duration))));
}

session.set("createProfile", PayloadFeeder.getPayload());
是我调用一个函数并获得动态载荷的地方。
然后我尝试在下面的部分中使用它,使用session.get()

我还尝试过session.set("postData","jsonstringdynamic")和session.getString("postData"),但是Gatling找不到该值并在HTTP正文中注入NULL。

寻找一些关于这个问题的指导

希望这能帮助你理解代码问题并寻找解决方案。

英文:

I am load testing a API and I want to inject different data in different post Requests. I went through Session API of gatling and I am trying following code.But i keep on getting error 11:49:30.717

[gatling-1-16] ERROR io.gatling.http.action.HttpRequestAction - 'create-profiles' failed to execute: No attribute named 'createProfile' is defined

Code for session:

  if (WRITES_PER_SECOND > 0) {
     scenarios.add( CoreDsl.scenario(CREATE_PROFILE)
              .feed(listFeeder(getFeed(noOfEntityIdsNeeded)).circular())
              .feed(listFeeder(TKN_FEED).circular())
              .exec(session -> {
                session.set("createProfile",*PayloadFeeder.getPayload()*);
                return session;
              })
              .exec(http("create-profiles").post(PROFILE_API_URL)
                      .header(CONTENT_TYPE_HEADER, MIME_TYPE_APPLICATION_JSON)
                      .header(AUTHORIZATION_HEADER, TOKENIZED_ACCESS_TOKEN)
                      .header(X_APP_KEY_HEADER, X_APP_KEY_123_REG)
                      .header(IDEMPOTENCY_KEY_HEADER, ENTITY_ID_PLACEHOLDER)
                      .body(StringBody("${createProfile.jsonStringify()}")))
                      //.body(StringBody(session -> session.getString("postData"))))
              .injectClosed(
                      constantConcurrentUsers(WRITES_PER_SECOND).during(Duration.ofSeconds(duration)))
              .throttle(jumpToRps(WRITES_PER_SECOND), holdFor(Duration.ofSeconds(duration))));

    }


            session.set("createProfile",*PayloadFeeder.getPayload()*);

is where I call a function and get dynamic payload.
then I try to use this in below section,using session.get()

I also tried with session.set("postData,"jsonstringdynamic")
and session.getString("postData") but gatling does not find the value and injects NULL in http body.

Looking for some guidance on this

答案1

得分: 1

.exec(session -> {
  session.set("createProfile",*PayloadFeeder.getPayload()*);
  return session;
})

这是错误的。请阅读会话 API 文档,会话实例是不可变的。

英文:
.exec(session -> {
  session.set("createProfile",*PayloadFeeder.getPayload()*);
  return session;
})

This is wrong. Please read the Session API documentation, Session instances are immutable.

答案2

得分: 0

我错过了阅读文档时的关键信息。由于会话 API 是不可变的,我们必须返回新的会话。

.exec(session -> {
               Session newSession= session.set("postData",PayloadFeeder.getDynamicDataString());
                return newSession;
              })

就是这样。

修改后的代码

scenarios.add(CoreDsl.scenario(CREATE_PROFILE)
                  .feed(listFeeder(getFeed(noOfEntityIdsNeeded)).circular())
                  .feed(listFeeder(TKN_FEED).circular())
                  .exec(session -> {
                   Session newSession= session.set("postData",PayloadFeeder.getDynamicDataString());
                    return newSession;
                  })
                  .exec(http("create-profiles").post(PROFILE_API_URL)
                          .header(CONTENT_TYPE_HEADER, MIME_TYPE_APPLICATION_JSON)
                          .header(AUTHORIZATION_HEADER, TOKENIZED_ACCESS_TOKEN)
                          .header(X_APP_KEY_HEADER, X_APP_KEY_123_REG)
                          .header(IDEMPOTENCY_KEY_HEADER, ENTITY_ID_PLACEHOLDER)
                          .body(StringBody(session -> {
                                  boolean contains=  session.contains("postData");
                                  System.out.println("is property contains"+"["+contains+"]");
                                  String data=session.getString("postData");
                                  System.out.println("data is"+data);
                                   return data ;
                                  })))
                  .injectClosed(
                          constantConcurrentUsers(WRITES_PER_SECOND).during(Duration.ofSeconds(duration)))
                  .throttle(jumpToRps(WRITES_PER_SECOND), holdFor(Duration.ofSeconds(duration))));
英文:

I missed crucial info while reading docs. We have to return newSession since session API is immutable.
.exec(session -> {
Session newSession= session.set("postData",PayloadFeeder.getDynamicDataString());
return newSession;
})

that was it.

ModifiedCode

scenarios.add(CoreDsl.scenario(CREATE_PROFILE)
                  .feed(listFeeder(getFeed(noOfEntityIdsNeeded)).circular())
                  .feed(listFeeder(TKN_FEED).circular())
                  .exec(session -> {
                   Session newSession= session.set("postData",PayloadFeeder.getDynamicDataString());
                    return newSession;
                  })
                  .exec(http("create-profiles").post(PROFILE_API_URL)
                          .header(CONTENT_TYPE_HEADER, MIME_TYPE_APPLICATION_JSON)
                          .header(AUTHORIZATION_HEADER, TOKENIZED_ACCESS_TOKEN)
                          .header(X_APP_KEY_HEADER, X_APP_KEY_123_REG)
                          .header(IDEMPOTENCY_KEY_HEADER, ENTITY_ID_PLACEHOLDER)
                          .body(StringBody(session -> {
                                  boolean contains=  session.contains("postData");
                                  System.out.println("is property contains"+"["+contains+"]");
                                  String data=session.getString("postData");
                                  System.out.println("data is"+data);
                                   return data ;
                                  })))
                  .injectClosed(
                          constantConcurrentUsers(WRITES_PER_SECOND).during(Duration.ofSeconds(duration)))
                  .throttle(jumpToRps(WRITES_PER_SECOND), holdFor(Duration.ofSeconds(duration))));

huangapple
  • 本文由 发表于 2023年6月1日 14:27:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76379195.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定