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