With the Load Test Editor, you can configure one load test profile for a given scenario. However, in the real-world testing, you may want to observe the performance of your applications / system under tests under different load patterns. And this can be achieved by writing your custom load profiles management classes and a load test plug-in. (Sample code included)

For example, in the below graph for a 30-min Load run, several load profiles, including const, stepUp, stepDown, CustomSinWave, are simulated about every 7.5 minutes.

image

This post will show you step by step on how to

1) Create customize user load profiles besides the existing LoadTestConstLoadProfile, LoadTestStepLoadProfile, and LoadTestGoalBasedLoadProfile (Covered in Step 1)

2) Simulate multiple and repeated user load patterns in a load test run (Covered in Step 2)

In the previous blog post, we have completed Step 2-1, i.e. Creating CustomLoadProfiles library. In this post, we will complete the load test plug-in code.

Step2 – Create Load Test Plug-in To Simulate User Load Patterns

The changes to enable multi- load profiles are highlighted as below

1) Plug-in data members

Add a private member of type LoadProfileManager. It is used to manage the profile groups, and return the load test profile at run time.

2) Initialize()

Add a Load Test heartbeat event handler to apply the user load change.

Add the profile groups you want to simulate. To simulate more than one load patterns, you can add multiple profiles to a given load test, e.g. const load 40 for 60 seconds followed by user load oscillation between (800, 1000) for 240 seconds. To simulate repeated load pattern group, you can specify the duration of the pattern group. During the load test run, the plug-in will cycle through the load profiles and apply the load accordingly.

3) Load Test heartbeat event handler

Get the Load profile to be applied for the current heart beat event.

[LoadTestPlugin.cs] For graphical view, see the screenshot at the beginning of this post.

using System;
using System.IO;
using System.Reflection;
using System.Collections;
using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.LoadTesting;
using CustomLoadProfiles;

namespace TestProject1
{
    // How to use this class?
    // This is a regular load test plugin class
    // InitLoadProfileGroups() - You specify the pattern groups you want to simulate // m_loadTest_Heartbeat event handler() -
    // CustomLoadProfiles.cs - add more custom load profiles to this file

    public class MultiLoadPatternsPlugin : ILoadTestPlugin
    {
        LoadTest m_loadTest;
        LoadProfileManager m_profileManager;

        public void Initialize(LoadTest loadTest)
        {
            m_loadTest = loadTest;
            m_profileManager = new LoadProfileManager();

            // The heartbeat event will be fired every second, it can be used to adjust the user load
            m_loadTest.Heartbeat += new EventHandler<HeartbeatEventArgs>(m_loadTest_Heartbeat);

            // Add the pattern groups you want to simulate
            InitLoadProfileGroups();
        }

        void InitLoadProfileGroups()
        {
            // Add profiles to a group
            // Simulate a custom user load pattern for 30 mins
            LoadProfileGroup profileGroup = new LoadProfileGroup("Custom1", 2400);

            // Simulate Const load 50 for 60 seconds
            profileGroup.AddProfile(new ConstLoadWrapper(50, 60));

            // Add custom load profile - simulate sin curve load
            CustomLoadWrapper profileWrapper = new CustomLoadWrapper(240);
            profileWrapper.Profile = new LoadTestSinLoadProfile(60, 30, 30, 1, 5);
            profileGroup.AddProfile(profileWrapper);

            // Simulate step up load - step up 30 users every 5 seconds for 40 seconds
            profileGroup.AddProfile(new StepLoadWrapper(60, 0, 300, 30, 5, 0, 40));

            // Simulate user load oscillation between [280, 320] for 60 seconds
            profileWrapper = new CustomLoadWrapper(60);
            profileWrapper.Profile = new LoadTestSinLoadProfile(300, 20, 30, 2, 5);
            profileGroup.AddProfile(profileWrapper);

            // Step down user load - step down 30 users every 5 seconds for 40 seconds
            profileWrapper = new CustomLoadWrapper(40);
            profileWrapper.Profile = new LoadTestStepDownLoadProfile(300, 60, 300, 30, 5);
            profileGroup.AddProfile(profileWrapper);

            // Add the profile group
            m_profileManager.ProfileGroups.Add(profileGroup);

            m_profileManager.SetStartEndTimeForProfileGroups(0);
        }

        // The following heartbeat event will be fired every second, it can be used to adjust the user load
        void m_loadTest_Heartbeat(object sender, HeartbeatEventArgs e)
        {
            if (!e.IsWarmupComplete)
            {
                return;
            }

            // Get the load profile to be applied for the event
            LoadTestLoadProfile profile = m_profileManager.GetCurrentLoadProfile(e.ElapsedSeconds, m_loadTest.Scenarios[0].CurrentLoad);
            if (profile != null)
            {
                m_loadTest.Scenarios[0].LoadProfile = profile;
            }
        }
    }
}