Saturday, November 29, 2014

Performance & Load testing - Part 1

Performance & Load testing a WCF service using Visual Studio (Part 1)

In this blog, we will be discussing how we can performance & load test WCF service using Visual Studio. 

Performance of any service is a critical factor and if a service is not responding within specified time then the response is as good as none. Hence it is absolutely imperative to not only make sure the service is responding but responding quickly.

There are couple of ways to performance test a service. Since we work on Visual Studio for development, that is the most handy and available tool for this. Below are the pre-requisites:-
  1. Visual Studio Ultimate
  2. One service endpoint to test
  3. Credentials if the service is secured via some security
So let's start, we will achieve this in two parts. First, we are going to write a web test which will call our endpoint. Second, we will use this test in our load test to add the desired load we want for our performance test.

1. Writing a web performance test:-

Open your WCF project in Visual Studio and right click on the solution and click add new project. On new project dialogue, click on test on the left hand side and select web performance and Load Test project. Please note, this option will only be available in Visual Studio Ultimate edition. 




Once the project is created, right click and add new web performance test. As soon as you click that, the browser will open for recording and just click Stop Recording. Now you have an empty web test. Right click on the webtest and click add Web Service Request. This will add a service call in your webtest with http://localhost/ url. Right click on the web service test and click properties and in the properties window, update the URL of the test with the one you wish to test.



Now the url is pointing to your service, it is time to add the payload. I will give you a tip here, if you are dealing with services, I would recommend to use SoapUI for testing them. It is a fantastic tool to add your service and it auto creates the request object for you as well which you can fill and test the service. 

I do have my service configured in SoapUI and I am going to use the same xml in request. If you are not using SoapUI then I would suggest to manually generate the xml request and add to the test. For this, right click on String body under web service test and click properties. Under String body property, add the request xml. Mine is shown below:-



Everything else in above xml is usual apart from Authorization header. My service is secured and the only way to successfully call it is either passing a basic Auth or SAML token in the request header. I am using Basic Auth above.

Now since the test is now loaded with request xml as well there is only one thing left to do. Specify the method of the endpoint. You might have noticed, we haven't mentioned which method of the endpoint to call. This information will go in header. Right click on the web service test and click add header. This will add a new header in the test.
Right click on the header and click properties. In Name add "SOAPAction" and in value add the fully qualified method name. 



This is important as without the SOAPAction header, your request will be lost. Now your test is ready to run. Right click on webtest1 and select Run Test. This will run you test and display the results.  

You can run this test as many number of times you want and it will give you the same results. Same results??? Yes, because currently our request is only invoked with the same request parameter. Check the username in the request screen shot above, "TestUser1". This is a hard coded value and will always be the same. Obviously when we wish to run our performance test, we would prefer to use different values in our requests so as to cover all testing scenarios and for this we would have to parametrize the request xml. 

Parametrization of web test:-

In order to parametrize a web test, you need to add a data source to it. Data source could be of three formats. Database, CSV file or an XML file. For our demonstration, I am going to use the simplest one. A CSV file.

First, let's create a file with our data. Create a CSV file on your desktop and in the first row, put the names of your parameters separated by comma and from the second row, the value of those parameters again separated by comma. Since in my request there is only one parameter, the content of my CSV file are given below:-

Usernames
TestUser1
TestUser2
TestUser3

Usernames is the name of the column and rest three are the values which my test will be using while running.

Now, let's add this CSV file as a data source in our web test. Right click on web test and click add data source. A dialogue will open showing three options of data source. Click on the CSV file and click next, now select the CSV file you just created. If the format of your CSV file is correct then the dialogue will display the contents of the CSV file. Press Finish.

The CSV file is now added in your web test as data source. All you have to do is to remove the hard coded value in the request and use it from data source. Below is the parametrized version of our request. 




Naming the parameter is simple. Name of your data source then name of your CSV file and name of the column to pick the value from. Now save the test and run again. You should see the results of the test using the new parametrized values.

This is it, we have successfully created a web test for our service and parametrized the request as well. In the next part, we will add this test in our load test configuration and will add load as per our requirements.

Stay tuned !!!

No comments :

Post a Comment