Udostępnij za pośrednictwem


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. You create a plug-in to expand upon or modify the built in functionality of the load test. For example, you can code a load test plug-in to set or modify the load test pattern while the load test is running. 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 Plug-In

Reference

ILoadTestPlugin