2

I need to update .csv file containing some ids used within Gatling simulation, as I need to create data beforehand. I tried to update that file within before() call however it won't work. Lazy evaluation won't work either.

  before {
    Helper.CreateDocuments()
  }

  lazy val documentIds = csv("data/documentIds.csv").circular

  val scn: ScenarioBuilder = scenario("PutFile")
    .feed(documentIds)
    .exec(http("Dynamic id")
      .put("files/${documentId}"))...

How can I solve that issue to feed simulation with refreshed ids?

enzor
  • 47
  • 4
  • Hi @enzor, I am also in same situation. Did you get the answer how to update or create a .csv file before simulation starts – ppb Mar 30 '21 at 22:44

3 Answers3

1

I've faced similar issue, and then I decided to not use CSV, but implicit feeders and to create a method to build my feeder. I wasn't able to fix that by using the before hook, so created a builder method for my feeder. An example on how it would work.

import io.gatling.core.Predef._
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef._
import org.apache.http.client.methods.{HttpGet, HttpPost}
import org.apache.http.entity.StringEntity
import org.apache.http.impl.client.HttpClientBuilder
import org.apache.http.util.EntityUtils
import spray.json.DefaultJsonProtocol._
import spray.json._

object TestFacade {    
var test_feeder = getFeeder()
    
      val test_scenario: ChainBuilder = feed(test_feeder).group("TEST_GROUP") {
        exec(
          http("POST /test")
            .post("/test")
            .header("Content-Type", Configuration.APPLICATION_JSON)
            .body(ElFileBody("test.json"))
            .asJSON
            .check(status.find.in(200))
        )
      }
    
      def getFeeder() = {
        val result = getRestContent("http://www.randomnumberapi.com/api/v1.0/random?min=100&max=1000&count=5")
    
        val ids = result.parseJson.convertTo[Array[Int]]
        var coolVars = Array.empty[Map[String, String]]
    
        for (id <- ids) {
          println(id)
          coolVars = coolVars :+ Map("coolVar" -> id.toString)
        }
    
        coolVars.random
      }
      
      def getRestContent(url: String): String = {
        val httpClient = HttpClientBuilder.create().build()
        val httpResponse = httpClient.execute(new HttpGet(url))
        val entity = httpResponse.getEntity
        var content = ""
        if (entity != null) {
          content = EntityUtils.toString(entity, "UTF-8")
        }
        httpClient.getConnectionManager.shutdown()
    
        content
      }
}
  • Thank you so much! It works perfectly. I used another rest client, and it worked just fine. Taking the feeder initialization out of the scenario is very elegant. – Dekel Jul 09 '21 at 16:38
0

just use initializing method to create the file, it will process before all stuff and should work

  /**
   * customize before
   */
  {
    Helper.CreateDocuments()
  }


Stéphane LANDELLE
  • 6,076
  • 2
  • 10
  • 12
Benny Hsieh
  • 21
  • 1
  • 4
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 18 '23 at 10:09
-1

You should be able to modify a csv file in the before step. For example:

  before {
    val pw = new PrintWriter(new File("data.csv"))
    for (i <- 0 to 10) {
      pw.write(i + "\n")
    }
    pw.close
  }

Remember to import the library import java.io._

And then use it in your simulation calling the file:

val orderId = csv("data.csv").queue

However this might be redundant as you can create the values an use the saveAs step, as mentioned in the Gatling Session API documentation. For example you can do something like:

val someHttpCall = http("Create data for the feeder").get("/my/resource").saveAs("data")

And then use in the feeder:

val scn = feed(data).exec(somethingElse)

If this is not enough, remember that you can also create you own feeder at any point in the simulation class.

hfc
  • 614
  • 1
  • 5
  • 13