Skip to content

Instantly share code, notes, and snippets.

@infomaven
Last active January 29, 2023 17:57
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save infomaven/1d9393c83d57eef7ae72e2b2818be718 to your computer and use it in GitHub Desktop.
Create a Gatling feeder file using REST calls
/*
Basic blocks of code that will build a CSV file using repsonse data from API calls
This is a "one-off" process, meaning you will
1. run Gatling script to generate CSV file using REST calls (usually POSTS, but GETS or datbase queries could work )
2. copy the file into your project
3. compile and run Gatling
*/
class MySimulation extends Simulation {
// output file
val nameOfMyFile = "coolFeeder.csv"
val filePath = "fully/qualified/path/to/file/"
/* file writer object */
val myFileWriter = {
val output = new FileOutputStream( filePath)
val pw = new java.io.PrintWriter( output, true)
//print header row for the file (just once)
pw.println("My,File,Column,Headers,Go,Here")
pw
}
/* function that extracts values from Gatling session (a huge Map) and forms them into a csv row (all Strings of course)
// format: rowvalue1, rowvalue2, rowvalue3, etc */
dev buildRow( session:Session): String = {
val filtered = new StringBuilder
filtered ++= session.get("Attribute1").as[String].concat(",")
filtered ++= session.get("Attribute2").as[String].concat(",")
// ...add as many of these as you need to create a complete row
val result = filtered.toString()
result
}
/* Gatling scenario that produces data for your file
The key thing that makes it work is being able to access and manipulate Gatling Session attributes.
*/
val scnPost = scenario("My-Uniquely-Named-Scenario")
.exec( session => {
session.set("Attribute1", valueForAttribute1.toString()).set("Attribute2", valueForAttribute2.toString())
// ...repeat this pattern until all your attributes are saved from the request
// There should be at least one attribute for each column in your final file
// build your request payload, headers, and exec the scenario ( code not included)
.exec( yourcodeThatGetsOrCreatesData )
// call "exec" a SECOND time to transfer data from the session object to buildRow()
.exec( session => {
val dataRow = buildRow( session )
println("***Debug: row sent to file = " + dataRow)
val writer = myFileWriter
writer.println( dataRow )
session
})
before {
println("*** creating feeder file ***")
}
setUp(
scenario.inject( loadprofile)).protocols( standardHttp)
)
after {
println("Done")
}
}
@xfornesa
Copy link

It was very much useful :)
Thank you.

@infomaven
Copy link
Author

You are very welcome!

@fico11
Copy link

fico11 commented Jun 28, 2021

Yeap, very useful, cheers.

@sensesnet
Copy link

sensesnet commented Jan 9, 2022

Thank you!
!But resource leaks)
you must close, at the end of simulation:

 val output = new FileOutputStream( filePath)
 val pw = new java.io.PrintWriter( output, true)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment