In Bean shell sampler I am getting the line count how to pass the line count to the Threadgroup
import org.apache.commons.io.FileUtils;
int lines = FileUtils.readLines(new File("path to data file")).size();
vars.put("lines", String.valueOf(lines));
this lines variable I want to use to thread group how I can achieve this
please help me on this
Updating the output of Debugsampler
JMeterVariables:
JMeterThread.last_sample_ok=true
JMeterThread.pack=org.apache.jmeter.threads.SamplePackage#1e59359f
START.HMS=081505
START.MS=1449648905577
START.YMD=20151209
TESTSTART.MS=1449652524822
applicationname=test
buildinfo=../data/Test.csv
env=Test
lines=77
package=.zip
First, you can use Debug sampler to verify if you are successfully getting the desired value in your variable.
Second, you can simply call the this variable just like other user defined variables in JMeter. Put ${lines} where required.
If you still face any issues, then let me know in comment or edit your question to add further details.
You cannot configure Thread Group loop count this way as it is being populated on JMeter launch. Here are the options:
Use Loop Controller instead, it is possible to set the number of loops via variable
Define "Loop Count" in Thread Group as a property, like ${__P(loops,)}. In that case you can get CSV file lines count before the test i.e. using the relevant OS command like:
type c:\temp\file.csv | find /c /v "~~~" - for Windows
cat /tmp/file.csv | wc -l for *nix
and pass the resulting value to JMeter via -J command line key as:
jmeter -Jloops=XX -t /path/to/your/testplan.jmx ...
See Apache JMeter Properties Customization Guide for more information on JMeter properties and ways of setting and overriding them.
Related
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
I have a JMeter script, where I have some user defined Variables like FILE_SAVE_PATH. This script should be started on a command line with parameter -J. So in the GUI, I changed the value for the variable FILE_SAVE_PATH to ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}, because the test should save there a file, but only on my machine. On the machine, where the script will be started from command line, it should save the file into another path.
My problem is now this: When I test this JMeter script on my machine in the GUI, I get an output of this:
About to replace in property of type: class org.apache.jmeter.testelement.property.StringProperty: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
2017/04/04 17:09:38 DEBUG - jmeter.testelement.property.AbstractProperty: Not running version, return raw function string
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: ${__P(FILE_SAVE_PATH, "C:\svn\trunk\dir")}
But I think, the last line should be something like this:
2017/04/04 17:09:38 DEBUG - jmeter.engine.util.ValueReplacer: Replacement result: "C:\svn\trunk\dir"
So, how to change the test to get the result I want?
Escape every backslash with another one - C:\\svn\\trunk\\dir, or use unix slash, JVM's gonna handle it right: C:/svn/trunk/dir
And remove the doublequotes, they're not needed.
P.S. I presumed you're not using that notation in the Beanshell/JSR223 context. If you do - stop there and use the legit way to access properties.
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.
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.
How can I run a specific thread group in a Test Plan from the command line? I have a Test Plan (project file) that contains two "thread groups": one for crawling a site and another for calling specific urls with parameters. From the command line I execute with Maven, like so:
mvn.bat -Dnamescsv=src/test/resources/RandomLastNames.csv
-Ddomainhost=stgweb.domain.com -Dcrawlerthreads=2 -Dcrawlerloopcount=10 -Dsearchthreads=5 -Dsearchloopcount=5 -Dresultscsv=JmeterResults.csv clean test verify
I want to pass an argument to run only one of the two "thread group" in that project file. Can you do that with JMeter? I don't want to use an IF controller unless I have to because it feels like a "hack". I know that SoapUI lets you do this with the '-s' option.
I asked this question on the JMeter forum also.
In our tests we use the while controller. It doesn't look like a hack to me and works well. You can turn thread groups on and off easily with the JMeter properties. Note you can't change its status when the test is already running though.
Add While controller - ${__P(threadActive)}
Set JMeter property on JMeter load ( -JthreadActive = true )
Run test
Please note ${__P(threadActive)} equates to ${__P(threadActive)} == true, anything other than true will result in that thread group not running