Introduction to Apache JMeter : Beginner’s Guide

Ashok Garg
BYJU’S Exam Prep Engineering
7 min readSep 12, 2018
  1. It is an open source Java-based application mainly used for load testing/Stress Testing.
  2. It is platform independent.
  3. JMeter can conduct load and performance test for many different server types — Web — HTTP, HTTPS, SOAP, Database via JDBC etc.

Installation:

Download JMeter: https://jmeter.apache.org/download_jmeter.cgi

Launch the following from /bin folder using terminal:
For Mac:
jmeter.sh
For Windows: .jmeter.bat

Basic terminology in JMeter :

Test Plan : A test plan describes a series of steps JMeter will execute when run. A complete test plan will consist of one or more Thread Groups, logic controllers, sample generating controllers, listeners, timers, assertions, and configuration elements.
Thread Group : A thread group is a set of threads executing the same scenario. Set the number of iterations in the configuration.
Number of Thread(Users) : Number of users to be tested with.
Ramp Up Period : Ramp up period defines the amount of time in which all users will be initialised like in 10ms 100 (Number of Users)
Loop Count : It defines the number of times Thread Group will execute.
Samplers : Samplers tell us which type of request is to be sent like HTTP,HTTPS etc.

Latency: It is the time when the first piece of information is received i.e. the first byte of data is received.
Connect Time: It is the time taken to establish connection with the server.
Sample Time: It is the time taken to receive all data.
Sample — Sequence of sample number.
Bytes — Size of data received.

WorkBench : A workbench is a place where you can store the elements which are not in use in your current test scenario.
HTTP(s) Test Script Recorder : It helps testers in recording the script and then configuring the load for each transaction.

Assertions : it helps in verifying the results which are obtained under results tree.

Response Assertion : In this we can add strings and then compare them with the responses received.
Duration Assertion : Duration Assertion validates that the server responded within a given amount of time
Size Assertion : Size assertion helps in validating the size which is defined for a server to respond with in ramp up time.
HTML Assertion : HTML Assertion verifies the HTML syntax of the response received from a server.

Config Element :

CSV Data Set Config : This is used when we have to upload any data externally through CSV like an API requires any id such that the id is a variable and can be uploaded through CSV.

User Defined Variables : It helps jMeter to pick values from a pre-defined variable e.g. if we need to test on various servers like Staging or Test Server.
HTTPS Requests Default : This is default server on which we have to make the request like Staging or Test Server so that we need not to change the default url every-time.
HTTPS Cache Manager : This is used to manage cache of the server.
HTTPS Cookie Manager :This is used to manage the cookies across all APIs

Logic Controllers : Logic controllers and timers help jMeter control the flow of transactions like we can have a:-

-While controller in which we can pass a variable for an API and can upload a CSV which will replace the variable in API.
-Loop Controller will run the samplers/requests stored in it a user-defined number of times or forever (if forever checkbox is selected).

1.Select ‘Forever’ checkbox to run tests infinite times
2.Enter loop count number to run tests a fixed number of times

JSON Extractor : JSON extractor is used to extract the required variable for a particular API like one API is dependent on the other e.g. :

/login API will provide us the user id which we can extract and use in /getWalletOfUser API.

In the above screenshots, we are creating a while controller which passes variables uploaded from CSV to run the API.

Bean shell Sampler : It is used to write scripts in java.

Below is an example in which we are using BSS to write data from an API in a file.

String name = vars.get(“tags”); //tags will be available in the response of the API.f = newFileOutputStream(“/Users/ashokgarg/Desktop/tags.csv”,false);
//Path to save the file to your machine
p = new PrintStream(f);//Print the output in built java functionthis.interpreter.setOut(p); print(name.split(“,”)); //Spilt the response from the stringf.close();

So this will fetch the response from /login api and write the results into a file ` tags.csv

Constant timer : It is added to put a delay among the API requests on the server e.g. if it is set to 10s, every API hit will wait for 10 seconds to execute after the former.

Pre Processors : They are added at thread group level so that they can be executed before the sampler request e.g. data required in an API from DB can be defined in preprocessor.

Post Processors : They are added at thread group level and execute after the sampler . It is used to process response data from server and extract specific values.

Summary Report :

Throughput : It represents the ability of the server to handle a heavy load. The higher the Throughput , the better the server performance.

Deviation : It is used to find the difference of the time taken from average time allotted for a sampler hit

Some important formulas :

 endTime = lastSampleStartTime + lastSampleLoadTime
startTime = firstSampleStartTime
converstion = unit time conversion value
Throughput = Numrequests / ((endTime - startTime)*conversion)

Steps :

  1. Create a Test Plan > Add Thread Group > At Thread group level add no of users > Ramp up period > and loop count.
  2. Add HTTP Cookie ,Cache Manager at Thread group level.
  3. Add sampler > Http Header Manager(Default values required to execute Sampler request).
  4. Add HTTP Default Request (Specify server on which you have to test).
  5. Add JSON Extractor to fetch any values from the response and Bean Shell sampler to write CSV data.
  6. Add Pre processor and Post processor to manage your requests (Specified Above).
  7. Add Listers : a) View result Tree b)Summary Report c) Graph Results
  8. Tap Command +E , Command +S , Command + R for mac after entering all above requests and check the Through Put and Standard Deviation.

Criteria for load testing /Stress Testing :

  1. Specify the number of users > execute the API > login on the server under test use “top command” > check CPU usage and CPU state.
  2. Increase the no. of users at sampler level and set loop count to 10, so that requests can be executed 10 times, and check server CPU usage, API hits and server logs.
  3. All APIs under test should give 200 response code, and server CPU usage and CPU state should be normal as before

Unless you have users actively hitting your server, you should see that the CPU(s) % user usage should be very low or 0%, and the CPU(s) % idle (id) should be 99%+.

Apache JMeter : Non GUI Mode

Why to use JMeter in Non GUI/command line mode:

1.We can increase the number of threads, unlike GUI mode which leads to crashes.
2.GUI mode is not suited for heavy test scenarios as jMeter consumes more memory and CPU, and so may affect your test results.
3.To increase jMeter capabilities i.e. to get more requests per second.

Steps : Save the jmx file into a folder >> Open terminal>> go to folder in which jmx (Java Management Extensions) file is saved and execute the following command:-

 jmeter -n –t login.jmx -l testresults.jtl
  • n: It specifies JMeter is to run in non-gui mode.
  • -t: Name of JMX file that contains the Test Plan.
  • -l: Name of JTL(JMeter text logs) file to log results.
  • -j: Name of JMeter run log file-R: list of remote servers.
  • H: proxy server hostname or ip address.
  • -P: proxy server port.
Non GUI mode O/P

--

--