Sumit Soni
Sumit Soni

Reputation: 39

Store extracted value in csv file using jsr223 postprocessor in jmeter

How can i store extracted value of a variable in a csv/text file using JSR223 post processor

Upvotes: 0

Views: 4928

Answers (3)

Dmitri T
Dmitri T

Reputation: 168157

If this is something you really need to do in the JSR223 PostProcessor the minimal code would be:

new File('/path/to/your/file.csv') << vars.get('YOUR_VARIABLE_NAME_HERE') << System.getProperty('line.separator')

However if there will be a minimal concurrency you will run into the race condition when 2 or more threads (virtual users) will be writing into the same file resulting in data corruption


The approach which I would recommend is using:

  1. Declare the variable you want to store via Sample Variables JMeter Property by adding the next line to user.properties file (lives in "bin" folder of your JMeter installation):

    sample_variables=YOUR_VARIABLE_NAME_HERE
    
  2. Once done you will be able to write the values using Flexible File Writer configured like:

    enter image description here

Upvotes: 2

luludenim
luludenim

Reputation: 61

1/ Consider for example a node in your test plan with your request :

A regular expression extractor and a JR223 post processor component as child of your request.

2/ If you extract for example a multiple variable named "blabla" positioning the match number to "-1"

3/ Here's the piece of Groovy code that you can use in your post processor component to write your variable in a file :

import org.apache.jmeter.*;

File outputFile = new File("MY_FILE.csv")

int max = Integer.parseInt(vars.get("blabla_matchNr"));

for (i=1;i<max;i++)  {

    def word = vars.get("blabla_"+i);
    outputFile << word  << "\r\n"

}

Upvotes: 0

samk
samk

Reputation: 440

You basically need to write the code to write into file.

Something like:

import org.apache.commons.io.FilenameUtils;

attr1 = vars.get("attr1");
attr2 = vars.get("attr2"); 

f = new FileOutputStream(locationOfCSVOutputfile, true);

p = new PrintStream(f);


p.println(attr2+","+attr2);

p.close();
f.close();

Like wise get the values you need and write into the file by comma separated.

Beware that in multiple threads scenario, Many threads will be accessing same file. therefore the file output may not be what you expected. To overcome this I used a critical section controller.

Hope this helps.

Upvotes: 1

Related Questions