user2571340
user2571340

Reputation: 31

How to write Extracted Data in to A File In Gatling

I am using Gatling for load testing. When I am creating Customer profiles , Customer IDs will be generated. I am able to extract the customer ID and saving in to session variables.

But How to store these values in to a Text file.

please help me.

Upvotes: 2

Views: 9946

Answers (4)

Gianluca Veschi
Gianluca Veschi

Reputation: 1618

In the example below I'm saving the extracted NumberIDs out of the SOAP response and then save them into the numbersIDs.csv file so that it can be fed into another Request.

//Variables to store the extracted values of the response
var numberIDs  : Seq[String] = _ 

//Java Writer    
val numberIDs_writer = {
   val fos = new java.io.FileOutputStream("numbersIDs.csv")
   new java.io.PrintWriter(fos,true)
}
val scn = scenario("basicSimulation")    
     .exec(http("Request_One")  
            .post("/services")
            .body(StringBody(inputXMLpayLoad))  
                        .headers(Request_One_Header)
                        .check(bodyString.saveAs("Request_Response")) //Save the response in a variable called Request_Response
                        .check((status.is(200)))
                    //Extract all numberIDs of the SOAP response
                        .check(regex("""<NumberId>([\d]+)</NumberId>""")
                            .findAll
                            .transform { string => numberIDs = string; string } 
                            .saveAs("numberIDs")))  
//Save variable numberIDs into the csv file
.foreach("${numberIDs}", "id") {
  exec(session => {
    numberIDs_writer.println(session("id").as[String])
    session
  })
}

Upvotes: 3

voho
voho

Reputation: 2905

Another possibility (not tested, but could work) is to use response transformer (call .transformResponse after .post). In the transformer body you would get a response object from which you can extract the generated ID and append it into a file, collection, etc.. Then just return the original response as the transformation result. This is however not a very nice solution from the design point of view, because your transformation has a side effect.

Upvotes: 1

avanderw
avanderw

Reputation: 675

Since 2.0.0-M1, infoExtractor hook takes a Session parameter: https://github.com/excilys/gatling/issues/1004

There's also a builtin that adds the Session content to simulation.log records when the request fails. https://github.com/excilys/gatling/commit/b7b6299c658d0aa1b88971208eb0c98a9808e37f

You can adapt this example if you just want to log with logback.

class MySimulation extends Simulation with Logging {

def logSessionOnFailure(status: RequestStatus, session: Session, request: Request, response: ExtendedResponse): List[String] = {
    if (status == KO) logger.error(session)
    Nil
}

val httpConf = http
...
.extraInfoExtractor(logSessionOnFailure)

...
}

Upvotes: 1

Stephane Landelle
Stephane Landelle

Reputation: 7038

There's several ways to achieve that.

If you're familiar with Scala, you can:

If this is too complex for you, the most simple way is to use logback:

  • declare a logger in your simulation with a special name
  • configure logback so this logger outputs in a dedicated FileAppender
  • use a pattern that just logs the message

Regarding the second solution, check the logback documentation: http://logback.qos.ch/documentation.html

Upvotes: 4

Related Questions