How to send one more request the same time

Hello everyone.
I must be send 2 request the same time.
I must get “trid” in the response of first request .
I must use the “trid” in the second request.
Single test is ok.
When I do load test.
I have 2 problem.
One problem is some times first request does not work correctly and I cant get “trid”.
Second problem is Some times First request work, but work multipal time then work second request. This time only I get of the last request “trid”.

I want to the request work the as below example

  1. work first request
  2. I must get the “trid”
  3. I must use “trid” on the second request.
  4. must be work the second request

My code is below

Please help me

package com.gatling.tests.Alltest

import com.google.common.base.Charsets.UTF_8
import com.google.common.hash.Hashing
import com.google.common.io.BaseEncoding
import io.gatling.core.Predef.*
import io.gatling.core.scenario.Simulation
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef.*
import io.gatling.http.response.*
import io.gatling.http.response.NoResponseBody.Empty.bytes
import io.gatling.jdbc.Predef.*
import org.apache.commons.*

import java.io.{FileOutputStream, PrintWriter}
import java.nio.charset.StandardCharsets
import java.nio.charset.StandardCharsets.UTF_8
import java.security.*
import java.text.SimpleDateFormat
import java.time.{LocalDateTime, LocalTime}
import java.util.{Base64, Calendar, Random, TimeZone}
import javax.crypto.SecretKey
import javax.crypto.spec.SecretKeySpec
import javax.xml.bind.DatatypeConverter
import javax.xml.ws.ResponseWrapper
import scala.concurrent.duration.*
import scala.language.postfixOps

class Alltest extends Simulation {

  val response_writer = {
    val fos = new FileOutputStream("src/test/resources/data/writeResponse.csv")
    new PrintWriter(fos, true)
  }

  val response_writer4 = {
    val fos = new FileOutputStream("src/test/resources/data/writeResponseData.csv")
    new PrintWriter(fos, true)
  }

  val format = new SimpleDateFormat("yyyyMMddHHmmss")
  format.setTimeZone(TimeZone.getTimeZone("UTC"))

  val format_date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS")
  format_date.setTimeZone(TimeZone.getTimeZone("GMT+4"))

  private val httpProtocol = http
    .baseUrl("https://test.ss.com")

  private val headers_0 = Map(
    "sec-ch-ua" -> """Chromium";v="104", " Not A;Brand";v="99", "Google Chrome";v="104"""
  )

  val age = "30"
  val name = "Test Name"
  val description = "Test Request"


  var request_date = ""
  var time = ""
  var nonce = ""
  var calculate_text = ""
  var date_time = ""
  var text = ""
  var desc = ""
  var trid = ""

  val first_req = scenario("auth_0")
    .exec(session => {
      date_time = format_date.format(Calendar.getInstance.getTime).toString()
      request_date = format.format(Calendar.getInstance.getTime).toString() + Random().nextInt(999999).toString()
      time = format.format(Calendar.getInstance.getTime).toString()
      nonce = Random().nextInt(999999).toString()
      text = name.length + name + age.length + age + description.length + description + request_date.length + request_date + time.length + time + nonce.length + nonce
      calculate_text = date_time + request_date + time + nonce + text + age + name + description
      session
    })
    .exec(
      http("request_0")
        .post("/test/test")
        .headers(headers_0)
        .formParam("age", _ => age)
        .formParam("name", _ => name)
        .formParam("description", _ => description)
        .formParam("date_time", _ => date_time)
        .formParam("time", _ => time)
        .formParam("nonce", _ => nonce)
        .formParam("calculate_text", _ => calculate_text)

        .check(bodyString.saveAs("resp"))
        .check(xpath("//desc").saveAs("desc"))
        .check(xpath("//action").saveAs("action"))
        .check(xpath("//trid").saveAs("trid"))

        .check(status.is(200))
        .check(xpath("//desc").is("Approved"))
        .check(xpath("//action").is("0"))
    )
    .exec(session => {
      desc = session("desc").as[String]
      trid = session("trid").as[String]
      session
    })
    .exec(session => {
      response_writer.println(session("resp").as[String])
      session
    })
    .exec(session => {
      response_writer4.println("first_req, desc = " + desc + ", trid" + trid )
      session
    })

  var request_date_2 = ""
  var time_2 = ""
  var nonce_2 = ""
  var calculate_text_2 = ""
  var date_time_2 = ""
  var text_2 = ""
  var desc_2 = ""

  val second_req = scenario("auth_0")
    .exec(session => {
      date_time_2 = format_date.format(Calendar.getInstance.getTime).toString()
      request_date_2 = format.format(Calendar.getInstance.getTime).toString() + Random().nextInt(999999).toString()
      time_2 = format.format(Calendar.getInstance.getTime).toString()
      nonce_2 = Random().nextInt(999999).toString()
      text_2 = name.length + name + age.length + age + description.length + description + request_date_2.length + request_date_2 + time_2.length + time_2 + nonce_2.length + nonce_2
      calculate_text_2 = date_time_2 + request_date_2 + time_2 + nonce_2 + text_2 + age + name + description
      session
    })
    .exec(
      http("request_1")
        .post("/test/test")
        .headers(headers_0)
        .formParam("age", _ => age)
        .formParam("name", _ => name)
        .formParam("description", _ => description)
        .formParam("date_time", _ => date_time_2)
        .formParam("time", _ => time_2)
        .formParam("nonce", _ => nonce_2)
        .formParam("calculate_text", _ => calculate_text_2)
        .formParam("trid", _ => trid)

        .check(bodyString.saveAs("resp"))
        .check(xpath("//desc").saveAs("desc"))
        .check(xpath("//action").saveAs("action"))
        .check(xpath("//trid").saveAs("trid"))

        .check(status.is(200))
        .check(xpath("//desc").is("Approved"))
        .check(xpath("//action").is("0"))
    )
    .exec(session => {
      desc = session("desc").as[String]
      trid = session("trid").as[String]
      session
    })
    .exec(session => {
      response_writer.println(session("resp").as[String])
      session
    })
    .exec(session => {
      response_writer4.println("second_req, desc = " + desc + ", trid" + trid)
      session
    })
  
  val admins = scenario("Admins").exec(first_req, second_req)
  {
    setUp(
      admins.inject(rampUsers(4).during(10))
    ).protocols(httpProtocol)
  }
}

Oh! My!

So many things!!!

  • Why global variables?
  • Why var usage?
  • Why do you use scala language ? Do you know we support Java and Kotlin as well?
  • Did you follow the Gatling Academy?
  • Did you read (and understand) the tutorial?
  • A scenario should not contains another scenario (admins scenario contains first_req declared as a scenario)

So, below will be an example of what I think will work for you. It use the concept of:

  • Feeder (generate data for user)
  • Use data from session
import io.gatling.core.Predef._
import io.gatling.core.feeder.Feeder
import io.gatling.core.scenario.Simulation
import io.gatling.http.Predef._

import java.text.SimpleDateFormat
import java.util.{Calendar, TimeZone}
import scala.language.postfixOps
import scala.util.Random

class Alltest extends Simulation {

  val format = new SimpleDateFormat("yyyyMMddHHmmss")
  format.setTimeZone(TimeZone.getTimeZone("UTC"))

  val format_date = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss.SSS")
  format_date.setTimeZone(TimeZone.getTimeZone("GMT+4"))

  private val httpProtocol = http
    .baseUrl("https://test.ss.com")
  
  val Age = "30"
  val Name = "Test Name"
  val Description = "Test Request"

  def generateOneEntry: Map[String, String] = {
    val dateTime = format_date.format(Calendar.getInstance.getTime)
    val requestDate = format.format(Calendar.getInstance.getTime) + Random.nextInt(999999).toString
    val time = format.format(Calendar.getInstance.getTime)
    val nonce = Random.nextInt(999999).toString
    val text = Name.length + Name + Age.length + Age + Description.length + Description + requestDate.length + requestDate + time.length + time + nonce.length + nonce
    Map(
      "date_time" -> dateTime,
      "request_date" -> requestDate,
      "time" -> time,
      "nonce" -> nonce,
      "calculate_text" -> (dateTime + requestDate + time + nonce + text + Age + Name + Description)
    )
  }

  val myFeeder: Feeder[String] = Iterator.continually(generateOneEntry)

  val first_req = http("request_0")
        .post("/test/test")
        .formParam("age", Age)
        .formParam("name", Name)
        .formParam("description", Description)
        .formParam("date_time", "#{date_time}")
        .formParam("time", "#{time}")
        .formParam("nonce", "#{nonce}")
        .formParam("calculate_text", "#{calculate_text}")

        .check(bodyString.saveAs("resp"))
        .check(xpath("//desc").saveAs("desc"))
        .check(xpath("//action").saveAs("action"))
        .check(xpath("//trid").saveAs("trid"))

        .check(status.is(200))
        .check(xpath("//desc").is("Approved"))
        .check(xpath("//action").is("0"))

  val second_req = http("request_1")
        .post("/test/test")
        .formParam("age", Age)
        .formParam("name", Name)
        .formParam("description", Description)
        .formParam("date_time", "#{date_time}")
        .formParam("time", "#{time}")
        .formParam("nonce", "#{nonce}")
        .formParam("calculate_text", "#{calculate_text}")
        .formParam("trid", "#{trid}")

        .check(bodyString.saveAs("resp"))
        .check(xpath("//desc").saveAs("desc"))
        .check(xpath("//action").saveAs("action"))
        .check(xpath("//trid").saveAs("trid"))

        .check(status.is(200))
        .check(xpath("//desc").is("Approved"))
        .check(xpath("//action").is("0"))

  val admins = scenario("Admins")
    .feed(myFeeder)
    .exec(first_req)
    .feed(myFeeder)
    .exec(second_req)

  {
    setUp(
      admins.inject(rampUsers(4).during(10))
    ).protocols(httpProtocol)
  }
}

Hope it helps!
Cheers!

Thank you very much.
I wrote the same.
But some parameter was written below

content=age=30&name=Test&description=TestForm&date_time=%23%date_time%7D&time=%23%time%7D&nonce=%23%7Bnonce%7D&calculate_text=%23%7Bnonce%7D}
=========================

body:
<?xml version="1.0" encoding="utf-8" ?>
<response>
    <desc>Access denied</desc>
    <age>30</age>
    <name>Test</name>
    <description>TestForm</description>
    <date_time>#{date_time}</date_time>
    <time>#{time}</time> 
    <nonce>#{nonce}</nonce>
    <calculate_text>#{calculate_text}</calculate_text>
</response>

And second issue

Map(
“date_time” → dateTime,
“request_date” → requestDate,
“time” → time,
“nonce” → nonce,
“calculate_text” → (dateTime + requestDate + time + nonce + text + Age + Name + Description)
)

must be different the first request and second request.
only “request_date” must be same
forexample
First request
“date_time” → 20222209104500,
“request_date” → 20222209144500065489,
“time” → 20222209144500,
“nonce” → 568745
“calculate_text” → dvfdvld;fmdfbb56664561212f1bfgb123bf11fgbbfgb13fgbfgbfgbfbg

Second request
“date_time” → 20222209105540,
“request_date” → 20222209144500065489,
“time” → 20222209142314,
“nonce” → 668795
“calculate_text” → dvfdvld;fmdfbb56664661212f1bagb123bf11fgbbfgb13fgbfgbfgbfbg

Why do you use scala language ? Do you know we support Java and Kotlin as well?
Because I am new is gatling. And I watch gatling with scala on the youtube. I cant find with java

And second issue

So you should open another thread. If the original issue is resolved, please mark thread as resolved.

And I watch gatling with scala on the youtube

You should look after our official Gatling academy !

You say that write as below.

.formParam("date_time", "#{date_time}")

But when I write this code,
did not write parameter value
written as this code

nonce=%23%7Bnonce%7D

content=age=30&name=Test&description=TestForm&date_time=%23%date_time%7D&time=%23%time%7D&nonce=%23%7Bnonce%7D&calculate_text=%23%7Bnonce%7D}
=========================



body:
<?xml version="1.0" encoding="utf-8" ?>
<response>
    <desc>Access denied</desc>
    <age>30</age>
    <name>Test</name>
    <description>TestForm</description>
    <date_time>#{date_time}</date_time>
    <time>#{time}</time> 
    <nonce>#{nonce}</nonce>
    <calculate_text>#{calculate_text}</calculate_text>
</response>

I’m not sure to understand.

Are you saying that the value given in the form param is exactly this string #{date_time} when the expected value is the one stored in the session?

What is you gatling version? (The # syntax is pretty new)

Cheers!

Version is 3.8.3

I write as this code

But when I run a test
The response is seen as below

content=age=30&name=Test&description=TestForm&date_time=%23%date_time%7D&time=%23%time%7D&nonce=%23%7Bnonce%7D&calculate_text=%23%7Bnonce%7D}
=========================

body:
<?xml version="1.0" encoding="utf-8" ?>
<response>
    <desc>Access denied</desc>
    <age>30</age>
    <name>Test</name>
    <description>TestForm</description>
    <date_time>#{date_time}</date_time>
    <time>#{time}</time> 
    <nonce>#{nonce}</nonce>
    <calculate_text>#{calculate_text}</calculate_text>
</response>

is not written parameter value

For example
Must be

content=age=30&name=Test&description=TestForm&date_time=20222209140300456987&time=20222209140300&nonce=652896&calculate_text=30TestTestForm2022220914030045698720222209140300652896}
=========================

body:
<?xml version="1.0" encoding="utf-8" ?>
<response>
    <desc>Access denied</desc>
    <age>30</age>
    <name>Test</name>
    <description>TestForm</description>
    <date_time>20222209140300456987</date_time>
    <time>20222209140300</time> 
    <nonce>652896</nonce>
    <calculate_text>30TestTestForm2022220914030045698720222209140300652896</calculate_text>
</response>

Really, I don’t know how you where you obtain such XML.

The only thing I could manage to obtain is a 200 OK from the server (and the request contained valid values)

HTTP request:
POST https://test.ss.com/test/test
headers:
        accept: */*
        host: test.ss.com
        content-type: application/x-www-form-urlencoded
        content-length: 295
body:FormUrlEncodedRequestBody{patchedContentType='application/x-www-form-urlencoded', charset=UTF-8, content=age=30&name=Test+Name&description=Test+Request&date_time=22-09-2022+20%3A15%3A22.526&time=20220922161522&nonce=221440&calculate_text=22-09-2022+20%3A15%3A22.52620220922161522204773202209221615222214409Test+Name23012Test+Request20202209221615222047731420220922161522622144030Test+NameTest+Request}
=========================
HTTP response:
status:
        200 OK
headers:
        Server: Internet
        Date: Thu, 22 Sep 2022 16:15:22 GMT
        Content-Type: text/html; charset=UTF-8
        Transfer-Encoding: chunked
        Connection: keep-alive
        Vary: Accept-Encoding
        P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"
        Set-Cookie: PHPSESSID=e78f4dd2031e239728cc01114476e8e3; path=/; SameSite=none; Secure
        Expires: Thu, 19 Nov 1981 08:52:00 GMT
        Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
        Pragma: no-cache
        Set-Cookie: LG=lv; expires=Fri, 22-Sep-2023 16:15:22 GMT; path=/; domain=www.ss.com; SameSite=none; secure;

body:
<!DOCTYPE html>
<HTML><HEAD>
<title>Sludinājumi - SS.COM</title>
<meta http-equiv="Content-Type" CONTENT="text/html; charset=UTF-8">
<meta name="viewport" content="user-scalable=1, width=device-width, initial-scale=1.0"/>
<meta name="keywords" content="Sludinājumi pārdošana pirkšana" />
<meta name="description" content="Sludinājumi Latvija un Rīga. Vislielākais sludinājumu serveris Latvijā. Vieglie auto, vakances un darbinieku meklēšana, kravas automašīnas, motocikli un velosipēdi, autoserviss un rezerves daļas. Аpmācība un kursi, kredīti un līzings. Nekustamā īpašuma cenas un vērtība Eiro (Eur) - dzīvokļi, mājas, zeme, mežs, zemes gabali, telpu noma, veikali. Pārdošana telefoni, foto un datori, drēbes, apavi, preces bērniem. Lauksaimniecība - dzīvnieki un tehnika, pārtikas produkti un apstrāde. Instrumenti un būvtehnika, būvmateriāli un materiāli ēku remontam. Sludinājumi par veselību un skaistumkopšanu, kosmētika, manikīrs, makijāža un medicīna. Mājdzīvnieki suņi, kaķi, tirdzniecība, atrasts un pazaudēts. Tūrisms, sports un atpūta. Biļetes, monētas, markas, nozīmītes, ordeņi, grāmatas, pulksteņi un zelts. Bezmaksas un maksas sludinājumi un reklāma.">
<meta http-equiv="imagetoolbar" content="no">
<meta name="verify-v1" content="KEj5wVQ9GyrN5I293Fluph86F8iCkIiDYWbIU2CqrsU="><meta name="google-site-verification" content="5wyLV6s1QGmQriz0WyvVzL3nsSg_kyu455JlGDg7b3A" /><meta name="msvalidate.01" content="08892626B3F398A88FC239518D209C93">
<BASE href="https://www.ss.com/">
<link href="https://i.ss.com/w_inc/style.css?v=229" rel="stylesheet" />
<style>html{overflow-y:scroll;}.page_bg{background-image:url('https://i.ss.com/img/head/classifieds.jpg?v=1' );background-repeat:no-repeat;}@media screen and (min-width:1280px){.page_bg{background-image:url('https://i.ss.com/img/head/classifieds-1280.jpg?v=1' );}}
</style>
<link rel="shortcut icon" href="/favicon.ico?v=1" type="image/x-icon" />
<link rel="apple-touch-icon" sizes="57x57" href="/img/m/ss-com-57x57.png"/>
<link rel="apple-touch-icon" sizes="72x72" href="/img/m/ss-com-72x72.png"/>
<link rel="apple-touch-icon" sizes="114x114" href="/img/m/ss-com-114x114.png"/>
<link rel="alternate" hreflang="lv" href="https://www.ss.com/lv/test/test" /><link rel="alternate" hreflang="ru" href="https://www.ss.com/ru/test/test" />
<script src="https://i.ss.com/w_inc/js/main.lv.ss.js?v=575" type="text/javascript"></script>

<SCRIPT type="text/javascript">
<!--
LINK_MAIN_HOST = "https://www.ss.com";var REGION = "";SS_Lang = "2";get_page_zoom();
if(window._setCookie){_setCookie("LG","lv",365,"www.ss.com");};var MAIN_VALUTA = "";if(window._check_remote_id){_check_remote_id( "new", "https://www.ss.lv" );}
-->
</SCRIPT>
</HEAD>
<BODY onLoad="window.defaultStatus='Sludinājumi - SS.COM ,Load - 7.80ms incl - 6.78ms mod - 1.00ms ';" bgcolor="#FFFFFF" class="body">

<div align=center class="noprint">
        <div id="main_table" class="page_header page_bg">
                <div z-index="20" id="sslogin"></div>
                <span class="page_header_head"><a href="/lv/" title="Sludinājumi"><img class="page_header_logo" src="https://i.ss.com/img/p.gif" border="0" alt="Sludinājumi"></a><h1>SLUDINĀJUMI</h1></span>
                <span class="page_header_menu"></span>
                <span id="mails_status_menu"></span>
                <span class="menu_lang"><div class="menu_langs"><a href="/ru/test/test" class=a_menu title="По-русски">RU</a></div>&nbsp;<div class="menu_langs"><a href="/en/test/test" class=a_menu title="English">EN</a></div></span>
        </div>
</div>
        <div class="onlyprint">
                <img src="https://i.ss.com/img/ss.com_green.svg" style="height: 1cm;">
        </div>
<div align=center>
<div class="page_header">
        <div id="page_main_full"><br><br><table width=100% cellpadding=2 cellspacing=0 border=0><tr><td align=center>
                <table align=center cellpadding=10 cellspacing=0 border=0 width=100% style="border:1px #418c4e solid;">
                        <tr><td align=center >Страница по указанному адресу выключена.</td></tr>
                        </table>
                </td></tr></table><br></div>
        
        <div id="page_footer" class="noprint"><a class="a1" href="/lv/rules/">Noteikumi</a>  &nbsp;|&nbsp;  <a class="a1" href="/lv/feedback/">Saikne ar redaktoru</a>  &nbsp;|&nbsp;  <a class="a1" href="/lv/reklama/">Reklāma</a>  &nbsp;|&nbsp;  <a class="a1" href="/lv/api/">Sadarbība</a> &nbsp;|&nbsp; Sludinājumi © ss sia 2000</div>
        <iframe src="https://i.ss.com/img/p.gif" width=0 height=0 frameborder=0 style="display:none" id="ss_mframe" name="ss_mframe"></iframe>
</div>
</div>

<SCRIPT type="text/javascript">
<!--
if( window.add_link_to_selection && document.body.addEventListener ){document.body.addEventListener( "copy", add_link_to_selection );}
load_script_async( "https://i.ss.com/w_inc/js/msg.count-ss.js?"+new Date() );
load_script_async( "/w_inc/chk.php?mm=1&c=&db=lv&mode=0&g=1" );
-->
</SCRIPT>

<div style="display:none;">
<img src="/counter/index.php?0" width=1 height=1 border=0 alt="">

<script async src="/w_inc/gcntr.php?id=ss"></script>

<!-- puls.lv START //-->
<div id="_puls.lv_232-26935-27307-27309" style="display:inline-block;"></div>
<script type="text/javascript">_puls_counter_local( "232-26935-27307-27309" );</script>
<noscript>
<a href="http://puls.lv/" target="_blank"><img src="https://hits.puls.lv/?sid=232-26935-27307-27309&jsver=0" width=1 height=1 border=0 alt=""></a>
</noscript>
<!-- puls.lv END //-->
<!-- europuls.eu START //-->
<div id="_europuls.eu_2" style="display:inline-block;"></div>
<script type="text/javascript">_ps_counter_local(2);</script>
<noscript>
<a href="http://europuls.eu/" target="_blank"><img src="https://hits.europuls.eu/?sid=2&jsver=0"  width=1 height=1 border=0 alt=""></a>
</noscript>
<!-- europuls.eu END //-->
<img src="https://top.lv/counter.php?sid=2774&type=4" width="1" height="1" border="0" style="display:none" alt="">

</div>
</BODY>
</HTML>

The request failed because it cannot find the trid element. But that’s all.

How do you print your values?
Do you still use your non-working Printwriter from your first code?

Cheers!

Thank you very much your answer