I had a scenario where I need to write correlation values in a CSV file. And the easiest way I had come up with is the below code in answer section.
More suggestions are appreciated.
There is a plugin called Flexible file writer You can use that it is efficient and easy to implement explained here.
Be aware that starting from JMeter 3.1 it's recommended to use JSR223 Test Elements and Groovy language for any form of scripting so consider migrating to the JSR223 PostProcessor and the following code:
new File('FILEPATH/filename.csv') << vars.get('PARAM_1') << ',' << vars.get('PARAM_2') << System.getProperty('line.separator')
However this approach will work only if no concurrency assumed, if the PostProcessor will be executed by 2 or more concurrent threads you may run into a race condition when multiple threads will be writing into the same file resulting in data corruption.
So I would recommend declaring your PARAM_1 and PARAM_2 as Sample Variables and storing them into a file using i.e. Flexible File Writer
Add this below code in Bean Shell Post Processor
a = vars.get("PARAM_1"); // PARAM_1 is parameter/correlation variable
b = vars.get("PARAM_2"); // PARAM_2 is parameter/correlation variable
f = new FileOutputStream("FILEPATH/filename.csv", true);
p = new PrintStream(f);
this.interpreter.setOut(p);
print(a +","+ b);
f.close();
Dont use Beanshell sampler. Use JSR sampler and get the variables as usual like vars.get("variableName");
In Bean shell sampler it wont work.