# Fine tune the request response count

**URL:** <https://community.gatling.io/t/fine-tune-the-request-response-count/9516>\
**Category:** Gatling (Open-Source)\
**Tags:** gatling-academy\
**Created:** [January 22, 2025, 7:27am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516 "2025-01-22T07:27:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [January 22, 2025, 7:27am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/1 "2025-01-22T07:27:10Z")

</div>

**Gatling version** : 3.9.5  
**Gatling flavor** : scala  
**Gatling build tool** : maven  
Hi Team,  
I have an requirement to capture total request and response count at the end of the result.  
I have two shared variables to capture the count and one scenario to print the count at last after all the scenarios completed.

But when I run the test, I am getting non equal count between request and response.

Below attached the code snippet and screenshot:

```auto
  def logRequestForRec(req: HttpRequestBuilder, grpName: String, reqMethod: String, writer: FileOutputStream, queryParam: String, isLogNeeded: Boolean = true): ChainBuilder ={
    doIfOrElse(isLogNeeded) {
      exec(session => {
        val startTime: Long = getUTCOffsetInMS
        MessagePasser.requestCount += 1
        session.setAll(Map("startTime" -> startTime))
      })
        .exec(req)
        .exec(session => {
          val startTimeUTCMillis: Long = session("startTime").as[Long]
          val endTimeUTCMillis: Long = session("startTime").as[Long] + session("responseTime").as[Int]
          val tpaasReqId = if(session.contains("tpaas-request-id")) session("tpaas-request-id").as[String] else "_"
          val reqUrl = ReqNameMatch.getReqNameMatch(session,queryParam)
          val responseBody = session("responseBody").as[String]
          MessagePasser.responseCount += 1
          val logStr = s"$tpaasReqId;$grpName;$reqUrl;$reqMethod;_;$startTimeUTCMillis;$endTimeUTCMillis;${session("responseTime").as[Int]};${PerfConfig.testName};_;$responseBody;${session("responseStatus").as[Int]}\n"
          writer.write(logStr.getBytes(), 0, logStr.length)
          session
        })
    }
    {
      exec(req)
    }
  }

```

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/5/52ff8a71112f54e392380fcb40a200be249b6e56.png)

Can you please confirm that the mismatch in count is because of scenarios were terminated on it reaches the duration set in “ **during** ” method?

I made sure I’ve update my Gatling version to the latest release  
[Y] I read the [guidelines](https://community.gatling.io/t/welcome-in-the-gatling-community-our-code-of-conduct/6462) and [how to ask a question](https://community.gatling.io/t/how-to-ask-a-question/6538) topics.  
[Y] I provided a [SSCCE](http://www.sscce.org/) (or at least, all information to help the community understand my topic)  
[Y] I copied output I observe, and explain what I think should be.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [January 22, 2025, 8:11am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/2 "2025-01-22T08:11:41Z")

</div>

> **Gatling version** : 3.9.5

Way too old, you should upgrade.

---

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [January 22, 2025, 9:04am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/3 "2025-01-22T09:04:55Z")

</div>

Hi @slandelle , sure I will update it. But, for the above scenario, I hope it is not related to the version.

---

<div class="post-metadata">

**Author:** ![Zuhair](https://avatars.discourse-cdn.com/v4/letter/z/f475e1/32.png) [@Zuhair](https://community.gatling.io/u/Zuhair)\
**Post date:** [January 22, 2025, 9:10am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/4 "2025-01-22T09:10:20Z")

</div>

Actually, the Gatling version we are using in our project is 3.11.3, sorry for the above misinformation.

---

<div class="post-metadata">

**Author:** ![slandelle](https://dub1.discourse-cdn.com/flex013/user_avatar/community.gatling.io/slandelle/32/4_2.png) [@slandelle](https://community.gatling.io/u/slandelle)\
**Post date:** [January 22, 2025, 10:00am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/5 "2025-01-22T10:00:45Z")

</div>

> Can you please confirm that the mismatch in count is because of scenarios were terminated on it reaches the duration set in “ **during** ” method?

Can’t help with this here, sorry. This would require investigating your custom code, which means professional services.

What I can say is that you seem to be performing concurrent updates on non-threadsafe counters. You should be using something like `AtomicInteger`.

---

<div class="post-metadata">

**Author:** ![system](https://europe1.discourse-cdn.com/flex013/uploads/gatling2/original/2X/b/b148e0742de35c6280726cabf3ecd3de2d320f63.png) [@system](https://community.gatling.io/u/system)\
**Post date:** [February 21, 2025, 10:00am UTC](https://community.gatling.io/t/fine-tune-the-request-response-count/9516/6 "2025-02-21T10:00:57Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
