This chapter explains advanced JMeter features like using JDBC, FTP, REST/JSON, AJAX, mobile web applications, and SOAP-XML performance testing, which will help you achieve specialized performance testing needs.
At the end of this chapter, you will have a good idea of a few advanced JMeter features and should be able to use these features to develop test scripts. Those who are already familiar with these features can proceed to the next chapter.
JDBC Performance Testing
This section covers a way to use JMeter to generate load and do the performance testing of a database running on MySQL.
Install MySQL
MySQL is a leading open source database server. Download and install the community server edition.1
Follow the instructions on the web site to start the server.2 Create a user and note the username and password. It is good to set the in folder in the standard path variable to access it from any directory.
C:mysql-5.6.36-winx64in>msysqld
The previous command will start the mysql server. Use the following command to log in as the root.
C:mysql-5.6.36-win64in>mysql -u root
mysql> create user "jmeter_user"@"localhost" identified by "mypass";
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on employees.* to "jmeter_user"@"localhost";
Query OK, 0 rows affected (0.05 sec)
mysql>
For this example, we will use Employees DB, which is a database that contains a large amount of data (approximately 160MB) spread over six separate tables and consisting of 4 million records in total. The documentation for this database is available on the MySQL web site.3
Download a prepackaged archive of the data from LaunchPad.4
Unzip in the employee_db folder.
To set up Employees DB, open the command prompt and enter the following:
C:employees_db>mysql -u jmeter_user -p -t < employees.sql
Enter password: ******
Here is the output:
+-----------------------------+
| INFO |
+-----------------------------+
| CREATING DATABASE STRUCTURE |
+-----------------------------+
+------------------------+
| INFO |
+------------------------+
| storage engine: InnoDB |
+------------------------+
+---------------------+
| INFO |
+---------------------+
| LOADING departments |
+---------------------+
+-------------------+
| INFO |
+-------------------+
| LOADING employees |
+-------------------+
+------------------+
| INFO |
+------------------+
| LOADING dept_emp |
+------------------+
+----------------------+
| INFO |
+----------------------+
| LOADING dept_manager |
+----------------------+
+----------------+
| INFO |
+----------------+
| LOADING titles |
+----------------+
+------------------+
| INFO |
+------------------+
| LOADING salaries |
+------------------+
C:employees_db>
Use the console to verify that the data loaded correctly.
C:>mysql -u root;
Mysql> use employees;
mysql> select count(*) from employees;
+----------+
| count(*) |
+----------+
| 300024 |
+----------+
1 row in set (0.16 sec)
If you see this previous output, you have set up the database correctly.
The schema of the database is shown in Figure 10-1.
Figure 10-1. Employee schema
Install JDBC Driver
Download the MySQL JDBC connector.5
Unzip and copy the driver to the lib folder of JMeter.
JDBC Test Plan
The JDBC test plan is easy; you’ll see by following an example.
Follow these steps or download JDBCTestPlan.jmx.6
Create a test plan and give it a meaningful name, such as JDBC Test Plan.
Click on Test Plan and go to Edit ➤ Add ➤ Thread (Users). Add Thread Group. Configure Number of Threads ( Users ) as 1 and Loop Count as 1000.
Click on Thread Group and go to Edit ➤ Add ➤ Configuration Element to add JDBC Connection Configuration. Configure Variable Name Bound to Pool, Variable Name as jdbcConfig, Database Connection Configuration, Database URL as jdbc:mysql://localhost:3306/employees, JDBC Driver class as com.mysql.jdbc.Driver, Username as jmeter_user, and Password as created previously. Choose the defaults for the other parameters (see Figure 10-2).
Figure 10-2. JDBC configuration
Click on Thread Group and go to Edit ➤ Add ➤ Samplers to add JDBC Request. Configure it with the Variable Name Bound to the Pool option set to jdbcConfig (this is the same value as configured in the JDBC Connection Configuration). Configure Query as select count (*) from employee where first_name like 'Ge%' (see Figure 10-3).
Figure 10-3. JDBC request sampler
Run the test .
Results will be similar to those shown in the following figures.
Check the sampler results of the JDBC request (see Figure 10-4).
Figure 10-4. JDBC request results
Check the JDBC request that JMeter sent to MySQL (see Figure 10-5).
Figure 10-5. JDBC request SQL
Check the response data for the JDBC request (see Figure 10-6).
Figure 10-6. JDBC SQL Response
Look at the performance results as well.
Figure 10-7 shows the result of the test performed on the MySQL database.
Figure 10-7. JDBC graph results
FTP Performance Testing
File Transfer Protocol (FTP) is a standard network protocol used to transfer files between computers. FTP follows a client-server model. The FTP client initiates a network connection (TCP) to the server. FTP uses separate control and data connections between the client and the server. FTP users authenticate themselves with a username and password, which is exchanged in clear text. Sometimes, the FTP server is configured to allow the client to connect anonymously without a password.
Tip
FTP is commonly available, but it is considered insecure. Use SFTP instead.
Follow these steps to set up the FTP if it’s not set up already.
Click on the Control Panel, then choose Programs and Features ➤ Turn Windows features on or off (left side). Select the Internet Information Service and select FTP Server, Web Management Tools, and World Wide Web Services. Click OK.
Click on the Control Panel, then choose ➤ Administrative Tools ➤ Internet Information Services (IIS) Manager. Right-click on the sites and then choose Add FTP Site.
Fill in the details. You are done setting up the FTP server locally.
Open your browser and launch ftp://<your_ip_address > enter user/password. You can see the local mapped folder files on the browser.
If you are MacOS user, to start the FTP server, enter the following command in the terminal window.
$ sudo -s launchctl load -w /System/Library/LaunchDaemons/ ftp.plist
Note
Be sure to shut down your FTP server after use for security reasons.
Let’s look at an example to illustrate the performance testing of a FTP server. In this example, we will use JMeter to connect to the FTP server and transfer a file from the server. The file is an image named cormorant.jpg, so the transfer should use binary mode.
Follow these steps or download FTPTestPlan.jmx.7
Create a test plan and give it a meaningful name, such as FTP Test Plan.
Click on Test Plan and go to Edit ➤ Add ➤ Threads (users). Add Thread Group. Configure Number of Threads (Users) as 1 and Loop Count as 5000.
Click on Thread Group and go to Edit ➤ Add ➤ Config Element ➤ FTP Request Defaults. Add FTP Request Defaults. Configure the Server Name or IP as localhost (see Figure 10-8).
Figure 10-8. FTP request defaults
Click on Thread Group and go to Edit ➤ Add ➤ Sampler. Add FTP Request. Configure the username and password. Set the value of the Remote File as /ftp.jpg and Local File as C: empftp.jpg. Since this is an image file, enable the Use Binary mode checkbox (see Figure 10-9).
Figure 10-9. FTP request sampler
Click on Thread Group and go to Edit ➤ Add ➤ Listener. Add Graph Result.
Run the test .
Results will be similar to those shown in Figure 10-10.
Figure 10-10. FTP request results
REST/JSON Performance Testing
REST stands for REpresentational State Transfer . It is not a specification or a protocol, but rather an architectural style. Through the use of REST architectural constraints, you can achieve performance, scalability, simplicity, modifiability, visibility, portability, and reliability.
Roy Fielding introduced REST in 2000 in his doctoral dissertation at UC Irvine.8 He used REST to design HTTP 1.1 and Uniform Resource Identifiers (URI) .
RESTful systems typically communicate over Hypertext Transfer Protocol (HTTP) with the HTTP verbs GET, POST, PUT, and DELETE. These verbs apply to web resources that are identified by Uniform Resource Identifiers (URIs) .
Let’s define the REST API for the entity called Book. Assume that it has the following attributes:
title: Title of the book
author: Book’s primary author
countryOfPublication: Country in which the book was published
publishedBy: Book publishing company
Now let’s look at the REST API for Create, Read, Update, and Delete (CRUD) operations. The API uses the HTTP verbs:
POST: Creates the book with a JSON payload
GET: Reads the book given the ID
PUT: Updates the book with a JSON payload
DELETE: Deletes the book given the ID
Let’s illustrate the Create operation through an example.
Follow these steps or download RESTJSONTestPlan.jmx.9
Create a test plan and give it a meaningful name, such as REST Create Book Test Plan.
Click on Test Plan and go to Edit ➤ Add ➤ Threads (Users). Add Thread Group.
Click on Thread Group and go to Edit ➤ Add ➤ Configuration Element. Add HTTP Header Manager. Configure the Headers Stored in the Header Manager, Name as Content-Type and Value as application/json. Add another header with Name set to Accept and Value set to application/json (see Figure 10-11).
Figure 10-11. HTTP header manager
Click on Thread Group and go to Edit ➤ Add ➤ Sampler. Add HTTP Request Sampler. Configure Server Name or IP as localhost, Port Number as 8080, Implementation as HttpClient4, Method as POST, and Path as /books. Click on the body data and use the following JSON payload . It has required attributes for the Create operation. The ID has not been specified and it will be assigned when the book gets created (see Figure 10-12).
Figure 10-12. HTTP request
{
"title": "The Zen Book",
"countryOfPublication": "Japan",
"publishedBy": "Good Books Inc.",
"author": "Kobayashi"
}
Click on Thread Group and go to Edit ➤ Add ➤ Listener to add View Results Tree .
Run the test.
Results will be similar to those shown in Figure 10-13.
Figure 10-13. REST JSON response
Observe that the response has an ID attribute that was assigned by the server .
AJAX Performance Testing
AJAX stands for Asynchronous JavaScript and XML . AJAX’s most important characteristic is its asynchronous nature of communication with the server. This lets you update portions of a page instead of having to refresh the entire web page. It can send/receive information in a variety of formats, including JSON, XML, HTML, and even text files.
Unlike the browser, JMeter does not execute JavaScript or render HTML. Testing AJAX is similar to any other web test. The key is to find all the AJAX requests and their expected responses. You can use the help of these tools/utilities/plugins:
We use Chrome developer tools to illustrate. You can open the DevTools one of the following ways:
Select the Chrome menu at the top-right of your browser window, and then select Tools ➤ Developer Tools.
Right-click on any page element and select Inspect Element.
Press Ctrl+Shift+I (or Cmd+Opt+I on the Mac) to open DevTools.
The DevTools window will open at the bottom of your Chrome browser . Select the Network tab. Further, refine the selection by choosing only AJAX (XHR) requests. The AJAX request URLs are displayed on the left panel. When you click on a specific AJAX request URL, its request and response details are shown in the panel to the right.
Now you have the AJAX requests and responses to model your JMeter test, as shown in Figure 10-14.
Figure 10-14. Chrome DevTools showing the AJAX requests
Mobile Performance Testing
JMeter can be used to do the performance testing of web applications running on mobile devices.
Simulating Mobile Devices
When a browser, including the browser on a mobile device, interacts with the server, it includes a header called the User-Agent. So to pretend that the HTTP request originated on a mobile device/browser, you have to insert the same header value in your request.
You can find a list of User-Agent strings to use for every mobile device by searching the Internet.13 You can then use the JMeter HTTP Header Manager to send the appropriate User- Agent string.
The following example uses a User-Agent string of Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25, which corresponds to a Safari browser running on an iPad (see Figure 10-15).
Figure 10-15. Header manager to set user agent
Simulating Network Speed
The connection speeds of mobile devices are often slow. It is easy to simulate slow connections.
The network speeds are measured using characters per second (cps), which can be calculated using this formula: cps = (target bandwidth in kbps * 1024) / 8
Select your device from the list in Table 10-1.14
Table 10-1. Device Bandwidth
Network | Bandwidth | CPS Value |
---|---|---|
Mobile data GPRS | 171 kbit/s | 21888 |
Mobile data EDGE | 384 kbit/s | 49152 |
Mobile data HSPA | 14.4 Mbp/s | 1843200 |
Mobile data HSPA+ | 21 Mbp/s | 2688000 |
Mobile data DC-HSPA+ | 42 Mbps | 5376000 |
Mobile data LTE | 150 Mbp/s | 19200000 |
WIFI 802.11a/g | 54 Mbit/s | 6912000 |
WIFI 802.11n | 600 Mbit/s | 76800000 |
Ethernet LAN | 10 Mbit/s | 1280000 |
Fast Ethernet | 100 Mbit/s | 12800000 |
Gigabit Ethernet | 1 Gbit/s | 128000000 |
10 Gigabit Ethernet | 10 Gbit/s | 1280000000 |
100 Gigabit Ethernet | 100 Gbit/s | 12800000000 |
WAN modems V.92 modems | 56 kbit/s | 7168 |
ADSL | 8 Mbit/s | 1024000 |
ADSL2 | 12 Mbit/s | 1536000 |
ADSL2+ | 24 Mbit/s | 3072000 |
The JMeter property file jmeter.properties has these two properties that you can configure.
httpclient.socket.http.cps=0
httpclient.socket.https.cps=0
Specify these values in the JMETER_HOME/bin/user.properties file.
JMeter to Record User Actions
Earlier in the book, we explored how to use JMeter to record a test script. You could use the same technique to record tests for mobile devices. The only difference is that you configure the mobile device to use JMeter as a proxy.
Android Proxy Configuration
Go to Settings ➤ Wi-Fi.
Long tap on the connected network and click the Modify Network option.
From the dialog box, check the Advanced Options checkbox.
This will open advanced settings from which you can modify the proxy manually. For this, set the Proxy option to Manual.
Now set the Proxy hostname to your computer’s IP address and set the Proxy port to 8080 (see Figure 10-16).
Figure 10-16. Android proxy settings
Click on the Save option. Run the application on your mobile device. Its requests will be recorded in JMeter .
iOS Proxy Configuration
Go to Settings ➤ Wi-Fi.
Click on your connected network.
Select the Manual option from the HTTP Proxy section.
Set the Server value to your computer’s IP address and the Port value to 8080.
Click on the Save option. Run the application on your mobile device. Its requests will be recorded in Jmeter, as shown in Figure 10-17.
Figure 10-17. IOS proxy settings
SOAP Performance Testing
SOAP stands for Simple Object Access Protocol . This is a specification for exchanging structured information in the XML message format and relies on application layer protocols, most notably Hypertext Transfer Protocol (HTTP) for message negotiation and transmission. SMTP and JMS are other possible transport mechanisms.
SOAP was the desired mechanism to implement the Service Oriented Architecture (SOA) . This has largely been replaced with REST.
W3C has a SOAP specification.15
Install SOAPUI
SoapUI is a free and open source functional testing tool with very good support for SOAP. Download the SoapUI package and then follow these steps to create a new project.16
Go to File ➤ New to create a new project. Choose the Description File and specify the value as Initial WSDL and URL as http://localhost:8080/services/CurrencyService?WSDL. Enable the Create Sample Requests for All Operations checkbox to automatically generate sample SOAP requests (see Figure 10-18).
Figure 10-18. New SOAP project
Double-click on Request1 to open the request template on the pane to the right (see Figure 10-19).
Figure 10-19. SOAP request menu
Fill in the XML arguments. Set arg0 to USD and arg1 to JPY (see Figure 10-20).
Figure 10-20. SOAP request
Click the green arrow to submit. You should see the response shown in Figure 10-21.
Figure 10-21. SOAP response
The WSDL for the Currency SOAP service is located at:
http://localhost:8080/services/CurrencyService?WSDL
You can view the WSDL using a browser (see Figure 10-22).
Figure 10-22. WSDL for the Currency SOAP service
Let’s see how to test SOAP using JMeter with an example.
Follow these steps or download SOAPTestPlan.jmx.17
Create a test plan and give it a meaningful name, such as SOAP Test Plan.
Click on Test Plan and go to Edit ➤ Add ➤ Threads (Users). Add Thread Group . Configure Number of Threads ( Users ) as 1 and Loop Count as 1000.
Click on Thread Group and go to Edit ➤ Add ➤ Configuration Element to add HTTP Header Manager. Configure Headers Stored in the Header Manager, Name as Content-Type and Value as text/xml (see Figure 10-23).
Figure 10-23. HTTP header manager
Click on Thread Group and go to Edit ➤ Add ➤ Sampler. Add HTTP Request. Configure Server Name or IP as localhost, Port Number as 8080, and Path as /services/CurrencyService. Copy the SOAP Request XML, which was generated by the SoapUI earlier, into the body data area (see Figure 10-24).
Figure 10-24. SOAP XML-HTTP request
Click on Thread Group and go to Edit ➤ Add ➤ Listener. Add View Results Tree .
Run the test.
The results are shown in Figure 10-25.
Figure 10-25. SOAP response XML format
Conclusion
In this chapter, we learned about the advanced features of JMeter. We learned about JDBC, FTP, and REST/JSON testing using their specific samplers and SOAP performance testing using the HTTP Request Sampler. We also learned about AJAX request performance testing as well as mobile web application performance testing. In the next chapter, we will go through a case study where we will be learning to apply JMeter skills that we learned in this and previous chapters to deal with a real-life performance testing project.
Footnotes
14 Credit: Antonio Gomes Rodrigues