Skip to content

Instantly share code, notes, and snippets.

@gareth-cheeseman
Created April 5, 2018 08:42
Show Gist options
  • Save gareth-cheeseman/fe7f60cb291447bec946a9d7f0548b77 to your computer and use it in GitHub Desktop.
Save gareth-cheeseman/fe7f60cb291447bec946a9d7f0548b77 to your computer and use it in GitHub Desktop.
Custom Load Profile for VS loadtesting. Must be in root of solution.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.LoadTesting;
namespace Tests
{
public class CustomLoadProfile : ILoadTestPlugin
{
// The initialize method is called just before the load test starts running
public void Initialize(LoadTest loadTest)
{
_mLoadTest = loadTest;
_currentUserLoad = 0;
// Register to listen for the heartbeat event
loadTest.Heartbeat += new EventHandler<HeartbeatEventArgs>(loadTest_Heartbeat);
}
private static int RampUp(int currentUsers, int maxUsers, int elapsedSeconds, int step)
{
if ((currentUsers < maxUsers) && (elapsedSeconds % step == 0))
{
currentUsers += 1;
}
return currentUsers;
}
private static int RampDown(int currentUsers, int minUsers, int elapsedSeconds, int step)
{
if ((currentUsers > minUsers) && (elapsedSeconds % step == 0))
{
currentUsers -= 1;
}
return currentUsers;
}
private int SetStepProfile(int elapsedSeconds, int previousUserLoad = 0)
{
var userload = ((LoadTestScenario)_mLoadTest.Scenarios[0]).CurrentLoad;
var stepLoadProfile = StepLoadProfile(elapsedSeconds);
userload = stepLoadProfile.Users > previousUserLoad ? RampUp(userload, stepLoadProfile.Users, elapsedSeconds, 2) : RampDown(userload, stepLoadProfile.Users, elapsedSeconds, 2);
return userload;
}
private struct Step
{
internal readonly int Start, End, Users;
internal Step(int start, int end, int users)
{
Start = start;
End = end;
Users = users;
}
}
private static Step StepLoadProfile(int elapsedSeconds)
{
var profile = new List<Step>
{
new Step(0, 660, 30),
new Step(660, 780, 64),
new Step(780, 900, 94),
new Step(900, 1080, 128),
new Step(1080, 1260, 162),
new Step(1260, 2460, 200),
new Step(2460, 2580, 162),
new Step(2580, 2700, 128),
new Step(2700, 2880, 64),
new Step(2880, 3000, 30),
new Step(3000, 3600, 10)
};
return profile.Single(p => p.Start <= elapsedSeconds && p.End > elapsedSeconds); ;
}
// This method changes the current user load for the 1st Scenario in the load test
// This example assumes there is only one scenario in the load test
private void SetLoad(int newLoad)
{
((LoadTestScenario)_mLoadTest.Scenarios[0]).CurrentLoad = newLoad;
}
// HeartBeat event handler - this gets calls once every second during the load test
// including during the warmup period
private void loadTest_Heartbeat(object sender, HeartbeatEventArgs e)
{
if (!e.IsWarmupComplete) return;
_currentUserLoad = SetStepProfile(e.ElapsedSeconds, _currentUserLoad);
SetLoad(_currentUserLoad);
}
private LoadTest _mLoadTest;
private int _currentUserLoad;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment