How to: Create a Load Test Plug-In

You can create a load test plug-in to run code at different times while the load test is running. To create a plug-in, you can expand upon the builtin functionality of the load test. To do this, you must create a class that inherits the ILoadTestPlugin interface. This class must implement the Initialize method of this interface. For more information, see ILoadTestPlugin.

Note

You can also create plug-ins for Web tests. For more information, see How to: Create a Web Test Plug-In

To create a Load Test Plug-in using C#

  1. Open a Test Project that contains a Web test.

    For more information, see How to: Create a Test Project.

  2. Add a Load test to the Test Project and configure it to run a Web test.

    For more information, see How to: Launch the Load Test Wizard.

  3. Add a C# Class Library Project to your test solution.

  4. Add a reference to the Microsoft.VisualStudio.QualityTools.LoadTestFramework dll in the Class Library project.

  5. In the class file located in the Class Library project, add a using statement for the Microsoft.VisualStudio.TestTools.LoadTesting namespace.

  6. Implement the ILoadTestPlugin interface for the class created in the Class Library project. See the following Example section for a sample implementation.

  7. In the Test Project, right-click and select Add Reference. From the Projects tab, select the Class Library Project. Click OK.

  8. Open the Load test and select the top node of the Load test. Press F4 to display the Properties window. You can now set the Load Test Plug-in property by clicking the ellipsis (…). Select your class in the dialog box.

Example

The following code shows a Load Test Plug-in that runs custom code after a LoadTestFinished event occurs. If this code is run on a test rig and the test rig does not have a localhost SMTP service, the load test will remain in the "In progress" state as a message box will be open.

using System;
using Microsoft.VisualStudio.TestTools.LoadTesting;
using System.Net.Mail;
using System.Windows.Forms;

namespace LoadTestPluginTest
{
    public class MyLoadTestPlugin : ILoadTestPlugin
    {
        LoadTest myLoadTest;

        public void Initialize(LoadTest loadTest)
        {
            myLoadTest = loadTest;
            myLoadTest.LoadTestFinished += new
                EventHandler(myLoadTest_LoadTestFinished);
        }

        void myLoadTest_LoadTestFinished(object sender, EventArgs e)
        {
            try
            {
                // place custom code here
                MailAddress MyAddress = new MailAddress("someone@example.com");
                MailMessage MyMail = new MailMessage(MyAddress, MyAddress);
                MyMail.Subject = "Load Test Finished -- Admin Email";
                MyMail.Body = ((LoadTest)sender).Name + " has finished.";

                SmtpClient MySmtpClient = new SmtpClient("localhost");
                MySmtpClient.Send(MyMail);
            }

            catch (SmtpException ex)
            {
                MessageBox.Show(ex.InnerException.Message +
                    ".\r\nMake sure you have a valid SMTP.", "LoadTestPlugin");
            }
        }
    }
} 

There are eight events that are associated with a Load test that can be handled in the Load Test Plug-in to run custom code with the Load test. The following is a list of the events which provide access to different periods of the load test run:

See Also

Tasks

How to: Create a Web Test Request Plug-In
How to: Create a Web Test Plug-In

Reference

ILoadTestPlugin