How to perform Distributed/Remote testing using JMeter

For this, you need to have one JMeter master and few JMeter agents.JMeter master is a JMeter instance which is used to direct the JMeter script to run them on agent instances.JMeter agents are also JMeter instances which are used to run JMeter scripts that direct from master.Look at the below diagram for more information.

To do above follow the below steps

  • Start JMeter in one machine and create a test plan and save.Will call this the master
  • Then go to \apache-jmeter-3.2\bin and get the jmeter.properties file.
  • Open this using a notepad and go to “Remote hosts and RMI configuration” section and add “remote_hosts=10.1.55.140,10.1.2.5” line there.Comment other remote hosts(ex: localhost).These remote hosts are the IP addresses of machines which the JMeter agents are running.
  • Now save the jmeter.properties and re-start JMeter to effect the change.
  • In agent machines, open Jmeter downloaded folder, go to bin and run “jmeter-server.bat” file.
  • Finally, go to JMeter master and run “Remote start all” under run menu.If you want to run the script in only one agent, choose Run -> Remote Start -><IP of the agent machine>.

Below image display the information in command window of JMeter agent side, when master run the script on an agent machine.

agentjm

 

How to load test a java API using JMeter

Let’s see how to perform a load test on a JAVA API.

  1. First create a JAVA API and create a .jar file.

use a random character generation function to create the java class.Follow below steps.

  • Create a class called “RandomString” and add the code below.
import java.security.SecureRandom;
public class RandomString {


         final String test = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";


        public String getRandomString(){
            SecureRandom rnd = new SecureRandom();
            int len = 10;
            StringBuilder sb = new StringBuilder(len);

            for(int i=0; i<len; i++)
                sb.append(test.charAt(rnd.nextInt(test.length())));
                return sb.toString();
        }

        public String getRandomString(int length){

            SecureRandom rnd = new SecureRandom();
            int len = length;
            StringBuilder sb = new StringBuilder(len);

            for(int i=0;i<len;i++)
                sb.append(test.charAt( rnd.nextInt(test.length())));
            return sb.toString();
        }

    }
  • Then create a main class called “TestRandomString” to execute the above class.Add the below code.
public class TestRandomString {
    public static void main(String[] args) {

        RandomString sm = new RandomString();
        System.out.println("Random String " +" "+ sm.getRandomString());
        System.out.println("Random String  "+" "+ sm.getRandomString(6));

    }

}
  • Now create a “.jar” file using above classes.
  • Then go to JMeter and  start it and create a new test plan.Add elements to it like below.

-Add a thread group to test plan

-Add a bean shell sampler to thread group

-Add a results tree listener to the thread group

sampleone

  • Browse and add the created .jar file in Test Plan.

javajar

  • Add the below code to the beanshell script

import com.lbf.qa.util.StringManager;
StringManager st = new StringManager();
System.out.println(“random string = “+” “+st.getRandomString());
System.out.println(“random string = “+” “+st.getRandomString(6));

sampletwo

  • Now adding number of threads,rampup and loop in thread group, you can run your load test on jmeter and it will show results as below in it’s command window.

The below has done for thread no=1

samplethree

How to record test cases of a web application, if you are testing behind a firewall/proxy server

Suppose, you are to test the performance of a web application which is accessible via a proxy server.And you are doing to via the recording template in JMeter.

Start JMeter in the following way.

-Open your PC’s command prompt.

-Go to the bin folder JMeter bin.

-Type jmeter.bat -H <url/ip of the web app> -P <port of the app> (ex: jmeter.bat -H 10.1.120.26 -P 4562)

-Enter.

Now the JMeter will open and select recording template and proceed with the recording as the usual way.