How to get the name of JMX Jmeter filename in a variable - variables

I want to use the name of the jmeter test script (.jmx) in a listener so as to generate the result file in a dynamic way. Can you please tell me what is the Jmeter variable for that purpose?
Used ${fileName} which didn't work

You can do it via Beanshell scripting like:
GUI mode
import org.apache.jmeter.gui.GuiPackage;
String scriptName = GuiPackage.getInstance().getTestPlanFile();
vars.put("scriptName", scriptName);
non-GUI mode
import org.apache.jmeter.services.FileServer;
String scriptName = FileServer.getFileServer().getScriptName();
vars.put("scriptName", scriptName);
Put the code snippet of your choice into any "Beanshell" test element (sampler, pre/post processor, or assertion), it will get .jmx test script name and store it into ${scriptName} variable.
To learn more about Beanshell scripting in JMeter check out How to use BeanShell: JMeter's favorite built-in component guide.

The variable that holds the test plan name is ${__TestPlanName}
Ref: http://jmeter.apache.org/usermanual/functions.html#__TestPlanName

Below would work irrespective of GUI / Non GUI mode:
import org.apache.jmeter.services.FileServer;
import java.io.File;
String testPlanFile = FileServer.getFileServer().getBaseDir() +
File.separator +
FileServer.getFileServer().getScriptName();
props.put("testPlanFile", testPlanFile);
Use this as ${__P(testPlanFile)} - Adding it as var would not work across all threads. From http://jmeter.apache.org/usermanual/functions.html -
Properties are not the same as variables. Variables are local to a
thread; properties are common to all threads, and need to be
referenced using the __P or __property function.

Related

Jmeter - can't pass property to JDBC connector

I have a setUp thread Group and a normal thread Group. Jmeter runs the thread groups consecutively.
Into the setup group I select a username from a database and I put that into a property with a JSR223 sampler like this:
import org.apache.jmeter.util.JMeterUtils;
JMeterUtils.setProperty("schemaProp", vars.get("schemaVar_1"));
Into the second thread I have a JDBC Connector which access the property like this:
${__property(sshUserProp,)}
But It doesn't work.
LOG:
2021-12-16 08:54:58,802 DEBUG o.a.j.p.j.c.DataSourceElement: Driver: org.postgresql.Driver DbUrl: jdbc:postgresql://127.0.0.1:50539/JB7 User: sshUserProp
The JDBC Connector doesn't really see the property, although it is actually set, cause I can access it with another JSR223 sampler:
import org.apache.jmeter.util.JMeterUtils;
log.info ("------------------------- MESSAGE: " + JMeterUtils.getProperty("sshUserProp"))
LOG:
2021-12-16 08:54:58,961 INFO o.a.j.p.j.s.J.JSR223 Sampler: ------------------------- MESSAGE: 160116
I'm afraid it's not possible, at least not with JMeter 5.4.1, looking into JDBC Connection Configuration source code the element is being set up in the testStarted() function and according to the JavaDoc
Called just before the start of the test from the main engine thread. This is before the test elements are cloned. Note that not all the test variables will have been set up at this point.
So you need to determine the user name beforehand somewhere somehow, i.e. by running another JMeter test and saving the username(s) into a file via Flexible File Writer and then readine the username from the file using __StringFromFile() function or pass the username via -J command-line argument - this way your current approach with __property() function will work.

Passing Environment variable to Mainframe JVM

I am running a JAVA Mainframe JAR. It is working successfully using the IBM JVMLDM fro z/OS. My next step was to integrate an in-house logging framework. I am required to pass several environment variables(user system properties) into the JVM using a STDENV DD statement in the JCL. I am using the environment variables provided by IBM (documentation) within a PARM file as follows:
IJO="$IJO -DCAR_PROP=6123548595"
EXPORT IBM_JAVA_OPTIONS="$IJO "
I am also using the following environment variable in a similar manner:
IJO="$IJO -DCAR_PROP=6123548595"
EXPORT JZOS_JVM_OPTIONS="$IJO "
Neither of these options are working as the property still fails to be captured by the JAR. Looking for some insight as to what might be the issue with the statement with which I may not be familiar.
DOCUMENTATION
https://www.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.iean500/bcdbatch.htm
Does using lower-case export work? i.e.
IJO="$IJO -DCAR_PROP=6123548595"
export IBM_JAVA_OPTIONS="$IJO "

How to set variables to custom function in Jmeter?

I have a basic function in BeanShellSampler.bshrc at Jmeter 4.0
String getMyString(String strParam) {
return "MyString: "+strParam;
}
I called in BeanShell Sampler as below
String N = "123123";
log.info("${__BeanShell(getMyString("${__V(Var${N})}"),)}");
When I run Sampler output is somthing like that.
2018-06-18 15:25:40,080 INFO o.a.j.u.BeanShellTestElement: MyString: Var${N}
How can I set string variable to my function?
I read function articles in Jmeter web site
Thank you.
Add the next line to user.properties file:
beanshell.sampler.init=BeanShellSampler.bshrc
Amend your code to look like:
String N = "123123";
log.info(getMyString(N));
That's it, you should get MyString: 123123 in jmeter.log file
Be aware that starting from Jmeter 3.1 it is recommended to use Groovy for all forms of scripting as Groovy performance is much better comparing to Beanshell so consider taking the following steps instead:
Create a file, i.e. foo.groovy in "bin" folder of your JMeter installation and put your function there:
String getMyString(String strParam) {
return "MyString: " + strParam;
}
Add the next line to user.properties file:
You should be able to refer your custom code from __groovy() function like:
${__groovy(log.info(getMyString("123123")),)}
functions can be used anywhere in the Test Plan
For each BeanShell Program type there are different beanshell.*.init properties defined in bin/user.properties
beanshell.function.init=BeanShellFunction.bshrc
beanshell.preprocessor.init=BeanShellSampler.bshrc
beanshell.postprocessor.init=BeanShellSampler.bshrc
beanshell.assertion.init=BeanShellFunction.bshrc
Hence the same function which needs to be called from any program(preprocessor, postprocessor, etc) we need to copy the function to every .bshrc file OR use same .bshrc file for every program init property.
In your case if you are using local string variable N and passing it along with the script. If you use ${Variable} there must be a JMeter variable defined so that JMeter can pick its value. To do that you can use vars.put , write N value to JMeter variables and use ${N} .
I have defined Var123123 value as FinalValue as shown below
And 2 beanshell samplers one is to put String variable N to Jmeter variable and one is beanshell script as shown below
You can see in the log its printed VAR123123's value which is FinalValue
The reason why i took 2 beashell samplers is if i write N to JMeter variables and use it in same script its not updating N value until the sampler executed..
References :
Configuring JMeter
JMeter Beanshell
Please let me know if it helps

Jmeter Beanshell: Accessing global lists of data

I'm using Jmeter to design a test that requires data to be randomly read from text files. To save memory, I have set up a "setUp Thread Group" with a BeanShell PreProcessor with the following:
//Imports
import org.apache.commons.io.FileUtils;
//Read data files
List items = FileUtils.readLines(new File(vars.get("DataFolder") + "/items.txt"));
//Store for future use
props.put("items", items);
I then attempt to read this in my other thread groups and am trying to access a random line in my text files with something like this:
(props.get("items")).get(new Random().nextInt((props.get("items")).size()))
However, this throws a "Typed variable declaration" error and I think it's because the get() method returns an object and I'm trying to invoke size() on it, since it's really a List. I'm not sure what to do here. My ultimate goal is to define some lists of data once to be used globally in my test so my tests don't have to store this data themselves.
Does anyone have any thoughts as to what might be wrong?
EDIT
I've also tried defining the variables in the setUp thread group as follows:
bsh.shared.items = items;
And then using them as this:
(bsh.shared.items).get(new Random().nextInt((bsh.shared.items).size()))
But that fails with the error "Method size() not found in class'bsh.Primitive'".
You were very close, just add casting to List so the interpreter will know what's the expected object:
log.info(((List)props.get("items")).get(new Random().nextInt((props.get("items")).size())));
Be aware that since JMeter 3.1 it is recommended to use Groovy for any form of scripting as:
Groovy performance is much better
Groovy supports more modern Java features while with Beanshell you're stuck at Java 5 level
Groovy has a plenty of JDK enhancements, i.e. File.readLines() function
So kindly find Groovy solution below:
In the first Thread Group:
props.put('items', new File(vars.get('DataFolder') + '/items.txt').readLines()
In the second Thread Group:
def items = props.get('items')
def randomLine = items.get(new Random().nextInt(items.size))

WSADMIN jacl Scripting - how get property value

in a WSADMIN jacl Script how i can get wsadmin.properties values ?
for example the "com.ibm.ws.scripting.traceFile" ?
i already try with
puts $com.ibm.ws.scripting.traceFile
buts return
can't read "com.ibm.ws.scripting.traceFile": no such variable
while executing
"puts $com.ibm.ws.scripting.traceFile"
The values stored in wsadmin.properties are loaded into the JVM, and are stored as System Properties. You can obtain the values of these properties by working with Java's java.lang.System object, and then retrieving the specific property you want:
Here's the JACL code:
package require java
set sysprops [java::call System getProperties]
set traceFile [[$sysprops get com.ibm.ws.scripting.traceFile] toString]
puts "trace file: $traceFile"
For anyone interested, here's the Jython equivalent:
from java.lang import System as javasystem
sysprops = javasystem.getProperties()
traceFile = sysprops.get('com.ibm.ws.scripting.traceFile')
print "traceFile: " + traceFile