I have some performance tests in jMeter and in one HTTP request I have BeanShell PostProcessor. It should write user email address at the top of CSV file (newest on top). Its very important, that this file is sorted.
import org.apache.commons.io.FilenameUtils;
import org.apache.jmeter.services.FileServer;
import java.text.*;
import java.io.*;
import java.util.*;
try {
String email = vars.get("emailPrefix") + "+" + vars.get("environment") + "-jm-" + vars.get("randomEmailNumber")+"@someEmail.com";
log.info(email);
String separator = System.getProperty("file.separator");
File mFile = new File(FileServer.getFileServer().getBaseDir() + separator + "investors_" + vars.get("environment") + ".csv");
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(mFile), "UTF8"));
String result = "";
String line = "";
while((line = br.readLine()) != null){
result = result + "\n" + line;
}
result = email + "," + result;
FileOutputStream fos = new FileOutputStream(mFile);
fos.write(result.getBytes());
fos.flush();
} catch(Exception e) {
System.out.println("Exception in 2 1 4 /getContactInformation::Email writer:" + e.toString());
throw e;
}
But when I try to run i.e. 100 threads at once, sometime happens, that not all emails are logged, or CSV file ends empty (content is deleted in the middle of the run).
My problem can be solved by reading this CSV file from the end, but jMeter can not do this.
Is there any simply way, how to synchronize this postprocessor, or how to rewrite this?