0
votes

I need to dynamically generate an XML or JSON in an iteration where the XML or JSON has a variable number of elements -- e.g., it could be books. The values of the books come from the CSV file.

I created a CSV Data Config that point to the CSV file with a variable called csvBook.

Next, in a BeanShell Sampler, I call

StringBuffer sb = new StringBuffer("<Order><Books>");
Random rv = new Random();
int size = rv.nextInt(100);
for (int i = 0; i < size; i++) {
   sb.append("<Book Name=" + vars.get("csvBook") + "/>");
}
sb.append("</Books></Order>");

The problem is I don't know how to get new values from the CSV file as I run through the loop inside one iteration. The vars.get("csvBook") returns the same value in the same iteration.

Is there a command to tell JMeter to get the next CSV value (next row) (again, inside one iteration)?

Thanks in advance.

1
if you have script anyways, IMO it's easier to read CSV inside it, like this: anadea.info/blog/jmeter-reading-csv-with-beanshell and only store "last read line" or something like that as variable, so next time you read the next portionKiril S.
@KirilS -- thanks. That worked. We read the CSV file into an ArrayList. Now, we would like to share that ArrayList across all the threads in a Thread Group because we have a lot of threads (virtual users) and the file is large -- e.g., 400K rows. But yes, that approach works.Bernie Wong

1 Answers

0
votes

Consider switching to JSR223 Sampler and Groovy language as:

  1. Groovy has built-in support for JSON
  2. Groovy has built-in support for XML
  3. Groovy performance is much better than Beanshell

The relevant Groovy code would be something like:

import groovy.xml.StreamingMarkupBuilder

def csvFile = new File('/path/to/csv/file')

def payload = {
    order {
        books {
            csvFile.readLines().each {line ->
                book (name:line)
            }
        }
    }
}

def xmlFile = new File('/path/to/xml/file')
def writer = xmlFile.newWriter()
def builder = new StreamingMarkupBuilder()
def writable = builder.bind payload
writable.writeTo(writer)