jdbcgroovyjmeterjsr223

JDBC Response save in csv file using JSR223 Postprocessor in the script location


I am trying to store my JDBC response in a CSV file with a particular path / Location on my local system. Please help me with the code below to pass my result path.

resultSet = vars.getObject("resultSet")
result = new StringBuilder()

def randomRow = resultSet.get(org.apache.commons.lang3.RandomUtils.nextInt(0,resultSet.size()))

randomRow.each { k, v -> 
    result.append("${k}").append(",")
}
result.append(System.getProperty("line.separator"))

for (Object row: resultSet ) {
    iter = row.entrySet().iterator()
    while (iter.hasNext()) {
        pair = iter.next()
        result.append(pair.getValue())
        result.append(",")
    }
    result.append(System.getProperty("line.separator"))
}

org.apache.commons.io.FileUtils.writeStringToFile(new File("foo.csv"), result.toString(), "UTF-8")

My script is working but it stores the values in my JMeter bin folder instead want to store a particular folder.


Solution

  • This constructor: new File("foo.csv") assumes that JMeter will save the output in the foo.csv file in its working directory.

    If you want a "particular folder" you can specify the full path instead of the relative path, i.e.

    new File("c:/onefolder/anotherfolder/foo.csv")

    Also be informed that if you will run your script with 2 or more threads (virtual users) in Thread Group - they will start concurrently writing into the same file which will result in data corruption or loss (race condition) so it's better to declare the variables from the JDBC Request as Sample Variables and go for Flexible File Writer instead of the JSR223 test element