ProgrammedChem
ProgrammedChem

Reputation: 157

JMeter BeanShell Property Setting Across Thread Group

I'm trying to calculate the time between two events in JMeter using BeanShell PostProcessors.

In the first block, I get the time and store it as a property. This is in one Thread Group. Then in another Thread Group, I have the second BeanShell block. I get an error which I cannot figure out. I have pasted the error here. Thank you very much for your hints and advice!

Here are the two pieces of BeanShell code:

FIRST POSTPROCESSOR:

//Set the current time to the time_upload variable
long time_upload = prev.getTime(); // get POST Time

props.put("time_upload",(String.valueof(time_upload))); 
log.info("Time for Upload is: " + time_upload); // print difference to jmeter.log file

SECOND POSTPROCESSOR:

String no_saved_carts = vars.get("no_saved_carts");
String no_saved_carts_trimmed = no_saved_carts.trim();

String temp_description = vars.get("description");
String temp_description_no_space = temp_description.trim();

String time_upload_local = props.get("time_upload");


if(temp_description_no_space.equals("</") || no_saved_carts_trimmed.equals("No Saved Carts Found")){
    vars.put("description","true");
} else{
    vars.put("description","false");
    //set the time to time_processing based on time_upload
    long time_processing_done = prev.getTime(); // get time 
    long time_upload_long = Long.parseLong(time_upload_local); // get HTTP Sampler 1 execution time from variable
    long delta = (time_processing_done - time_upload); // calculate difference
    log.info("Time difference is: " + delta + " ms"); // print difference to jmeter.log file
}

The relevant part of the ERROR LOG:

2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``//Set the current time to the time_upload variable long time_upload = prev.getTi . . . '' : Error in method invocation: Static method valueof( long ) not found in class'java.lang.String' 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread is done: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 INFO  - jmeter.threads.JMeterThread: Thread finished: Upload Saved Cart Thread Group 1-1 
2016/06/03 17:21:22 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:22 WARN  - jmeter.extractor.BeanShellPostProcessor: Problem in BeanShell script org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval   Sourced file: inline evaluation of: ``String temp_description = vars.get("description"); String no_saved_carts = vars. . . . '' : Typed variable declaration : Method Invocation Long.parseLong 
2016/06/03 17:21:26 INFO  - jmeter.threads.JMeterThread: Thread is done: Check Upload Status 2-1 

Upvotes: 1

Views: 1346

Answers (1)

Dmitri T
Dmitri T

Reputation: 168072

You cannot convert long to String using String.valueOf() method, there are following options:

  1. If you still want String just change the line to convert long to String to look like:

    props.put("time_upload", Objects.toString(time_upload,null)); 
    
  2. Get rid of long -> String and vice versa conversion, props is an usual java.util.Properties class instance so it stores Objects

    In first PostProcessor:

    long time_upload = prev.getTime();
    props.put("time_upload", time_upload);
    

    In second PostProcessor:

    long time_upload_long = props.get("time_upload"); // no need to cast from String
    
  3. You can use bsh.shared namespace to keep any Object - it will be accessible by all Thread Groups

    In first PostProcessor:

    bsh.shared.time_upload = prev.getTime();
    

    In second PostProcessor:

    long time_upload = bsh.shared.time_upload
    

You can get more informative error messages in jmeter.log file in case of Beanshell script error by surrounding your code with try/catch block like:

try {
    //your code here
}
catch (Throwable ex) {
    log.error("Something wrong", ex);
    throw ex;
}

See How to Use BeanShell: JMeter's Favorite Built-in Component for more JMeter and Beanshell tips and tricks.

Upvotes: 1

Related Questions