Wednesday, August 22, 2012

Load Testing of GUI using JMeter

This is a post I found in the net which is very helpful, I tried and it is working like a charm. Therefore, I would like to repost it here. Source: Death By Code's Blog

What is JMeter?

JMeter is a Java Based Open Source application which may be used for load/traffic testing of Web Applications.

Quote from JMeter home page:

Apache JMeter may be used to test performance both on static and dynamic resources (files, Servlets, Perl scripts, Java objects, Databases and Queries, FTP Servers and more). It can be used to simulate a heavy load on a server, network or object to test its strength or to analyze overall performance under different load types. You can use it to make a graphical analysis of performance or to test your server/script/object behaviour under heavy concurrent load. 

What am I trying to do with JMeter?

I wanted to use JMeter to do load testing of a GUI. I wanted to record some regular transaction done in GUI, like login, browse to different pages, display data (i.e. search data from the DB), modify data and finally log out. Once this test case is recorded, then simulate a scenario where multiple users would do the same set of transactions simultaneously. My target was to find out the maximum number possible simultaneous sessions in my GUI.

How does JMeter do this?

1. Initially JMeter needs to be configured to sit between the browser and the target server (on which GUI is running) and then it records all the HTTP requests sent by the browser to the server. If the target GUI accepts only HTTPS requests, then at this phase the browser should sent HTTP requests (and NOT HTTPS) to the JMeter, JMeter records those requests and finally encrypt and send those to the server. Reverse things happens for HTTPS response - JMeter receives the responses from the server, decrypts those, records and sends the HTTP responses back to the browser. The logical entity that is responsible for this HTTP message recording is called Http Proxy manager (described later).

2. Once the recording is done, JMeter can be configured to start a large number of simultaneous threads - each thread represents one user sending/receiving the same set of requests/responses recorded in the first step. Now onwards, it's between the JMeter and the server; browser is no longer used. Not surprisingly, JMeter maintains separate Http sessions for each user. The component responsible for this is called thread group.

3. The result can be checked by using something called "listeners" - there are different flavors for the same.

How to use JMeter?

1. Download JMeter from here.

2. In case the server accessed and the desktop on which the JMeter is launched reside in the same network run jmeter.bat from $JMETER_DIR/bin or just type "jmeter" in the command prompt.

3. If a proxy sits in between the JMeter and the server, issue the following command from the command prompt:
jmeter -H <my.proxy.server> -P <port> -u <username> -a <password>

4. Add a Thread Group to the Test Plan by right clicking on Test Plan and Add -> Thread Group. Number of threads indicates the number of users to be simulated. Ramp up period is the time over which all the sessions will start uniformly one by one.

5. Add the HTTP Cookie Manager to enable session tracking by right clicking anywhere in the Test Plan hierarchy. Add -> Config Element -> HTTP Cookie Manager. Set the cookie policy to compatibility.

6. Select the thread group. Right click "Add -> Config Element -> Http Request Defaults". Set the server name or IP to server's IP. Keep pot number/path empty. Set protocol to HTTP.

7. Add a Recording Controller to the Thread Group by right-clicking on Thread Group -> Add -> Logic Controller -> Recording Controller. Rename the Recording Controller to take a suitable value. All the Http Requests recorded would be saved under this recording controller.

8. Select workbench. Right click on workbench and add the Http proxy server: Add -> Non-Test Elements -> Http Proxy Server. Port field - Enter "9090" (Any other value would do). Go to Target Controller - click on the drop down and select the recording controller created in step 6. As mentioned earlier, Http Proxy Server is the element which would record the Http requests and 9090 is the port on which the JMeter recording controller would run.

9. In the same window, click on the Attempt Http Spoofing. It ensures that while recording the test case, JMeter encrypts the HTTP request before sending to the server and decrypts the HTTP response after receiving it back from the same.

10. Add the following to the URL patterns to exclude:

  • .*\.png
  • .*\.gif
  • .*\.jpg
  • .*\.php
  • .*\.jsp
  • .*\.html
  • .*\.htm
  • .*\.js
  • .*\.css
11. Add Gaussian Random Timer under Http Proxy Server. Right click on the Http Proxy Server, add -> Timer -> Gaussian Random Timer. Put the following values:
  • Deviation (in milliseconds) -> 500.0
  • Constant Delay Offset (in milliseconds) -> {$T}
Gaussian random timer should be used if you want JMeter to record the time taken by human user between clicking different links across the GUI. While executing the test case, the HTTP requests would be sent one after another maintaining the same time gap (as taken by the user). If Gaussian random timer is not used, all the requests would be pumped to the server at once. I am sure that no body wants to test such an unrealistic scenario. 

12. Add Listeners to the Thread Group by right clicking on Thread Group -> Add -> Listener -> View Results in Table & View Results Tree & Aggregate Report. Different Listeners would represent the result (request sent, response received, time stamp, success/failure rate etc) in different form helping you to analyze.

13. Go back to the Http Proxy Server. Now we are ready to record the test cases. So, click on Start button. 

14. Open the browser and configure proxy settings to point to JMeter's proxy server. For Internet Explorer, click on Tools and select Internet Options, click on the Connections tab, click on LAN Settings, check the 'Use proxy server for LAN...' and set the address to 'localhost' and port to '9090' (i.e. the port at which JMeter is configured to run). Now the browser will send the requests to the JMeter which in turn will send it to the server. JMeter will record them in between. 

15. Go to I.E. In the address bar type the following:

http://<Test Machine's IP>/<Link>/

Note, even if the server supports only secured connection, we are not using "https" here. Since, JMeter has been configured to do https spoofing, browser will send normal http request to JMeter and JMeter will do further encryption before sending the requests to the server. 

16. Login to the GUI normally. And do some operations. Navigate through, perform actions etc as per your use case. 

17. Click on the Stop button from the Proxy Server once the recording is finished. All the requests will be stored under the recording controller. If the HTTP requests are not visible there, you must have missed something in the previous steps. Have a look. Fix it and then come back.

18. Finally, run the test case by hitting Ctrl+R. A green box on the upper right corner should be visible indicating the test is running. The figure beside that green field indicates the number of threads (simulated users running at any instance of time). Different listeners will give detailed information about the test case while it is running. The box will turn gray once the execution stops. Go back to the listeners to find the results. 

Further reading:

No comments:

Post a Comment