0

I am executing a test plan where I have moved the files from source to destination one by one by using for each loop in JSR223 Sampler of JMeter. I am unable to print each fIle's status that has been moved from source to destination in the HMTL dashboard report. Currently I have used the Label property of JSR223 Sampler like this - SampleResult.setSampleLabel("Source: " + '${source}' + "::" + "Destination: " + '${destination}' + '${data}');

here ${source} represents the source path of the folder, ${destination} represents the destination folder path, and ${data} is just appended at the end of the ${destination} folder path.

My groovy script is:

import org.apache.jmeter.services.FileServer
log.info("############################################################# START #############################################################");
def baseDestinationFolder = new String('${destination}');

log.info(baseDestinationFolder);   // /home/solitera/CDRs/Input/

def dataDestFolder = baseDestinationFolder + '${data}';
def SMSDestFolder = baseDestinationFolder + '${sms}';
def VoiceDestFolder = baseDestinationFolder + '${voice}';

log.info("dataDestFolder : " + dataDestFolder);       // /home/test/CDRs/Input/DATA/
log.info("SMSDestFolder : "+SMSDestFolder);          // /home/test/CDRs/Input/SMS/
log.info("VoiceDestFolder : " + VoiceDestFolder);   // /home/test/CDRs/Input/VOICE/

File sourceFolder = new File('${source}');
File[] datafiles = sourceFolder.listFiles();     


for (File inputFile : datafiles){
        def fileName = "" + inputFile.getName();
        log.info("fileName : " + fileName);
        def finalPath;
        if(fileName.contains("data")){
            SampleResult.setSampleLabel("Source: " + '${source}' + "::" + "Destination: " +  '${destination}' + '${data}');
            log.info( "The Sample Label is : " + SampleResult.getSampleLabel() );
            finalPath = dataDestFolder + fileName;
        }else if(fileName.contains("voice")){
            SampleResult.setSampleLabel("Source: " + '${source}' + "::" + "Destination: " +  '${destination}' + '${voice}');
            log.info( "The Sample Label is : " + SampleResult.getSampleLabel() );
            finalPath = VoiceDestFolder + fileName;
        }else if(fileName.contains("sms")){
            SampleResult.setSampleLabel("Source: " + '${source}' + "::" + "Destination: " +  '${destination}' + '${sms}');
            log.info( "The Sample Label is : " + SampleResult.getSampleLabel() );
            finalPath = SMSDestFolder + fileName;
        }

        log.info("finalPath is : " + finalPath);
        def newFile = new File(finalPath);
        newFile << inputFile.text
    }


log.info("############################################################# END #############################################################");

My HTML Dashboard Report is: enter image description here

Currently in my source folder there are three CSV files that I have moved to the destination folder but it prints only the name of one file path and gives me the response time of all three files. Is anyone please help me how can I get the individual response time of each file?

1 Answer 1

1

If you read JSR223 Sampler documentation more attentively you will probably see the following stanza:

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

so don't ever inline JMeter Functions and/or Variables into JSR223 scripts, either use "Parameters" section or go for code-based equivalents instead

So if you change this line:

def dataDestFolder = baseDestinationFolder + '${data}';

to this one:

def dataDestFolder = baseDestinationFolder + vars.get('data')

and similarly refactor all other JMeter Variables occurrences - your script should start working as expected.

vars is the shorthand for JMeterVariables class instance, check out JavaDoc for comprehensive information on all functions, you might also be interested in Top 8 JMeter Java Classes You Should Be Using with Groovy article

3
  • Hi Dmitri, I have applied as per the documentation details later and changed the inline variables to vars.get(), but I got only the result as I shared a picture above of statistics. I want to print the each file response time in html dashboard report that moved from source to destination folder. How can I achieved that using a single JSR223 SAMPLER with having NO. OF THREAD - 1 ? Currently in this sampler 5 files have been moved one by one using groovy script logic.
    – Rajan
    Commented May 22, 2020 at 5:15
  • Use SampleResult.addSubResult() function for each file
    – Dmitri T
    Commented May 22, 2020 at 6:56
  • I have used this SampleResult.addSubResult() but got the error saying ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script jsr223 sampler, message: javax.script.ScriptException: javax.script.ScriptException: org.codehaus.groovy.runtime.metaclass.MethodSelectionException: Could not find which method addSubResult() to invoke from this list: public void org.apache.jmeter.samplers.SampleResult#addSubResult(org.apache.jmeter.samplers.SampleResult)
    – Rajan
    Commented May 22, 2020 at 7:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.