JMeter: Top 20 Interview Questions for Freshers

Based on JMeter Masterclass for Beginners

JMeter can perform load testing, stress testing, and functional testing

JMeter works by simulating multiple users accessing the application or service at the same time, thus generating load on the application
It then measures the performance of the application under this load and provides reports on response times, errors, etc.

A very basic Test Plan in JMeter consists of 3 sections:

To create a test plan in JMeter, you need to first create aThread Group, which represents a group of users
Then, you need to add one or more samplers, which represent the actions that users will perform on the application
Finally, you can add one or more listeners, which will collect data on the performance of the application

A sampler in JMeter represents an action that a user will perform on the application. For example, a sampler could be an HTTP request to a web page, a JDBC request to a database, or a JMS request to a messaging system

A listener in JMeter collects data on the performance of the application during the test
For example, a listener could be a graph that shows the response times of the application over time, or a table that shows the number of errors encountered during the test

To run a test in JMeter, you need to first configure the test plan by setting the properties of the Thread Group, samplers, and listeners
Then, you can start the test by clicking the "Start" button on the main JMeter window

To create a variable in JMeter, you can use the "User Defined Variables" configuration element
This allows you to define one or more variables that can be used throughout the test plan. For example, you could define a variable called "baseUrl" that contains the URL of the web application being tested
Video - JMeter Functions and Variables

A regular expression extractor in JMeter is used to extract data from a response and store it in a variable for later use
To use a regular expression extractor, you need to first specify the text to be searched (e.g. the response body), and then define a regular expression that matches the data you want to extract
Example: Let's say you want to extract the value of a hidden input field from an HTML response. You can use the following regular expression:

<input type="hidden" name="csrf_token" value="(.+?)"/>

This regular expression will match the entire input field and capture the value of the "value" attribute. You can then use a regular expression extractor to store this value in a variable.

See video - JMeter Correlation using Regular Expression Extractor

A correlation in JMeter is the process of replacing a dynamic value in a request (e.g. a session ID) with a value that was previously extracted from a response
Correlation is necessary because many web applications use dynamic values to maintain state, and these values can change from one request to the next

To use a correlation in JMeter, you need to first extract the dynamic value from a response using a regular expression extractor. Then, you need to replace the dynamic value in the subsequent request with the new value that was extracted
This can be done using a "Regular Expression" or "Boundary Extractor" configuration element to extract the dynamic value and a "BeanShell PreProcessor" to replace the value in the subsequent request

Example: Let's say you have a web application that uses a session ID to maintain state. The session ID is included in the response to the login request, and needs to be included in subsequent requests to access other pages. You can use the following steps to perform correlation in JMeter:

See video - JMeter Correlation using Regular Expression Extractor

To create a loop in JMeter, you can use the "Loop Controller" configuration element. This allows you to specify a group of samplers that should be repeated a certain number of times or until a certain condition is met

Example: Let's say you want to simulate 100 users accessing a web page repeatedly. You can use the following steps to create a loop in JMeter:

Step 1: Create a Thread Group with 100 users.

  • Add a "Thread Group" to the test plan.
  • Set the "Number of Threads" to "100".
  • Set the "Ramp-Up Period" to "1".

Step 2: Add a sampler that accesses the web page.

  • Add an "HTTP Request" sampler to the Thread Group.
  • Set the "Server Name or IP" and "Path" to the URL of the web page.

Step 3: Add a Loop Controller to the sampler.

  • Right-click on the sampler and select "Add -> Logic Controller -> Loop Controller".
  • Set the "Loop Count" to "10" to repeat the sampler 10 times for each user.

This will simulate 100 users accessing the web page 10 times each, for a total of 1000 requests.

Assertions in JMeter are used to verify that a response meets certain criteria, such as containing a certain string or having a certain response code
To use an assertion in JMeter, you can add an "Assertion" configuration element to a sampler and choose the type of assertion to perform.

Example: Let's say you want to verify that a web page contains a certain string in the response body. You can use the following steps to add an assertion to a sampler in JMeter:

Step 1: Add an HTTP Request sampler to the Thread Group.

  • Add an "HTTP Request" sampler to the Thread Group
  • Set the "Server Name or IP" and "Path" to the URL of the web page

Step 2: Add an assertion to the sampler.

  • Right-click on the sampler and select "Add -> Assertions -> Response Assertion".
  • Choose the "Contains" radio button under "Patterns to Test".
  • Enter the string you want to verify in the "Patterns to Test" field, e.g. "Welcome to our website".
  • Click "OK" to save the assertion.

This will cause the sampler to fail if the response body does not contain the specified string.

In JMeter, a controller is a configuration element that is used to group other elements, such as samplers, timers, and assertions
Controllers provide a way to organize the test plan and control the order in which elements are executed

There are several types of controllers in JMeter, including:

  • Simple Controller: Groups other elements but does not affect their order.
  • Loop Controller: Repeats a group of elements a certain number of times or until a certain condition is met.
  • If Controller: Executes a group of elements only if a certain condition is true.
  • While Controller: Repeats a group of elements while a certain condition is true.
  • Switch Controller: Executes one of several groups of elements based on a variable value.

Example: Let's say you want to test a web application that has several pages with different functionality. You can use the following steps to organize your test plan using controllers in JMeter:

Step 1: Create a Thread Group

  • Add a "Thread Group" to the test plan.

Step 2: Create a Simple Controller for each page.

  • Right-click on the Thread Group and select "Add -> Logic Controller -> Simple Controller".
  • Give the controller a descriptive name, e.g. "Home Page".
  • Add samplers, timers, assertions, and other elements to the controller that simulate user behavior on the page.

Step 3: Add a Loop Controller to repeat the pages.

  • Right-click on the Thread Group and select "Add -> Logic Controller -> Loop Controller".
  • Set the "Loop Count" to "10" to repeat the pages 10 times.
  • Add the Simple Controllers to the Loop Controller in the order you want them to be executed.

This will simulate 10 users accessing the web application and navigating through the pages in the specified order

Timers in JMeter are used to simulate user think time or delays between requests. They can be added to samplers or controllers to create a more realistic load on the server being tested.

There are several types of timers in JMeter, including:

  • Constant Timer: Delays for a fixed amount of time.
  • Gaussian Random Timer: Delays for a random amount of time with a normal (Gaussian) distribution.
  • Uniform Random Timer: Delays for a random amount of time within a specified range.
  • Poisson Random Timer: Delays according to a Poisson distribution.

Example: Let's say you want to simulate users accessing a web page with a delay between requests. You can use the following steps to add a timer to a sampler in JMeter:

Step 1: Add an HTTP Request sampler to the Thread Group.

  • Add an "HTTP Request" sampler to the Thread Group.
  • Set the "Server Name or IP" and "Path" to the URL of the web page.

Step 2: Add a timer to the sampler.

  • Right-click on the sampler and select "Add -> Timer -> Constant Timer".
  • Set the "Thread Delay" to "5000" to delay for 5 seconds between requests.
  • Click "OK" to save the timer.

This will cause a 5-second delay between each request made by the sampler

Parameterization in JMeter is the process of using variables to replace values in requests, allowing for greater flexibility and scalability in testing. Parameters can be created at the thread group level or within individual samplers, and can be sourced from a variety of sources, such as CSV files or random values.

Example: Let's say you want to test a web application that has multiple users with different login credentials. You can use the following steps to parameterize the login requests in JMeter:

Step 1: Create a CSV file with login credentials.

  • Create a CSV file with columns for username and password.
  • Add multiple rows with different login credentials.

Step 2: Add a CSV Data Set Config element to the Thread Group.

  • Add a "CSV Data Set Config" element to the Thread Group.
  • Set the "Filename" to the path of the CSV file.
  • Set the "Variable Names" to the names of the columns in the CSV file, separated by commas, e.g. "username,password".
  • Set the "Recycle on EOF" to "False" and "Stop Thread on EOF" to "True" to stop the thread when all rows have been used.

Step 3: Parameterize the login request.

  • Add an "HTTP Request" sampler for the login page.
  • Replace the username and password values in the request body or URL parameters with variables, e.g. "${username}" and "${password}".

This will allow JMeter to simulate multiple users logging in with different credentials

Running JMeter tests in non-GUI mode is useful for running tests in a headless environment or for running tests in a distributed environment
Non-GUI mode allows JMeter to run without the graphical user interface, using command-line options to specify the test plan and output files

Example: Let's use the following steps to run JMeter tests in non-GUI mode:

Step 1: Create a test plan in JMeter GUI mode.

  • Open JMeter and create a test plan as you normally would in GUI mode.
  • Save the test plan file to a location on your computer.

Step 2: Open a command prompt or terminal window.

  • Open a command prompt or terminal window on your computer.

Step 3: Navigate to the JMeter bin directory.

  • Navigate to the bin directory in the JMeter installation directory, e.g. "C:\apache-jmeter-5.4.1\bin" on Windows or "/usr/local/apache-jmeter-5.4.1/bin" on Linux.

Step 4: Run the JMeter test in non-GUI mode.

  • Use the following command to run the JMeter test in non-GUI mode:
    jmeter -n -t [test plan file] -l [results file]
    Replace [test plan file] with the path to your test plan file and [results file] with the path to the file where you want to save the test results.

    This will run the JMeter test in non-GUI mode and save the test results to the specified file

    Optionally, you can specify additional command-line options, such as the number of threads or the duration of the test.

JMeter provides a built-in HTML report generator that can be used to create detailed reports of test results. The HTML report includes summary statistics, graphs, and detailed information about individual requests.

Example: Let's say you want to generate a HTML report for a test plan that you ran in non-GUI mode. You can use the following steps to generate the HTML report in JMeter:

Step 1: Navigate to the JMeter bin directory.

  • Navigate to the bin directory in the JMeter installation directory, e.g. "C:\apache-jmeter-5.4.1\bin" on Windows or "/usr/local/apache-jmeter-5.4.1/bin" on Linux.

Step 2: Run the JMeter report generator.

  • Use the following command to run the JMeter report generator:
    jmeter -g [results file] -o [output directory]

    Replace [results file] with the path to your test results file and [output directory] with the path to the directory where you want to save the HTML report.

    This will generate the HTML report in the specified output directory.

    Optionally, you can specify additional command-line options, such as the title of the report or the location of the JMeter properties file

JMeter is commonly used for load testing, which involves simulating multiple users accessing a web application simultaneously to test its performance under high load
Load testing can help identify performance bottlenecks and determine the maximum number of users that a web application can handle.

Example: Let's say you want to use JMeter to load test a web application. You can use the following steps to create a basic load test in JMeter:

Step 1: Create a Thread Group.

  • Create a new Thread Group in JMeter.
  • Set the number of threads to the number of users you want to simulate.
  • Set the ramp-up period to the time it should take for all users to start the test, e.g. 30 seconds for 10 users.

Step 2: Add samplers to the Thread Group.

  • Add HTTP Request samplers to the Thread Group for each page or action that you want to test
  • Set the URL and HTTP method for each sampler.
  • Optionally, you can add additional parameters, such as headers or post data

Step 3: Add listeners to the Thread Group.

  • Add listeners to the Thread Group to capture test results, such as the Aggregate Report listener or the Summary Report listener.
  • Set the output file or directory for the test results.

Step 4: Run the load test.

  • Run the load test in GUI mode to verify that it runs correctly.
  • Once you are satisfied with the test plan, run the test in non-GUI mode using the steps outlined in Question 18.
  • Generate an HTML report using the steps outlined in above question

1. Create a realistic test plan. i.e. allow some breathing time between sampler requests

Like in real-world the users may not all start at one time and keep on hitting the application with multiple inputs

You can have some ramp-up and ramp-down so that it does not start with a full load at the start
Try checking the concurrency thread group, and ultimate thread group
Add think time between requests
You have the option when you do right-click on the thread group
Also when you add a template you can see examples of think time

2. Environment and system where you run these tests should be configured according to the load you wish to put
Performance Testing Environment can include:
Servers
CPU
Memory
OS
Network
You have to discuss this with your networks/ops teams to see a suitable configuration and create remote systems/servers for this.

Some other good practices

1. Always do performance testing on a separate env. Create a sep env that will not be used for other activities

2. Run your tests with the same infrastructure, network stats etc. To get consistent results and can be compared with the baseline

3. Create a more realistic test - have some think-time between requests

4. Add ramp-up time, Do not start directly with 500 users

5. Always have a baseline to compare your test against

6. JMeter checks the server performance and does not care for browser render time. So if you see your JMeter results are good but your application is still slow. Check for client-side performance testing tools

7. Focus on the areas that need performance testing based on the user scenario

8. Always document your results

0