12 February 2012

JMeter: Load testing a Java Web Application which requires multiple User Login's

Background: What JMeter is and what problem it solved for me

Jmeter is an open-source load testing tool from Apache which can be used to load test Web Applications amongst other things (Web Services, Databases etc). This blog entry gives an overview of what I learnt from my first experience of using JMeter and is designed to be read by anyone trying to test a Web Application that requires a log in with multiple different users.

The problem
One of my clients Web Application's was having performance issues around a piece of functionality running a sysbase stored procedure which caused a high number of locks under heavy load. To re-create this scenario JMeter needed to log into the application as 10 different users, go through a short web flow (which included filling in a form), this was then repeated (forever over a period of 2 hours). So if you need something similar hopefully this will help. If you just want a basic static web flow go to the Jmeter web starter tutorial.

Creating the basic Test Plan fundamentals

Setting up a Thread Group to represent your users
The first thing you'll need to do is create a thread group to represent the users - here you define the number of users, repetitions and delay time between each user. Then add a HTTP Request Defaults (which will contain shared default data for your http requests) and a HTTP Cookie Manager to take care of cookie handling to this thread group. Next you'll want to see some output from your load test so add a Graph Results listener to your user group to see a simple graphical representation of load time and also add a Tree Results listener which will list all the request/response data (very handy for debugging).


Creating a thread group: this group has 10 users with a second pause in between each user looping continuously for 2 hours


Making the HTTP requests
Set up your Http defaults to contain your web server/host (any ports), also set the protocol to default to https (if needed) and To create a basic HTTP GET request add a HTTP Request Sampler to your thread group and enter your relative path e.g. / for homepage (this assumes that you have your host/domain entered in your http request defaults) and that's it. Run the test and look in the tree results to ensure you are getting a valid 200 response back. You can add extra Http Headers to the request (e.g. Refer or User-Agent) by right-clicking on your http request Sample and adding a Http Headers Manager - here you just add key-value pairs for the headers.



To make a POST request simply set the Method drop down to POST in your Http Request Sampler. For your POST to work correctly for a form set the path to the target of the form and add the form field values as key-value pairs (by clicking on add under the ' send parameters with request' section) ensuring the field name matches that in the html (also include any hidden fields). Ensure that the 'Use multipart/form-data for POST' is ticked. Again when you run this the tree results are very handy to check that all the correct data is being sent in the request.

Making a HTTP Post Request: this request logs into a web application using data in a CSV file


Making the POST request data unique for each user (thread) allowing multiple different logins
You can use placeholders in your parameter value as opposed to a hardcoded value these placeholders are then replaced with a value found in a row within a given CVS file. To do this create a common delimited CSV file for example for log in details:

username1,password1
username2,password2

Then add a CSV Data Set Config to your Http Request Sampler, specify your filename and columns (Variable Names) e.g. username,password. You then put the variable name placeholder in your post param data e.g. $(username) $(password). For a more detailed explanation go here xxx.

Increasing the load
Once everything’s in place simply tweak the loop values in your User Group settings or increase the number of values, you can even schedule the job to run.

Running behind a proxy?
If you need to run Jmeter behind a proxy then simply start Jmeter with the following parameters  like so;  jmeter -H proxy.rta.nsw.gov.au -P 8888 -u username -a password -N localhost 

Conclusion
JMeter was quick and easy to set up as a developer and can be a very useful tool to try and re-create a performance issue/load problems when used along side a Java profiler such as visual vm.

8 comments:

  1. veyr helpful, thanks!

    ReplyDelete
  2. Hi,
    Have you tried using Selenium Webdriver for Java with JMeter for load testing? I am trying to find someone who did that successfully. Thank you.
    Maria

    ReplyDelete
    Replies
    1. Hi Maria,

      I have only used the Firefox Selenium plugin and have not worked with the new Java Webdriver API for Unit Testing.

      Reading up on the topic I found a blog post where the author has made a bold claim that '...it's not complicated to integrate Selenium with JMeter', hopefully it will help http://www.anzaan.com/2012/07/integrating-selenium-with-jmeter-for-application-load-testing/.

      It would be good to know if you get this working and what you manage to achieve with it.

      Cheers,
      Phil

      Delete
  3. Hi,

    would you have more specific instructions to create the CSV file and how to use it.

    -Londo

    ReplyDelete
  4. I know its a little late but If "Anonymous 2" is still looking for an answer I would check out PushToTest. It's simple, affordable and effective.

    www.pushtotest

    ReplyDelete
  5. Thanks for article.
    Another way to set values externally is to use user.properties file, where we can set jmeter properties in the following format:

    number_of_users=100

    and then pass them to the test scenario as ${__property(number_of_users)}
    This will allow to remove CSV DataSet Config from testplan to make it more simple.

    Look through our blog about Jmeter, I hope, it will be usefull.

    ReplyDelete
  6. Thank you for sharing. Load testing services is a highly specialized field of testing.

    ReplyDelete