When modeling a Web Application’s responsiveness with an increasing User Load, you usually configure a Load Test Mix with a Step based Load Pattern, run the test until the Response Time goes above a certain response time, and then manually stop the test. This requires some of your time monitoring the test progress and having to intervene to stop the test.

 image

Wouldn’t be nice if this was automated? Fortunately this can be easily achieved through the use of a custom Load Test Plugin (ILoadTestPlugin) and a configured Threshold. (Custom load test plugins were improved in VS2008SP1 and are also available in VS 2010, used in the creation of this blog post.) This will save you (the Tester) the trouble of having to manually monitor the load test and stop it when the threshold is exceeded.
image

Let’s take a look at how this can be implemented in code:

Creating a Load Test Plug-in that stops the Load Test when a threshold is exceeded

Load Test Plug-ins allow custom code to be executed when a load test starts, ends, or when a threshold was exceeded, among other events. We’ll create a Custom Load Test Plug-in that attaches itself to the ThresholdExceeded event and stops the test.

  1. Create a Class Library project.
  2. Add a reference to the Microsoft.VisualStudio.QualityTools.LoadTestFramework assembly.
  3. Create a class named CustomLoadTestPlugin and complete it with the following code (VS2008 and VS2010):

    using System;
    using Microsoft.VisualStudio.TestTools.LoadTesting;
    
    public class CustomLoadTestPlugin : ILoadTestPlugin
    {
        private LoadTest test;
    
        public void Initialize(LoadTest loadTest)
        {
            if (loadTest == null)
            {
                throw new ArgumentNullException("loadTest");
            }
    
            this.test = loadTest;
    
            loadTest.ThresholdExceeded += new EventHandler<ThresholdExceededEventArgs>(OnThresholdExceeded);
        }
    
        void OnThresholdExceeded(object sender, ThresholdExceededEventArgs e)
        {
            if (e.ThresholdResult == ThresholdRuleResult.Critical
                && e.CounterValue != null
                && e.CounterValue.CounterName == "Avg. Test Time")
            {
                this.test.Scenarios[0].CurrentLoad = 0;
                this.test.Abort();
            }
        }
    }

  4. Build the Load Test Plug-in solution

Note: If you’d like to get more information about creating a Load Test Plug-in, see the following link.

Using the Custom Load Test Plug-in

  1. Open the Load Test Configuration (.loadtest) you’d like to modify.
  2. Using Solution Explorer, add a reference to the Load Test Plug-In (either to the Load Test Plug-In project or to the compiled assembly).
  3. Add a Compare Constant threshold rule on the Avg. Test Time counter. To do this, Expand the Counter Sets | LoadTest | Counter Categories | LoadTest:Test element, right click on the Avg. Test Time counter and select Add Threshold Rule.image
  4. Add the Load Test Plug-In to the test run. To do this, right click on the Load Test Name (the top element) and choose Add Load Test Plug-in. On the window that opens, select CustomLoadTestPlugin and hit OK.

    image

  5. That’s it, you should now run the Load Test and see if it stops after your configured threshold.

Hope this saves you time. If you’d like to learn more ways to extend Visual Studio Load Testing, check Bill Barnett’s  article.

Related Articles

Happy testing!

kick it on DotNetKicks.com

  • http://dobrzanski.net Jarosław Dobrzański

    Great post! All I needed in one place!