Sunday, May 12, 2013

Load and Performance testing of WCF Service

In my previous post I described the steps to test the WCF service. In this article I shall try to describe some simple steps for load testing of a WCF service in VS 2010.

As we have the web service with single GetData method. And added code for wait window, to sleep the thread for random time between 1 to 2 seconds.

Now follow these steps for load testing of WCF service

Step 1: Create at Test Project 

Choose the following options to create a test project from the template.

File > New Project> Test Template> Test Project

Step 2: Add the service reference from the project:

Step 3: Add the following code in the test class

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MyRestfulServiceTestProject.Posts;

namespace MyRestfulServiceTestProject
    public class WcfLoadTest

        private static IList<string> _valuesList=new List<string>();

        public  static  void Initilization(TestContext context)


        /// <summary>
        /// To get the random value from the list of values
        /// </summary>
        /// <returns></returns>
        private string GetNextValue()
            var rand = new Random();
            lock (_valuesList)
                return _valuesList[rand.Next(_valuesList.Count)];

        public void UnitTestMethodForGetData()
            //To get the randon value as input from the value collection
            var value = GetNextValue();

            //Created a service client
            var serviceClient=new PostClient();

            // call the service method getdata
            var response = serviceClient.GetData(value);


Step 4: Once the code is done and compiled Add a Load Test template in your test project

Right click on the project and select the “Load Test…” option from the templates as shown below

A wizard screen will open

Configure the following from the wizard options

i. Click Next from the welcome screen
ii. Enter the scenario name and click next 

iii. This screen it to configure the load of 20, 50 or 100 users whatever you want. I am selecting the load of 20 users as shown below

Either you can select the user count or you can choose for Step Load.
In Step Load you have different options
Start User Count: Users to hit the service at very first time
Step Duration: Duration between the second batches of call
Step User Count: To set the increment of the users in next batch
Start User Count: What can be maximum number of user at a time after increasing user counts from “Step User Count”

iv. In step for “Test Mix Model” you will see various options, as shown in screen shot below, which actually let you decide how you want to run the test

I am going to use “Based on the total number of tests”

v. The next screen “Test Mix” allow to configure the test method(s) which you want to run

AS shown in the screen above we can see Add button. On the click of Add button we will have another window which will show the method created in test class (Step 2)
Similarly, we can add multiple test method if we have.

vii. On the next screen you will have the option to select whether you are in local environment, on mobile app etc. This is to choose the connectivity.

In my case I am using LAN to perform test in my local environment.
viii. Next screen will be used to define the applications to monitor during the test run
Click Add Computer and select the application you want to monitor

ix. And the last screen to decide how long you want test to run.
Here you can simply define
Warm up duration: This duration is to set the application and load the all-state data, server connectivity etc. which takes long time for very first few requests.

Now click Finish and your load test setup is ready.
Step 5: Once the Load test setup is ready you will see the following screen

Any time you want to change the setting you can change it righting clicking on the load test template.

Step 6: Run the test

Now finally you can run the test as shown in screen shot below

You will see the various charts as result

Happy testing… :)

No comments: