medy75
medy75

Reputation: 656

jMeter BeanShell postprocessor synchronization

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?

Upvotes: 1

Views: 688

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

Your test seems to be badly designed, concurrent writing into the same file is not something recommended, you should have ReadWriteLock implementation or use JMeter's Critical Section Controller to ensure that only one thread is writing something into the file at a time.

Another option is running your CSV file creation logic before you will attempt to read the lines.

The third (and IMHO) the best solution is getting rid of the file, write everything into JMeter Variable(s) or properties and read the values from memory when required. I would also recommend considering switching to JSR223 PostProcessor and Groovy language as for your scenario Groovy will provide much better performance than you can achieve using Beanshell, see Groovy Is the New Black article for details.

Upvotes: 1

Related Questions