How to run JMeter from Java for the remote (distrubted) application - jmeter

From GUI we can follow
Run -> Remote Start -> (select the ip, which we want to run remotely)
From command line also we can specify -R flag to run remote jmeter application.
But here I am creating jmx file using Java and I want run this one from Java only. So, is there any way achieve this

You need to use DistributedRunner class like:
List<JMeterEngine> engines = new LinkedList<>();
Properties remoteProps = new Properties();
//set properties you want to send to remote clients here
DistributedRunner distributedRunner=new DistributedRunner(remoteProps);
List<String> hosts = new LinkedList<>();
//add your JMeter slaves here
hosts.add("remote_host_1");
hosts.add("remote_host_2");
distributedRunner.setStdout(System.out);
distributedRunner.setStdErr(System.err);
distributedRunner.init(hosts, testPlanTree);
engines.addAll(distributedRunner.getEngines());
distributedRunner.start();
For example of creating a JMeter test from Java code using JMeter API check out Five Ways To Launch a JMeter Test without Using the JMeter GUI guide.
Also remember that JMeter is open source so you can look for implementation of this or that functionality directly in JMeter's source code.

You must implement an interface RemoteJMeterEngine. Also, have a look at RemoteJMeterEngineImpl class for reference.
If you have tried running test locally trough Java, this should not be much more complicated to achieve.

Related

How to pass properties files to Jmeter server in distributed setup

I have Jmeter distributed setup (One client and one server).
In single jmeter instance I'm using the below command to execute jmeter with a properties file that will pass the user defined variables to my test script
/apache-jmeter-5.3/bin/jmeter -p reco.properties -n -t Performance.jmx -l test2server.csv
When I tried to use the same command including the server configurations I noticed that the values defined in my "reco.properties" are not picked at the test execution.
Instead I need to pass all the variables with "-G" argument. Ex:
/apache-jmeter-5.3/bin/jmeter -GENV=test -n -t Performance.jmx -l test2server.csv
is it possible that I can pass all variables in a properties file for Jmeter Server as I do in the normal execution with "-p"?
Thank you
As per JMeter Documentation:
If the test uses any data files, note that these are not sent across by the client so make sure that these are available in the appropriate directory on each server. If necessary you can define different values for properties by editing the user.properties or system.properties files on each server.
So currently as per JMeter 5.4 this is not supported, you either need to pass the properties one-by-one like -Gprop1=value1 -Gprop2=value2 or to copy the .properties file to all the slave machines
The limitation can be also worked around via getting the properties from a dataabse using JDBC Request sampler or with plugins like HTTP Simple Table Server or Redis Data Set
If you do believe that the feature is essential you can try raising an enhancement request in JMeter Bugzilla

Jmeter Cross-Platform Path config issue

I am working on master-slave Jmeter configuration, my data set exist on each machine on a different path for (macOS, Windows).
I use a global data set to fetch data for multiple thread groups.
However, the variable/property can be different across platforms.
I also tried the JSR223 to check the, but still no luck. check out the below snapshot.
I am trying to make sure that once I ran my test from Master (Mac), it also run on Windows.
Any thoughts how to do that on multiple platform setup.
The best solution is placing your test data under the same path which will be relative to JMeter working directory, this way you will not have to change anything in your script.
If for some reason you cannot afford this you can add a JSR223 Sampler to your Test Plan and use the code like:
if (org.apache.commons.lang3.StringUtils.containsIgnoreCase(System.getProperty('os.name'), 'Windows')) {
vars.put('data-path', 'c:/windows/specific/path')
} else {
vars.put('data-path', '/macos/specific/path')
}
It will detect the operating system name in the runtime and you will be able to define an OS-specific paths using the above approach.
vars is a shorthand for JMeterVariables class instance, the above code defines ${data-path} JMeter Variable which you can use later on for specifying data files locations. See Top 8 JMeter Java Classes You Should Be Using with Groovy to learn more about JMeter API shortcuts exposed to JSR223 Test Elements

Read system property from a target server for JMeter GUI tests

I'm running tests on JMeter on a target server. So I start JMeter(GUI) on my Mac but it points to a Linux server and runs tests against it. I need to read a system property on the target server and use that in my If Controller.
System.getProperty("prop_name"); works if the property is defined on my mac. But it doesn't pick it up from the target server.
Any suggestion is much appreciated!
Thanks!
I can suggest 2 options:
If your application under test has JMX enabled - you can read the "interesting" property value in one of JSR223 Test Elements using Groovy code. The properties can be queried from the RuntimeMXBean.
See Java Management Extensions (JMX) Technology Tutorial for more information.
If there is no JMX exposed to the outer world you can still try to get the property value remotely. Be aware that you can run any command or program on remote Linux instance using SSH Sampler. SSH Sampler can be installed using JMeter Plugins Manager.
See How to Run External Commands and Programs Locally and Remotely from JMeter for more information.

Can't get property value in Jmeter master-slave mode

I wrote a jmeter plugin which is used to analyze jmeter sample event, and send the results to a specified server. In this plugin, user can config the server IP/Port etc. These configurations normally use the variable and read them from given CSV file(using CSV config plugin). For example, the variables could be filled like: ${serverIP}, ${serverPort} on plugin GUI.
Ok, my jmx and plugin with these configurations can work well in single mode. But now, I have requirement to run them in Master-Slave mode, the problem is coming, the ${serverIP}, ${serverPort} can't not be converted to real value in Master side. I added some logs in my plugin codes which I used Jemter API to read its value
public String getServerIP() {
return getPropertyAsString(SERVER_IP);
}
and found jmeter straightly return me with the variable name: ${serverIP}, not like what it did in single mode that convert them to real value.
I googled and searched Jmeter doc, but nothing found, How could I let jmeter read configured variable from given CSV file in Master side(I know it could do this in Slave side). Can't Jmeter support it?
Any help are really appreciated! If you need more infos, please let me know, Thanks a lot!

How do I write a Jmeter script without using the GUI?

Is there a way to create a JMeter test plan without going through the GUI?
I.E. can you create a script with pure code?
In a way - yes. You can create the JMX file in a text editor, but it would require knowing what every component needs.
You're better off creating in GUI mode and modifying the JMX file manually as needed.
Yes it's possible to write a test plan just by writing JAVA code.
The key classes to look into are:
StandardJMeterEngine - the main class which configures the Test Plan and executes it
HashTree - a special collection which holds Test Plan elements
A minimum of JMeter Controllers necessary to run the test.
Reference: http://blazemeter.com/blog/5-ways-launch-jmeter-test-without-using-jmeter-gui
I can write and run JMeter without GUI. JMeter use JMX file, as you can here.
Not entirely sure why you would want to write a JMX in this way? ... But yes, entirely possible if you understand the structure of the document.
You could use any basic text editor to achieve this (notepad / notepad++ for example).
A JMX file is a saved JMeter project in XML format. ~ Ref
Depending on your needs you could the browser editor from the JMeter Plugins website.
jmeter-plugins.org/editor/
Or simply save a basic JMX as a template and spend loads of time learning the structure, syntax and namings etc.

Resources