Fine tune the request response count

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:

  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)
    }
  }

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 and how to ask a question topics.
[Y] I provided a SSCCE (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.

Gatling version : 3.9.5

Way too old, you should upgrade.

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

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

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.