4

I am trying to generate a random number using beanshell post processor but I am continuously getting an error

 "ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval   In file: inline evaluation of: `` try {....."

I am fetching the total count of matching records through Regular Expression extractor and passing the variable to the Post processor but it's not working.

Please see the screenshot.Regular Expresssion Extractor Beanshell Script

Naseem
  • 911
  • 5
  • 18
  • 48

1 Answers1

6
  1. Don't inline functions and/or variables in form of ${CountID} into Beanshell scripts as they may resolve into something which will cause compilation error or other form of unexpected behaviour. Replace this line:

    int count = ${__Random(1,counter,)};
    

    with this one

    int count = ThreadLocalRandom.current().nextInt(1, counter);
    
  2. log.info(count); line won't work as you cannot print an integer to jmeter.log file, you need to cast it to String first so change this line to

    log.info(String.valueOf(count));
    
  3. Consider using JSR223 Elements and Groovy language instead of Beanshell as Beanshell interpreter has worse performance than Groovy engine.


If your target is to get a random match you can do it without any scripting using only JMeter Functions like:

${__V(countID_${__Random(1,${countID_matchNr},)})}

See Here’s What to Do to Combine Multiple JMeter Variables guide for more details.

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • Hi...Thanks for the response. I tried with the below code but still it is giving an error "Error invoking bsh method: eval Sourced file: inline evaluation of: ``try { int counter = Integer.parseInt(vars.get("countID_matchNr")); int count = T . . . '' : Typed variable declaration : Attempt to resolve method: current() on undefined variable or class name: ThreadLocalRandom " try { int counter = Integer.parseInt(vars.get("countID_matchNr")); int count = ThreadLocalRandom.current().nextInt(1, counter); log.info(String.valueOf(count)); } – Naseem May 09 '17 at 09:52
  • My dear, have you added the import statement like `import java.util.concurrent.ThreadLocalRandom;`? – Dmitri T May 09 '17 at 09:55
  • Apologies. I am prettly much new to JMeter. After adding the above line, it started showing the value in the log but it is showing the log multiple times with different value like BeanShellTestElement: 6 BeanShellTestElement: 6 BeanShellTestElement: 2 BeanShellTestElement: 4 Anything I am missing ? – Naseem May 09 '17 at 10:03
  • If you run it with multiple threads or in multiple loops the function will provide a random number each time it will be called so it is expected – Dmitri T May 09 '17 at 10:10
  • I am running single thread with no loop, still getting these values. Noticed that as soon as I start the test, I get following error "ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``impor" but then it start showing the values. – Naseem May 09 '17 at 10:12