Load Testing Using Apache JMeter — Practice

PART ıı

Alperen Bekçi
BosphorusISS

--

This article is for load testing practice using Apache JMeter. If you need to know definitions of Jmeter elements and more information go to the link below.

Performance and Load Testing with JMeter

Golden Rules While Using JMeter

  • Use config elements for dynamic structure.
  • Use timers for UX
  • Use Logic Controllers for a good scenario
  • Use Pre and Post Processors for parsing requests and responses

Golden Rules of Load Testing using JMeter

  • Don’t Use in GUI Mode

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

1. Increasing the number of threads (after a certain limit) due to which JMeter crashes in the GUI mode.

2. JMeter consumes memory and CPU and it may affect your test results for heavy test scenarios.

3. JMeter consumes memory and CPU and it may affect your test results for heavy test scenarios.

4. To increase JMeter capabilities.

5. The individual system can handle up to 300 to 500 threads as per your System configuration and it will differ if you are using less memory.

6. The JMeter probably will crash when you are running the script with lots of users.

if you start the JMeter in the command line, you’ll see this message
  • Don’t use Listeners

Listeners is not necessary in Non-GUI Mode and it consumes the memory and CPU.

Whenever we are using more listeners and View Result Tree and it will consume a lot of CPU and Memory, it will make the JMeter application to freeze for a while so your load testing will not be more accurate.

  • Use Post-Processor and Assertion efficiently

Ensure you use them when required and use the ones that consume less memory and CPU.

  • Use the most performing scripting language.

Installation

JMeter is very easy to install and run. If the operating system has Java 8 and higher versions, it can run on all operating systems.

  1. Download JMeter from JMeter source

2. Go to “/path/to/jmeter/folder/bin”

3. Run this command using the terminal:

Linux and Mac: jmeter.sh
Windows: jmeter.bat

Our Sample Test Scenario:

  1. Create a form from Google Forms,
  2. Go to our google form,
  3. Fill in and submit it.

We will use the Favorite Music Genres form in this article. If you want to practice with the same parameters, you can create a new form according to this form.

Form
JMeter Welcome Screen

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.

Let’s add a thread group:

Right-click the Thread Group and Add -> Thread (Users)-> Thread Group

Add Thread Group
Thread Group

First, we should look at the request data and header from the Google Developer Console.

Let’s add HTTP Request.

  1. Beginner Scenario — Add HTTP Request

First, go to the URL of the form and inspect it (CTRL + Shift + I). Click the Network option. Fill the form and submit.

Examine the request header and request data.

Request header
Request data

Right-click the Thread Group and Add -> Samplers -> HTTP Request, add new HTTP Request and adapt parameters according to the form request body.

HTTP Request

Add new HTTP Header Manager to configure the HTTP request header according to the form request header.

Follow the images below:

Right-click the Thread Group and Add -> Config Element ->HTTP Header Manager
Adapt HTTP Header Manager according to the form request header

HTTP Header Manager: it is optional, the test case will not fail.

Our beginner scenario is ready. If you run this, the first request will be gone.

2. Intermediate Scenario — Add some defined variables

Now, add some customization to prevent duplicate data.

First, we need to add HTTP Request Defaults if we use multiple HTTP Requests. After adding the HTTP Request Defaults and defining the variables, the HTTP Request will include automatically the protocol, server name, IP, port number, content-encoding, etc. We don’t need to write these parameters to all HTTP Requests.

Right-click the Thread Group and Add -> Config Element -> HTTP Request Default

Adding HTTP Request Defaults
Configuring HTTP Request Defaults

You can also define variables before the test cases. It also prevents duplicate data and keeps test data in one element.

Right-click the Test Plan and go to Add -> Config Elements -> User Defined Variables and define our test variables.

User Defined Variables
User Defined Variables Usage

We can use defined variables as ${variable-name}, wherever we want in Test Plan.

The intermediate scenario is ready.

3. Upper-Intermediate Scenario — Use a CSV file to reading data and use JMeter Helper Functions

We can use our data in JMeter test cases. There are multiple ways to use our data. I will explain the most used and most practicable.

Let’s prepare our CSV file:

Dummy,Person,Male,Engineer,Classic Rock
Dummy_2,Person_2,Female,Doctor,Rock Music
Dummy_3,Person_3,Female,Lawyer,Hip Hop/Rap
Dummy_4,Person_4,Male,Lawyer,Hip Hop/Rap
Dummy_5,Person_5,Prefer not to say,Lawyer,Hip Hop/Rap
Dummy_6,Person_6,Other,Lawyer,Hip Hop/Rap
Dummy_7,Person_7,Female,Lawyer,Rock Music
Dummy_8,Person_8,Male,Lawyer,Rock Music
Dummy_9,Person_9,Female,Lawyer,Hip Hop/Rap
Dummy_10,Person_10,Male,Lawyer,Indie/Alternative Rock

Right-click the Test Plan and go to Add -> Config Elements -> CSV Data Set Config.

  • Write CSV path,
  • Define variable names to use in JMeter.
CSV Data Set Config

Now, increase our users to see the results and compare them with CSV’s data.

Increasing Number of Threads

JMeter functions are special values that can populate fields of any Sampler or other element in a test tree. It is a useful tool to make your test plan flexible and maintainable according to the user manual.

When you want to define a JMeter function, you can easily write it manually with some simple functions. But in most of the other cases, you have to use the Function Helper Dialog to define correctly the function without any issue related to the syntax, or wrong order of the parameters.

Let’s use the Random String function. Go to Tools -> Function Helper Dialog. Click the combo box and select “Random String”. Determine the size and content of the string,

JMeter Functions

and let’s use this function for name and surname.

JMeter Random String Functions

4. Advance Scenario — Use Controller and Assertions

We should add Think Time to be closer to the end-user scenario. Think Time can be said as end-user action time. We will use “Uniform Random Timer” in this scenario.

Right-click the HTTP Request and go to Add -> Timer -> Uniform Random Timer.

Uniform Random Timer

It is also useful to add assertions in the load test, so we can check some values from the response data.

We will use Size Assertion and Response Assertion (to check the response code).

Size Assertion
Response Assertion

Using a controller is important for load testing. We will use the Simple Controller in this test because there is only one request. Simple Controller is only for classification, it has no other function.

It is not a realistic scenario for 100 users to fill the form at the same time. Therefore, let’s increase the ramp-up period. Since we set the Ramp-up Period as 5 (seconds), 20 users will fill the form at the same time(100/5).

Increasing Ramp-up period

Reporting

You can browse the Performance and Load Testing with JMeter for more information about reporting.

Run the test in non-GUI mode from JMETER_HOME/bin and save the report as CSV or JTL format.

Windows: jmeter –n –t [path to test JMX file] –l [path to result file]

Linux and Mac: jmeter.sh –n –t [path to test JMX file] –l [path to result file]

Use the following command to generate a report from an existing sample CSV/JTL result file as above.

Windows: jmeter -g [path to result file] -o [path to report output folder]

Linux and Mac: jmeter.sh -g [path to result file] -o [path to report output folder]

Open the Index.html from the report output folder and you can see your load testing consolidate report.

Report Dashboard
Form Responses
Form Responses

Don’t think it’s over…

Recording with Think Time

Normally, writing HTTP Requests can be troublesome in advanced test scenarios. For instance, your scenarios have more than 50 requests or what actually you need. In this case, it can be difficult to write and check the header and body of HTTP requests. JMeter has one easy way to prevent this case. It is called the HTTP(S) Test Script Recorder.

HTTP(S) Test Script Recorder can be used to record all the requests. As the name suggests, it will record only the HTTP(s) requests. It also has Recording with Think Time option. This option provides to record with Think Time.

Let’s record our HTTP request according to JMeter HTTP(S) Test Script Recorder.

  1. Go to JMETER_HOME/bin and start JMeter with jmeter.bat on Windows and jmeter.sh on Linux/Unix
  2. Select Templates… on the menu bar
Select Templates... option
  • Select Recording with Think Time on the list
Templates pop-up
  • A complete Test Plan is generated. Return to HTTP(S) Test Script Recorder, enabled it and click the Start button at the top.
Enable the HTTP(s) Script Recording
Click the start button
  • When starting to run the recorder, it will display a warning message saying that it can’t start because a certificate does not exist. Click OK. The temporary certificate named ApacheJMeterTemporaryRootCA.crt has been created in the JMeter bin directory. Click OK.
  • Let's configure the browser to use the JMeter Proxy and add the JMeter certificate to the browser. Start Firefox and go to Options -> General -> Network Settings,
  • Click the Settings button.
Network Settings Tab on Firefox
  • On the new pop-up, check Manual proxy configuration. The address and port fields should be enabled now. Fill in the fields according to the picture below.
  • Now, we should add the JMeter certificate to the browser. Go to Options -> Privacy & Security -> Certificates and click the View Certificates… button.
Privacy & Security tab in Firefox
  • Click the Import… button in Certificate Manager and select the certificate from JMETER_HOME/bin/ApacheJMeterTemporaryRootCA.crt and click OK.
Certicifacete Manager
  • Now, we can go to the form URL.
  • Before filling in fields, we can name the HTTP request specifically by Recorder: Transaction Control like the picture below.
Recorder: Transaction Control
  • Fill in the fields and submit it.
  • Now we can see our HTTP Request in Thread Group.
Recorded HTTP Request

Using only HTTP(S) Test Script Recorder may not be enough for an advance test plan. JMeter Config elements should be used to control body data of requests and to ensure the longevity of the test.

In conclusion, the recommended way for advanced load testing is to first save your script via HTTP (S) Test Script Recorder and then customize your test script using configuration items. Finally, run your test plan in JMeter NON-Gui Mode.

to be continued…

--

--