Search code examples
jmeterbeanshell

Beanshell Script to generate sequence of strings


Can any one help in creating sequence of strings in multiple lines in a file using Beanshell Script

For example: Looking for output of

Test1,Test2,Test3
Test4,Test5,Test6
Test7,Test8,Test9

Tried this but giving incorrect result

int numSets = 1;    
for (int j = 1; j < numSets; j++) { 
    for (int i = 1; i <= numStrings; i++) {
    String str = "test" + i;
    if (i < numStrings) {
        result += str + ",";
    } else {
        result += str;
    }       
        out.write(str);

}
out.write("\n");
}
out.close();

Solution

  • Be informed that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting.

    Here is an example solution in Groovy:

    def lines = 3
    def itemsPerLine = 3
    def prefix = 'Test'
    def counter = 1
    
    1.upto(lines, { lineNumber ->
        1.upto(itemsPerLine, { itemNumber ->
            print(prefix + counter)
            if (itemNumber < itemsPerLine) {
                print(',')
            }
            counter++
        })
        print(System.getProperty('line.separator'))
    
    })
    

    More information: Beanshell vs. JSR223 vs. Java For JMeter: Complete Showdown

    If you want to print it to jmeter.log file or to another file you can use StringBuilder.append() instead of print()

    enter image description here