pnp.gif

How To: Create a Load Test Plug-In using Visual Studio Team System

J. D. Meier, Prashant Bansode, Carlos Farre, Scott Barber

Applies to

Summary

This How To explains how to write and use an extensible Load Test plug-in that allows code to be executed at different times while the load test is running. To create a Load Test plug-in, you will need to create a class that inherits the ILoadTestPlugin interface. This How To explains the steps involved in creating a Load Test plug-in class and integrating it with your load test.

Contents


Objectives

Overview

You can create a Load Test plug-in to run code at different times while a load test is running; for example:
The events that the implementation of ILoadTestPlugin can handle are LoadTestStarting, LoadTestFinished, LoadTestWarmupComplete, TestStarting, TestFinished, TestSelected, ThresholdExceeded, HeartBeat, and LoadTestAborted.
An implementation of a Load Test plug-in can be used to extend or modify the behavior of a load test as it runs. The following list contains some possible uses of a Load Test plug-in:

Summary of Steps

Step 1. Create a Load Test Containing One or More Web Tests in a Test Mix

First, create a test project that will contain your load test and Web tests. Next, create individual Web tests that reflect key business user scenarios. Finally, create a load test to which you will add your Web tests. While creating the load test, you can set many run-time properties to generate the desired load simulation; for example, you can specify the load pattern, browser, and network types and add the performance counters to be monitored.
For more information on creating load tests, see “How To: Create a Load Test Using Visual Studio .NET 2005" at << to link to how to >>

Step 2. Create a Load Test Plug-In

In this step, you create a plug-in class for your load test project. The Load Test plug-in is a point of extensibility that allows code to be executed at different times while the load test is running. To create a plug-in, you can expand upon the built-in functionality of the load test. To do this, you must first create a class that inherits the ILoadTestPlugin interface. This class must implement the Initialize method of this interface.
The process of writing a Load Test plug-in can be summarized as follows:

Create a Load Test Plug-in Class

To create a load test plug in class, carry out the following steps: using Microsoft.VisualStudio.TestTools.LoadTesting; public class MyLoadTestPlugin: ILoadTestPlugin private LoadTest mLoadTest;


Your class should now look like this:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
 using Microsoft.VisualStudio.TestTools.LoadTesting;

 namespace CustomPlugin
 {
 
    public class CustomLoadTestPlugin : ILoadTestPlugin
    {
        #region ILoadTestPlugin Members
        private LoadTest mLoadTest;
        private string someOthermemberVariable;       
    }
 }


More Information
The purpose of the member variables is as follows:

Implement the Initialize Method

Implement the Initialize method of the ILoadTestPlugin interface, which takes the LoadTest object as a parameter. In the Initialize method, assign local variable mLoadTest and any other declared member variables whose value is available through parameter context. Also subscribe to any of the events that are relevant to the purpose of your Load Test plug-in, for example TestFinishedEvent, raised when iteration of one test is finished.

 public void Initialize(LoadTest mloadTest)
 {
   //load Test object
   this.mLoadTest = myloadTest;            
   if (this.mLoadTest.Context.ContainsKey("SomeOtherParameter"))
   {
      try
      {
         //Retrieve the value
	 someOthermemberVariable = (String)this.mLoadTest.Context[
"SomeOtherParameter"]);
      }
      catch (FormatException)
      {
	   throw new ApplicationException(
                " SomeOtherParameter not in string format");
      }
   }
                
   //Subscribe to the relevant event, in this case we are subscribing to
   // TestFinished event 
   this.mLoadTest.TestFinished += new 
                EventHandler<TestFinishedEventArgs>(LoadTestFinished);
 
   ….
 }


More Information
Before the test starts, the Initialize method is called once. The mLoadTest variable is assigned with LoadTest passed as a parameter; member variables are assigned from the context parameters read from the LoadTest object.
Also, the Load Test plug-in subscribes to the one of the following event handlers, depending on which one is relevant to the intended purpose of the custom code:

Implement the Relevant Event Handler.

In our case, we have subscribed to the TestFinished event; you will implement the relevant event handler as follows:

 void LoadTestFinished(object sender, TestFinishedEventArgs e)
 {
      // custom code to be executed when the test is finished.
 }


More Information
Every time a test is completed; the TestFinished event is raised, which will be handled by this callback function. Any custom code in the event handler will be executed each time the test execution has finished.

Example Load Testing Plug-In Class

The following is the complete code sample for the plug-in class, which you can use as a reference to build your own custom Load Test plug-in:

 using System;
 using System.Collections.Generic;
 using System.IO;
 using System.Text;
 using Microsoft.VisualStudio.TestTools.LoadTesting;
 
 namespace CustomPlugin
 {
    public class CustomLoadTestPlugin : ILoadTestPlugin
    {
        #region ILoadTestPlugin Members
        private LoadTest mLoadTest;
        private string SomeOtherParameter;
 
        public void Initialize(LoadTest loadTest)
        {
            mLoadTest = loadTest;
 
            if (mLoadTest.Context.ContainsKey("SomeOtherParameter"))
            {
                try
                {
                   SomeOtherParameter =
                   (string)mLoadTest.Context["SomeOtherParameter"];
                }
                catch (FormatException)
                {
                    throw new ApplicationException("MaxTestIterations 
                                                    not in string format");
                }
 
                mLoadTest.TestFinished += new 
                EventHandler<TestFinishedEventArgs>(mLoadTestTestFinished);
 
                ….
            }
        }


        void mLoadTestTestFinished(object sender, TestFinishedEventArgs e)
        {
            // Custom code to be executed when test execution is finished
        }
        #endregion
    }
 }

Step 3. Add the New Load Test Plug-In Class to the Test Project.

After the Load Test Plug-in class has been developed, add the class project containing the plug-in into the test project. This is required for configuring the load test properties for the Load Test plug-in.

Step 4. Add the Load Test Plug-In to the Load Test.

In this step, you add the Load Test plug-in to the load test so that it can be executed. For a load test to execute the Load Test plug in, the load test needs to be configured after the Load Test plug-in has been added to the test project. The Load Test plug-in needs to be added in the properties of the root node of the load test. You do this by performing the following detailed steps: LoadTestPlug-In 1.GIF LoadTestPlug-In 2.GIF

Step 5 Add Context Parameters.

The context parameters are data object variables that you pass to the Load Test plug-in. These paramters can be assigned to local variables during the initialize phase before the load test executes, or during the execution of the callback function. Context parameters are used to allow the passing of variables during test execution to make it possible to change load test settings such as current load, based on conditions such as the Web test name and the number of test executions.
Add context parameters by performing the following steps: LoadTestPlug-In 3.GIF LoadTestPlug-In 4.GIF

Step 6 – Run the Load Test

In this step, you run the load test (created with one or more Web tests in the mix setting) which now includes the Load Test plug-in that was added in the previous steps.

Resources