Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gatling exec with session

Tags:

scala

gatling

I need to make a request in Gatling, in which I'm able to access session items (without the expression language). I need to do this, because I want to inject data into a ByteArrayBody request from a csv feeder. To demonstrate my problem, I have a small example (without the actual need of the session).

The following scenario runs fine:

val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
  exec(http("Http Test test").get("http://google.de/"))
}

But that one doesn't (I get the exception There were no requests sent during the simulation, reports won't be generated):

val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
  exec(session => {
    http("Http Test test").get("http://google.de/")
    session
  })
}

I run my simulations in IntelliJ (which worked fine so far) and in the following (here minimized) simulation file:

package test.scala

import java.text.SimpleDateFormat
import java.util.Date

import io.gatling.core.Predef._
import io.gatling.core.body.ByteArrayBody
import io.gatling.core.structure.ScenarioBuilder
import io.gatling.http.Predef._
import io.gatling.http.protocol.HttpProtocolBuilder
import org.slf4j.LoggerFactory
import test.scala.TerminalTesterRequest.url
import test.scala.requests._
import test.scala.util.CharsetConverter

import scala.concurrent.duration._
import scala.language.postfixOps

class MySimulation extends Simulation {

  //base URL (actually this URL is different, but it's not important)
  val ecmsServerUri = "http://0.0.0.0"

  //base Protocol
  val httpProtocol: HttpProtocolBuilder = http
    .baseUrl(ecmsServerUri)
    .inferHtmlResources(BlackList(""".*\.js""", """.*\.css""", """.*\.gif""", """.*\.jpeg""", """.*\.jpg""", """.*\.ico""", """.*\.woff""", """.*\.(t|o)tf""", """.*\.png"""), WhiteList())
    .acceptHeader("*/*")
    .acceptEncodingHeader("gzip, deflate")
    .acceptLanguageHeader("en,en-US;q=0.7,de-DE;q=0.3")
    .userAgentHeader("Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 2.0.50727.8762)")

  val scnBase: ScenarioBuilder = scenario("Test scneario").repeat(1){
    exec(session => {
      http("Http Test test").get("http://google.de/")
      session
    })
  }

  setUp(
    scnBase.inject(constantUsersPerSec(1) during(1 seconds)).protocols(httpProtocol)
  ).maxDuration(5 minutes)
}

How can I run an exec request with the information of the session (or at least the data from the feeder)? I'm using Gatling 3.1.1

like image 640
MetaColon Avatar asked Apr 29 '19 16:04

MetaColon


People also ask

What is a session in Gatling?

Session is a virtual user's state. Basically, it's a Map<String, Object> : a map with key Strings. In Gatling, entries in this map are called Session attributes. A Gatling scenario is a workflow where every step is an Action .

What is exec Gatling?

Actions are usually requests (HTTP, LDAP, POP, IMAP, etc) that will be sent during the simulation. Any action that will be executed will be called with exec . For example, when using the Gatling HTTP module you would write the following line: Java Kotlin Scala. // attached to a scenario scenario("Scenario") .

How do I send a post request on Gatling?

Gatling POST Syntaxpost("request-url"). formParam("key", "value") , To post a multipart request, for instance by uploading a file: http("Multipart POST"). post("request-url").


1 Answers

Build whatever you need in a function and put the result in the session, then refer that value in the actual request

val feeder = csv("foo.csv")

scenario("Test scenario")
  .feed(feeder)
  .exec(buildPostData)
  .exec(http("Http Test test")
    .post(createApiURL)  
    .body(ByteArrayBody("${postData}"))
    .check(status.is(200))
  )

def buildPostData: Expression[Session] = session => {
  val postData: Array[Byte] = 
    ... // getting values from csv record: session("csvHeader").as[String]
  session.set("postData", postData)
}
like image 78
Camilo Silva Avatar answered Oct 07 '22 21:10

Camilo Silva