Home > Software design >  jmeter beanshell pre processor
jmeter beanshell pre processor

Time:11-30

I have been using jmeter with the below logic:

  1. Get all items
  2. get all item_ids in a variable using json extractor
  3. Using beanshell pre-processor choose one item_id
  4. Get all sub items for selected item_id
  5. Get all sub_item_ids in a variable using json extractor
  6. Choose sub_item_id using beanshell pre-processor
  7. Edit sub item of sub_item_id

Kindly find the bean shell script below:

import java.util.Random;
var item_id_ALL_data=vars.get("item_id_ALL");
int item_id_max=Integer.parseInt(vars.get("item_id_matchNr"));
int item_id_min = 1;
int item_id_idx;
if(item_id_max>1)
{
  item_id_idx=  item_id_min   (int) (Math.random() * ((item_id_max - item_id_min)   1));
}
else{
  item_id_idx=0;
}
String item_idx=Arrays.asList(item_id_ALL_data.split(",")).get(item_id_idx);
log.info("item_idx:" item_idx);
vars.put("item_id_edit",item_idx);

I had ran jmeter based on the above logic with 5 threads and 50 iterations. But sometimes I am getting the below error:

2022-11-29 18:30:21,719 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import java.util.Random; var item_id_ALL_data=vars.get("item_id_ALL"); i . . . '' : Typed variable declaration : Method Invocation get

This error has been thrown randomly when executed. Also only when executed for multiple times, say multiple loops and threads with all possibilities. How can this be solved?

CodePudding user response:

Put your code into try block like:

try {

    // your code here
}

catch (Exception ex) {
    log.error("Failure in preprocessor", ex);
}

this way you will be able to see the real cause of the error in jmeter.log file.

Alternatively you can place debug(); directive in the beginning of your script.

Most probably there is something wrong with your variables, i.e. extractor fails and the variable is null or index is out of range or something like this.

Also be informed that starting from JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language for scripting mainly because Groovy has the best performance comparing to other scripting options. Groovy will give you human-readable error message without any extra steps required. You won't need to rewrite your code as well.

  • Related