Question

To log the total time of a test scenario I use Groovy scripts in JSR223 PostProcessors to capture the timestamp before and after the script.

Script to capture the scenario start timestamp:

def scenario_start = System.currentTimeMillis().toString();
vars.put("scenario_start", scenario_start);

When I want to append the results for one Customer I want to append the results to an output file using a second groovy script:

def scenario_end = System.currentTimeMillis().toString();
def scenario_start = vars.get("scenario_start");
def cm = vars.get("CUSTOMER_NUMBER");

log.debug("scenario_start = " + scenario_start);
log.debug("scenario_end = " + scenario_end);
log.debug("CUSTOMER_NUMBER = " + cm);

File file = new File("logs/output/scenario_times_idtv.txt").newOutputStream().withWriter { out ->
    out.append(cm + ";" + scenario_start + ";" + scenario_end);
}

The actual results is the first line in my log: scenario_times_idtv.txt is overwritten each time.

I've used out.append, out.println, new File("...").withWriter {...}, new File("...").newOutputStream().withWriter {...} but I get ONE line of data each time in my log.

I can't find how to properly APPEND new results to the log file.

Thanks for your experienced input!

Was it helpful?

Solution

I'm not very Groovy expert, but recall answering something similar at JMeter soap response - data analysis

using Beanshell.

I guess that if you change your code line

File file = new File("logs/output/scenario_times_idtv.txt").newOutputStream().withWriter { out ->
out.append(cm + ";" + scenario_start + ";" + scenario_end);

to something like:

FileOutputStream file = new FileOutputStream("logs/output/scenario_times_idtv.txt", true);
file.append((cm + ";" + scenario_start + ";" + scenario_end).getBytes());
file.flush();
file.close();

your script will behave as expected. Mind the true parameter of FileOutputStream. It tells the stream to append existing file. Perhaps there is a Groovy analogue, but this bit should work as well.

OTHER TIPS

What worked best for me was to set up the output file in a setUp thread group as a property (so it will be global across all thread groups), then use it within my main thread group, and close it out in a tearDown thread group.

setUp:

def fileName = vars.get("CSVFileName");

def currDate = new Date().format("_yyy-MM-dd_HH.mm.ss")
fileName = fileName.replace(".csv",  currDate + ".csv");
FileOutputStream fileLOG = new FileOutputStream(fileName, true);
fileps = new PrintStream( fileLOG );
fileps.println("user,otherCSV headers...");

props.put("MYLOGPRINTSTREAM", fileps);  // Properties are global to JMeter, so can be used to communicate between threads and thread groups

then in the main thread group:

def fileps = props.get("MYLOGPRINTSTREAM");
fileps.println(vars.get("user") + "," + other CSV fields ...);

and in the tearDown:

def fileps = props.get("MYLOGPRINTSTREAM");
fileps.close();
log.info("***TEARDOWN DONE*** "); 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top