How to write custom feeder for json holiday pattern

I wrote a simple tutorial application that offers a REST interface called the "endpoint of the world" that you can use to add residents, etc. An example json request for this might look like this:

{ "name": "Sergio Gonzales", "age": "34", "languages": [ { "id": "119" }, { "id": "14" } ], "homeland": { "id": "121" }, "hometown": { "id": "155" } } 

I would like to add a gatling boot test to test the โ€œcreationโ€ of new residents. To get the data, I have three different sources:

  • first_names.csv
  • last_names.csv
  • country_capital_language.csv

The first two will be used to create random names (its, of course, not a very complicated approach for creating meaningful data). The latter will be used to select identifiers for the homeland, hometown and native language. In addition, I will select additional 0 - 3 languages โ€‹โ€‹at random.

I would suggest that I need to write my own feeder for this, but unfortunately the documentation for the custom feeder seems to be missing after the 2.xx release

What would be a good approach to writing a feeder? My first thought was to download csv data directly as follows:

Source.fromInputStream (getClass.getResourceAsStream ("/name/first_names.csv")). GetLines.toSet

Not sure if using csv ("first_names.csv") would be a better approach?

In addition, I also do not know how I can replace the "Languages" section in json with dynamically generated data? Is it possible to pass a list of language identifiers and automatically translate into a valid json array?

UDPATE

Here is my first working version. This has flaws, but it basically does what I want. If anyone has any suggestions for improvement, please feel free to (I'm pretty new to scala).

 package com.u6f6o.apps.hazelnate.load.scenario import io.gatling.core.Predef._ import io.gatling.core.feeder.Record import io.gatling.http.Predef._ import scala.concurrent.forkjoin.ThreadLocalRandom class Insert100kInhabitants extends Simulation { val random = ThreadLocalRandom.current val footprints = csv("data/footprints.csv").records val forenames = csv("data/forenames.csv").records val surnames = csv("data/surnames.csv").records val httpConf = http .baseURL("http://localhost:4567") .acceptHeader("application/json") .doNotTrackHeader("1") val scn = scenario("Insert100kInhabitants").repeat(10000){ exec{ session => val footprintRecords = chooseRandomly(footprints, 5) session.setAll( "forename" -> chooseRandomly(forenames).getOrElse("forename", ""), "surname" -> chooseRandomly(surnames).getOrElse("surname", ""), "age" -> random.nextInt(1, 110), "hometown" -> footprintRecords.head.getOrElse("city", ""), "homeland" -> footprintRecords.head.getOrElse("country", ""), "languages" -> footprintRecords.map{ x => x.getOrElse("language", "")} ) } .exec(http("insert100kInhabitants") .post("/world/inhabitants") .body(StringBody( session => generateJson(session))).asJSON ) } setUp( scn.inject(atOnceUsers(10)) ).protocols(httpConf) def generateJson(session:Session) : String = { s"""{ | "name": "${session("forename").as[String]} ${session("surname").as[String]}", | "age": "${session("age").as[String]}", | "hometown": "${session("hometown").as[String]}", | "homeland": "${session("homeland").as[String]}", | "languages": [ | ${session("languages").as[Seq[String]].map{ x => s"""{ "id": "${x}" }"""}.mkString(", ")} | ] |}""".stripMargin } def chooseRandomly(pool:IndexedSeq[Record[String]]) : Record[String] = { pool(random.nextInt(pool.length)) } def chooseRandomly(pool:IndexedSeq[Record[String]], maxLength:Int) : IndexedSeq[Record[String]] = { for (i <- 1 to random.nextInt(1, maxLength)) yield pool(random.nextInt(pool.length)) } } 
+5
source share
2 answers

For the first and last name, use simple feeders.

For a more complex data injection logic, do not use a feeder. Write exec (function) where you manually select the entries and install them into the session. You can use the Gatling csv parser to load data:

 val records: Seq[Map[String, Any]] = csv("country_capital_language.csv").records 

As you want a dynamic number of languages, you cannot use the EL Gatling template. You will have to manually create your request bodies, see the documentation .

+4
source

Gatling can currently handle all of these things on its own.

Note. I have not tested the simplified code examples. So forgive me for any typos.

  • JSON templates : https://groups.google.com/forum/#!topic/gatling/ggR6L4ukmqU . Keep in mind that column names in CSV files must match the names in the template and you need to use ElFileBody ( http://gatling.io/docs/2.2.1/http/http_request.html ).

    myTemplate.json

     { "name": "${firstName} ${lastName}", "age": "34", "languages": [ { "id": "119" }, { "id": "14" } ], "homeland": { "id": "121" }, "hometown": { "id": "155" } } 

    Expected content, for example. names.csv: http://gatling.io/docs/2.2.1/session/feeder.html

    firstName, lastName
    Miguel, Prado
    Dexter, Morgan
    Deborah, Morgan

  • Random CSV Feeder

     val scn = scenario("Insert100kInhabitants").repeat(10000){ feed(csv("names.csv").random) .exec(http("insert100kInhabitants") .post("/world/inhabitants") .body(ElFileBody("myTemplate.json").asJSON ) } 
+1
source

Source: https://habr.com/ru/post/1209835/


All Articles