run sh script in jmeter - bash

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.

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/using output of CMD window of Jmeter

I,m running a Java file from BeanShell Sampler in jmeter, I'm getting the output successful in cmd windows of jmeter. The output comprises of series of logger files,I need to extract only a specified string from the cmd window and use it for another sample
Given you run your program using i.e. ProcessBuilder you should be able to access its output via Process.getInputStream() method
Process process = new ProcessBuilder('c:\\apps\\jmeter\\bin\\jmeter.bat', '-v').start()
String output = org.apache.commons.io.IOUtils.toString(process.getInputStream(),'UTF-8')
log.info('My program output is:')
log.info(output)
Also I would recommend considering switching to JSR223 Sampler and Groovy language as this way it will be much faster and easier:
def output = "jmeter.bat -v".execute().text
log.info('My program output is:')
log.info(output)
Demo:
This java bean shell Command made the console out by j meter that is std out to be written in a file
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("D:\\dir1\\dir2\\abc.out")),true));
Make sure your path to file should have double backward slash

Call BeanShell function from BeanShell Programs like PreProcessor, PostProcessor, Assertion, etc

Pre-requisite:
Inside JMeter bin folder, I have edited BeanShellFunction.bshrc file to add my function as follows
String getMyString()
{
return "MyString";
}
I have enabled the BeanShellFunction.bshrc from jmeter.properties file as
beanshell.function.init = BeanShellFunction.bshrc
When I use the following syntax to call function it works fine.
${__BeanShell(getMyString())}
It works fine for below case:
Question:
How can I call the same function from BeanShell Programs like PreProcessor, PostProcessor, Assertion, etc.?
Analysis:
I tried with following but no luck:
String myStr = getMyString();
It gives an error as:
Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: `` String myStr = getMyString(); print("MyStr: "+myStr);'' : Typed variable declaration : Command not found: getMyString()
Add the next line to user.properties file (lives under "bin" folder of your JMeter installation)
beanshell.function.init=BeanShellFunction.bshrc
Restart JMeter to pick the property up
Once done you should be able to use it wherever required
Same approach applies to
beanshell.sampler.init
beanshell.assertion.init
beanshell.listener.init
etc.
References:
Configuring JMeter
How to Use BeanShell: JMeter's Favorite Built-in Component
From this SO post I found the solution: Calling Jmeter Functions from BeanShell Assertion Script
Solution
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.
Syntax to use:
You need use the same syntax used for sending URL parameter:
String myStr = "${__BeanShell(getMyString())}";
This automatically calls the beanshell method from defined .bshrc file.
For Advance Scripting
If your BeanShell function accepts a parameter:
String getMyString(String strParam)
{
return "MyString: "+strParam;
}
And you want to pass a property as a parameter to the BeanShell function, you can use following syntax:
String myStr = "${__BeanShell(getMyString("${__P(param1)}"))}";
Believe me it works and it does not give any syntax error.

Running a jmeter test via Blazemeter Taurus and Jenkins

I am having issues with my jmeter test.
I am using Blazemeter Taurus (bzt command) to run it, and I run it as a Jenkins job.
My issue is:
I created user defined values, which I set as Jmeter properties so I can pass them params from the command line:
example for a property I set
The issue occurs when I pass a number:
bzt -o modules.jmeter.properties.profileId=413 -o modules.jmeter.properties.lab=8050
these are parsed as 8050.0 and 413.0
Because the "lab" param is embeded in a url, it breaks the url.
When running this via command line with the jmeter command, this works fine.
I tried working around this with a bean shell sampler that does the following:
int a = Integer.parseInt(vars.get(${lab}));
String raw = String.ValueOf(a);
String processed = raw.substring(0,5);
vars.putObject("lab" ,new String(processed));
props.put("lab", lab);
log.info("this is the new " + ${lab});
but this fails.
any help would be appreciated.
In regards to Taurus issue - report it via Taurus support forum
In regards to Beanshell workaround - your code is not very correct, you need to amend it as follows:
int lab = (int)Double.parseDouble(props.get("lab"));
int profileId = (int)Double.parseDouble(props.get("profileId"));
props.put("lab", String.valueOf(lab));
props.put("profileId", String.valueOf("profileId"));
log.info("lab=" + lab);
log.info("profileId=" + profileId);
as stuff passed via -o modules.jmeter.properties should be accessed via props shorthand, not vars
Demo:
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using JMeter and Java API from Beanshell test elements in your JMeter test.

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