JMeter - get error when retrieving the Thread Group name - jmeter

I tried the following statements in the BeanShell PreProcessor
String groupName = ctx.getThreadGroup().getName();
groupName = "${__BeanShell(ctx.getThreadGroup().getName())}"
Both return the ERROR
jmeter.util.BeanShellInterpreter:
Error invoking bsh method: eval Sourced file:
inline evaluation of: ctx.getThreadGroup().getName();
However, they print the Thread Group name correctly.

Your code looks good (at least this line), the problem seems to be somewhere else
Going forward you can use the following options to get to the bottom of your Beanshell script issue:
Put your code inside the try block like:
try {
String groupName = ctx.getThreadGroup().getName();
} catch (Throwable ex) {
log.error("Something went wrong", ex);
throw ex;
}
This way you will be able to see full error details in jmeter.log file
Add debug() command to the beginning of the Beanshell script - it will trigger debugging output to JMeter console window
See How to Debug your Apache JMeter Script article for more information on JMeter test script issues troubleshooting.

Related

jmeter: Error invoking bsh method: in Beanshell Postprocessor

I have a BeanShell postprocessor which has the below code:
props.put("accDom",vars.get("DOMAIN_ID_1"));
Although the code works, and the value does get written to the defined property correctly, the log file is filled with such errors:
2020-11-24 14:41:19,655 ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``props.put("accDom",vars.get("DOMAIN_ID_1"));'' : Method Invocation props.put
2020-11-24 14:41:19,655 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: ``props.put("accDom",vars.get("DOMAIN_ID_1"));'' : Method Invocation props.put
Any ideas?
I changed the BeanShell PostProcessor to a JSR223 Sampler, and edited the syntax to:
String accDom = vars.get("DOMAIN_ID_1");
props.put("accDom",accDom);
// Hide sampler
SampleResult.setIgnore();
This stopped all errors in the logs
The log entry doesn't tell the full story, my expectation is that your DOMAIN_ID_1 variable is null at some point, you can try surrounding your code into try block like:
try {
props.put("accDom", vars.get("DOMAIN_ID_1"));
}
catch (Exception ex) {
log.error("Failure", ex);
}
this way you will get more comprehensive stacktrace in the jmeter.log file
Be aware that since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting so:
Change your Beanshell PostProcessor to JSR223 PostProcessor
Amend your code to use Elvis operator to provide the default value if DOMAIN_ID_1 variable is not set:
props.put("accDom", vars.get("DOMAIN_ID_1") ?: "some default value");
More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It
And last but not the least, it might be the case your PostProcessor placement causes its extra execution, see JMeter Scoping Rules chapter for detailed explanation, you should put the PostProcessor as a child of the Sampler which defines this DOMAIN_ID_1 variable, if you place it at the same level with all the Samplers - it will be executed after each sampler causing errors as the variable is not defined.

Getting an error on writing a DB output value to a variable in jmeter

The syntax I used is props.put("perpetual_id",vars.get("Inventory_id_1"));
Response code: 500
Response message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``props.put("perpetual_id",vars.get("Inventory_id_1")); System.out.println(vars.ge . . . '' : Method Invocation props.put
If I am using System.out.println(vars.get("inventoryId_1"));, it's printing fine.
You probably have a typo, i.e. missing underscore in your props.put... statement.
Given System.out.println(vars.get("inventoryId_1")); works fine my expectation is that you need to change your line to something like:
props.put("perpetual_id",vars.get("inventoryId_1"));
Few more hints:
you can use bsh.shared namespaces as an alternative to JMeter Properties
you can get more human-friendly error message by putting your Beanshell code inside the try block like:
try {
//your code here
}
catch (Throwable ex) {
log.error("Error in beanshell", ex);
throw ex;
}
This way you can get the "usual" stacktrace printed to jmeter.log file which will be way more informative and will help you to get to the bottom of the issue much faster and easier.
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Beanshell in JMeter tests.

Jmeter Beanshell error

i am getting following error
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``import java.text.*; import java.io.*; import java.util.*; import org.apache.jmet . . . '' Encountered "/" at line 15, column 74.
could anyone tell what is causing the problem.Thanks.
You can use the following approaches to debug your Beanshell script:
Add debug(); line at the beginning of your script and inspect STDOUT so you will be able to see what exactly goes on
Add extra logging like log.info("something"); so you will be able to determine which lines are fine and where execution stops by looking into jmeter.log file
Wrap your code into try/catch block as follows:
try {
//your Beanshell code here
} catch (Exception ex) {
log.info("Script execution failed", ex);
}
Exception details will be printed to jmeter.log file, it's much more informative than Error invoking bsh method one.
See How to use BeanShell: JMeter's favorite built-in component guide for more tips and tricks.
can you post the full beanshell script please?
its likely you have a small syntax error in there. According to the error message, it will be on line 15.

run sh script in jmeter

For load testing I want to randomize my testvalues before I run the test in jmeter. To do so, I want to use this bash script:
#! /bin/bash
cat data.dsv | shuf > randomdata.dsv
This should be executed in jmeter. I tried using a BeanShell Sampler with this command (I am using this command to always find the correct paht to the file no matter on which machine I want to execute it):
execute(${__BeanShell(import org.apache.jmeter.services.FileServer; FileServer.getFileServer().getBaseDir();)}${__BeanShell(File.separator,)}random.sh)
but I always get this error message:
ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval In file: inline evaluation of: ``execute(/home/user/git/path/'' Encountered "( /" at line 1, column 8.
Any Ideas what to do or is there some best practice I just di not found yet?
I would suggest going for OS Process Sampler instead, it should be easier to use, something like:
In regards to Beanshell approach, there is no need to us __Beanshell function in the Beanshell sampler, besides an instance of Beanshell interpreter is created each time you call the function causing performance overhead. You can just put the code into sampler's "Script" area as
import org.apache.jmeter.services.FileServer;
StringBuilder command = new StringBuilder();
FileServer fileServer = FileServer.getFileServer();
command.append(fileServer.getBaseDir());
command.append(System.getProperty("file.separator"));
command.append("random.sh");
Process process = Runtime.getRuntime().exec(command.toString());
int returnValue = process.waitFor();
return String.valueOf(returnValue);
See How to use BeanShell: JMeter's favorite built-in component guide for information on Beanshell scripting in JMeter.

JMeter Beanshell script for sharing cookie between threads throws an error

I am trying to execute the following Beanshell script in JMeter and it throws an error in the log. The script is:
import org.apache.jmeter.protocol.http.control.CookieManager;
import org.apache.jmeter.protocol.http.control.Cookie;
CookieManager manager = sampler.getCookieManager();
Cookie cookie = new Cookie("ApiSession",props.get("MyCookie"),"","/",false,0);
manager.add(cookie);
The error in the log file is:
jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.jmeter.protocol.http.control.CookieManager; import org.apache. . . . ''
It is not happy with the line: manager.add(cookie);
If I comment it out, then the script runs, but obviously doesn't do what I want. So, not sure what the problem is.
It is not helpful that I can't see whole of the debug information. Jmeter log records only part of the actual error message (as above) and that message is cut in the middle. Switching on debugging mode doesn't help.
If you want to see full error message you'll need to surround problematic statement in try/catch block and print stacktrace to sdtout / log.
Particularly sharing cookies between thread groups use case is highlighted in How to use BeanShell guide.

Resources