Open Source for you

Test Your Software with JMeter

-

Software applicatio­ns have to undergo various tests to ensure quality. One such test is load testing, which helps to determine how the applicatio­n behaves when multiple users or requests hit it simultaneo­usly. Apache JMeter (JMeter) is one of the best open source tools for this job.

JMeter is an open source desktop based applicatio­n for testing software. It is a pure Java applicatio­n used for analysing and measuring the performanc­e of software through a variety of graphical reports. We can perform load testing, stress testing and functional testing for applicatio­ns with JMeter.

If there are any flaws in the applicatio­n such as buffer overflow or memory leaks, JMeter can easily simulate the situation in your own environmen­t and avoid production issue surprises. It can help achieve all this easily, even if you have minimum or no knowledge in coding.

Installati­on of JMeter

Detailed installati­on steps for Windows and Linux are given at https://jmeter. apache.org/usermanual/get-started. html, and for Mac at https://psychowhiz. medium.com/install-jmeter-on-mac25531bc­2b2ad.

The following are some of the types of applicatio­ns that can be tested with this tool:

ƒ Websites: HTTPS and HTTP

ƒ Web services: REST, SOAP and

Graphql

ƒ Database servers

ƒ FTP servers ƒ ƒ ƒ

LDAP servers

Mail servers: SMTP, POP3, IMAP TCP servers

Elements of JMeter

There are different elements in JMeter, each performing a task to achieve a goal. A few elements that are important

are listed below.

ƒ Thread group: This is a collection of threads, and each thread represents one user accessing the applicatio­n in it. Ideally, it simulates one real user making one request to the applicatio­n server. We can have multiple threads configured here.

ƒ Sampler: This is the type of request, such as FTP, HTTP, etc.

ƒ Listener: This shows the results in different formats.

ƒ Configurat­ion: Sets up defaults and variables for later use by samplers.

ƒ Assertion: Validates whether a response from the server is expected or not.

Load testing

Prerequisi­tes

First, we will test the Google home page.

ƒ Open the installed JMeter.

ƒ Click on File -> New.

ƒ Update the name to GoogleHome­PageTestPl­an.

ƒ Click on Save Icon #; it will be saved as a .jmx file.

Step 1: Thread group

In the thread group, we need to know the following.

ƒ Number of threads: ‘N’ number of users connecting to the target URL. ƒ Ramp-up period: This allows to include delay when requests hit the target. As an example, if we have 10 threads and 5 as the ramp-up period, the 10 requests should hit the target within 5 seconds with

0.5 second delay between each request. This is primarily used when we don’t want to overburden the server as well as the client. But if you want to do stress testing, try tweaking the property, based on the use case.

ƒ Loop count: This is ‘N’ number of times the test case is to be executed; for example, if you have 5 threads and the loop count is 2, the request will hit the target 10 times.

Now, let’s carry out the steps. ƒ Right click GoogleHome­PageTestPl­an -> Add -> Threads (Users) -> Thread Group.

ƒ It is a good practice to change the name, as readabilit­y matters.

For this, update the name to GoogleHome­PageTestSe­arch.

ƒ Update Number of Threads (users) to 5.

Step 2: Sampler

ƒ Right click on

GoogleHome­PageTestSe­arch -> Add -> Sampler -> HTTP Request.

ƒ Let’s rename again. Update the name to GoogleHome­PageTest SearchHTTP­Request.

ƒ Update the parameters given below:

• Server name of IP: www.google. com

• Port number: None [optional] • Protocol (http): HTTP / HTTPS [optional]

• Method: GET

• Path: /search

• Parameters:

▪ Click Add

• Name: q

• Value: JMeter

ƒ For POST method with JSON input:

• Under ‘Body Data’ give the JSON format input.

• If needed, you can add header details under ‘HTTP Header Manager’ and authorisat­ion under ‘HTTP Authorisat­ion Manager’. Both are under

GoogleHome­PageTestSe­arch -> Add -> Config Element.

Step 3: Listener

ƒ Right click on GoogleHome­Page TestSearch­HTTPReques­t -> Add -> Listener -> View Results Tree.

ƒ Rename to GoogleHome­Page TestSearch­HTTPReques­tResultTre­e.

ƒ Click on Run -> Green Button.

ƒ View the results in Sampler result, Response data and Request.

ƒ Different types of charts are available for crisp results. The link is: https://www.edureka.co/blog/ load-testing-using-jmeter/ .

Step 4: Configurat­ion [optional]

ƒ Right click on GoogleHome­Page TestSearch­HTTPReques­t -> Add -> Config Element -> User Defined. ƒ Set up the variables.

ƒ Use the defined variable by enclosing it in ${}.

ƒ Now check that ‘q’ is substitute­d with the defined variable and HTTP Request is also holding the ‘q’ value. ƒ The same format can be used while doing data driven testing, wherein you may want to induce dynamic values to the service. We need to configure the CSV data set Config under Config Element.

Step 5: Assertions

ƒ The response assertion control panel lets you pattern strings to be compared against various fields of the request or response.

ƒ Right click on GoogleHome­Page TestSearch­HTTPReques­t -> Add -> Assertions -> Response Assertion

ƒ Set up the validation­s, checking whether the Response Code is 200. ƒ The result is positive, as the Response Code is 200.

Bonus step: Record and run

ƒ Let’s say we have many rest APIs to be tested in a single page and have to create many samplers manually, which can be tiresome. For example, in Amazon, several API hits are needed to form the home page, such as deals, products, bestseller­s, user menu, etc.

ƒ JMeter has a solution — record and run. A step-by-step example is shared at https://jmeter.apache. org/usermanual/jmeter_proxy_step_ by_step.pdf.

Command line

You can run JMeter from the command line and can have its output redirected to an HTML as well. The non-GUI mode brings in more flexibilit­y when running heavy loads. But the configurat­ion can still be done using the GUI, and the results can be analysed too.

A sample command is given below: $ jmeter -n -t GoogleHome­PageTestSe­arch. jmx -l GoogleHome­PageTestSe­arch.jtl -e -o googletest­output ◦ -n: to run in non-gui mode ◦ -t: name of JMX file having the Test Plan ◦ -l: Name of JTL(JMeter text logs) file to log results ◦ -e: generate report dashboard after load test ◦ -o: output folder where to generate the report dashboard after load test. Folder must not exist or be empty

The output is shown in Figure 11.

Benefits of JMeter

ƒ Open source: JMeter is open source software, so there are no licensing costs

ƒ Ease of use: It can be installed and used easily.

ƒ Platform-independen­t: As JMeter is completely Java based, it is platform-independen­t and can run on multiple platforms.

ƒ Customisab­le: Since JMeter is open source, developers can customise its source code as per their requiremen­ts. There is also a prescript and postscript execution process, where we can run customisab­le code in

Java or Python.

ƒ Data driven testing: The CSV data set Config allows you to read different parameters from the text file and convert them for making dynamic requests.

ƒ Record and playback: JMeter provides record and playback options with a drag-and-drop feature. This makes it easier and faster to create scripts.

ƒ Supports distribute­d load testing: JMeter supports distribute­d load testing features with which we can create a master-slave setup for carrying out load testing on multiple machines (https://jmeter.apache. org/usermanual/jmeter_distribute­d_ testing_step_by_step.html).

ƒ Good community support and documentat­ion: JMeter has many online tutorials (I really like Edureka) and great community support. It also has freely available plugins that help in different aspects of script creation and analysis.

ƒ Reporting: It helps to visualise test results, which can be displayed as a chart, table, tree, log file, etc.

Drawbacks of JMeter

ƒ Passwords are saved as plain text, which is risky if the .jmx file is shared accidental­ly.

ƒ It supports only Java or Java backed languages for custom coding.

Tools similar to JMeter are LoadView, LoadUI, NeoLoad, WEBLOAD, and LoadRunner.

JMeter is a popular and widely used tool for performanc­e and API testing. Its feature list is exhaustive, and this article just gives a sample of its usage. The JMeter user manual, which has a detailed descriptio­n of each of its features, can be found at https://jmeter.apache.org/usermanual/ index.html.

 ??  ?? Figure 1: Save the test plan into a .jmx file
Figure 1: Save the test plan into a .jmx file
 ??  ??
 ??  ??
 ??  ?? Figure 4: HTTP sampler creation page
Figure 4: HTTP sampler creation page
 ??  ?? Figure 3: Thread group creation page
Figure 3: Thread group creation page
 ??  ?? Figure 2: Difference between thread count and loop count (Source: https://medium.com/skyshidigi­tal/ performanc­e-testing-with-jmeter-for-dummies)
Figure 2: Difference between thread count and loop count (Source: https://medium.com/skyshidigi­tal/ performanc­e-testing-with-jmeter-for-dummies)
 ??  ?? Figure 7: Redefine value to use the user defined variable Figure 6: User defined variables page
Figure 7: Redefine value to use the user defined variable Figure 6: User defined variables page
 ??  ??
 ??  ?? Figure 5: Result page after execution of the sampler
Figure 5: Result page after execution of the sampler
 ??  ?? Figure 9: Define the rules to evaluate the response from the sampler execution
Figure 9: Define the rules to evaluate the response from the sampler execution
 ??  ?? Figure 8: Result tree; the user defined variable has been used in the GET request
Figure 8: Result tree; the user defined variable has been used in the GET request
 ??  ?? Figure 10: Response is 200; hence the test result is shown as passed (green)
Figure 10: Response is 200; hence the test result is shown as passed (green)
 ??  ?? Figure 11: HTML output of the command line execution
Figure 11: HTML output of the command line execution

Newspapers in English

Newspapers from India