Creating a custom load profile which will step user load up and then back down

This blog post is going to show you how to create a custom load profile. Out of the box there are 3 load profiles: constant, step and goal. I will show you how to create a load profile and how to set it with a load test plug-in. We will walk through creating a profile which is similar to the step profile, but after a set number of seconds will start decreasing the load in a step manner.

First, the ability to create a custom load profile was added in VS 2008 SP1. So if you have not installed SP1, please install it now.

Creating the profile:

1) Add a class to the test project.

2) Add a using statement for Microsoft.VisualStudio.TestTools.LoadTesting

3) To implement a custom profile, you have to extend the LoadTestLoadProfile class. Check out Bill Barnett’s blog post for more information on the base class: https://blogs.msdn.com/billbar/pages/load-test-api-enhancements-in-vsts-2008-sp1-beta.aspx

4) You can extend one of the existing profiles. We will extend LoadTestStepLoadProfile. We want our custom profile to do the what the step profile does, but then switch behavior at a configured time in the load test.

5) We need to add 3 new properties to our custom profile:

public int StepDownUserCount {get; set;}

      public int StepDownStepDuration { get; set; }

      public int StartStepDownAtRunOffset { get; set; }

6) A custom load profile needs to override the int GetLoad(int elapsed) method. We want to use the base functionality up until the StartStepDownAtRunOffset. At that point we need to start the step down. Here is what our method will look like:

public override int GetLoad(int elapsed)

        {

            if (elapsed < StartStepDownAtRunOffset)

            {

             m_lastStepUpCount = base.GetLoad(elapsed);

                return m_lastStepUpCount;

            }

            else

            {

                long currentUserCount = m_lastStepUpCount - (StepDownUserCount * ((elapsed - StartStepDownAtRunOffset) / StepDownStepDuration));

                return (int)Math.Max(Math.Max(currentUserCount, 0), MinUserCount);

            }

        }

7) Here is the full class:

 

using System;

using System.Diagnostics;

using Microsoft.VisualStudio.TestTools.LoadTesting;

namespace TestProject1

{

    [Serializable()]

    public class StepUpAndDownLoadProfile : LoadTestStepLoadProfile

    {

        public StepUpAndDownLoadProfile(int runDuration)

        {

            InitialUserCount = 10;

            StepUserCount = 10;

            StepDuration = 10;

            StartStepDownAtRunOffset = runDuration;

            m_runDuration = runDuration;

          

        }

       

        // ******************************************************************

               

        public override int GetLoad(int elapsed)

        {

            if (elapsed < StartStepDownAtRunOffset)

            {

                m_lastStepUpCount = base.GetLoad(elapsed);

                return m_lastStepUpCount;

            }

            else

            {

                long currentUserCount = m_lastStepUpCount - (StepDownUserCount * ((elapsed - StartStepDownAtRunOffset) / StepDownStepDuration));

                return (int)Math.Max(Math.Max(currentUserCount, 0), MinUserCount);

            }

        }

        public override void Validate()

        {

            base.Validate();

           

        }

        public int StepDownUserCount {get; set;}

        public int StepDownStepDuration { get; set; }

        public int StartStepDownAtRunOffset { get; set; }

        // ******************************************************************

        // Private members

        // ******************************************************************

        private int m_runDuration;

        private int m_lastStepUpCount;

    }

}

 

Creating the Plug-in:

You set a custom load profile in a load test plug-in. The plug-in below will create an instance of the new plug-in and set it on the first scenario. You can see that it sets the step up and step down properties. The user load does not need to decrease at the same rate that it increased.

 

using Microsoft.VisualStudio.TestTools.LoadTesting;

namespace TestProject1

{

    public class SampleLoadTestPlugin : ILoadTestPlugin

    {

        #region ILoadTestPlugin Members

        public void Initialize(LoadTest loadTest)

        {

            StepUpAndDownLoadProfile p = new StepUpAndDownLoadProfile(loadTest.RunSettings.RunDuration);

            p.InitialUserCount = 20;

            p.MaxUserCount = 100;

            p.MinUserCount = 5;

            p.StepUserCount = 5;

            p.StepDuration = 10;

            p.StepRampTime = 0;

            p.StartStepDownAtRunOffset = 480;

            p.StepDownStepDuration = 5;

            p.StepDownUserCount = 10;

            loadTest.Scenarios[0].LoadProfile = p;

        }

        #endregion

    }

}

 

 

The values used in the above plug-in do the following:

1) Start at 20 users.

2) Add 5 users every 10 seconds until you reach 100 users

3) Stay at 100 users until 480 seconds into the test.

4) Then start decreasing the user load by 10 users every 5 second until you reach 5 users.

I ran this test for ten minutes. Here is what the user load counter for this test looks like:

 

 

 

I hope this post shows you how to create custom plug-in and set them on a scenario. Another way to extend this example would be to create a xml document that has the user load at different times during the load test. Then you could read that xml do in from the plugin and pass it to your custom profile. Then the custom profile would set the userload based on the values from the xml doc. This would allow you to step the load up, step down, back up, etc.