jmeter - Beanshell post processor

Viewed 1409

Getting below error in log:

2020-06-24 13:23:51,091 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,091 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,154 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,154 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,236 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: //to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler 2020-06-24 13:23:51,236 WARN o.a.j.e.BeanShellPostProcessor: Problem in BeanShell script: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of://to output the execution flow in the jmeter.log to help demonstrate how the scr . . . '' : Typed variable declaration : Attempt to resolve method: getName() on undefined variable or class name: sampler

Below is the code used in Beanshell postprocessor.

import java.text.DecimalFormat;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

int tdNumber = ctx.getThreadNum();
int noThreads = ctx.getThreadGroup().getNumThreads();
long tdstart = ctx.getThread().getStartTime();
long tdEnd = ctx.getThread().getEndTime();
String respCode = ctx.getPreviousResult().getResponseCode();
String respCode = ctx.getPreviousResult().getResponseMessage();
String samplerName = sampler.getName();


Date date = new Date();
date.setDate(date.getDate());
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss");
String formattedDate = df.format(date);

log.info("sampler::"+ sampleName +"....ThreadNo::"+ (tdNumber + 1) +" ....Starttime::"+ formattedDate +
"....StatusCode::" + respCode +"....TotalThreads::"+ noThreads +" ....Iterationno::"+vars.getIteration());
2 Answers

undefined variable or class name: sampler

There is no sampler shorthand in the Beanshell PostProcessor, you should use one of the following alternatives:

  • prev.getSampleLabel(false)
  • ctx.getCurrentSampler().getName()

Also be aware that starting from JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so consider migrating to Groovy on next available opportunity.

More information on this ctx, prev and friends: Top 8 JMeter Java Classes You Should Be Using with Groovy

Move to JSR223_PostProcessor which is the best practice and include also sampler

Migration to JSR223 PostProcessor+Groovy is highly recommended for performance, support of new Java features and limited maintenance of the BeanShell library.

Related