Randomizing Input Data for Visual Studio Load Tests
While preparing for my presentation Load and Performance Testing: How to do Transactional Root-Cause Analysis with Visual Studio Team System for Testers that I gave at the Boston .NET User Group on May 13th I came across certain load-testing topics. One was: How to randomize Input Data.
If you go with Visual Studio you can code your web tests in any .NET Language giving you the freedom to create random data by using e.g.: System.Random. If you however want to use the “nicer” UI Driven Web Test Development (that’s how I call it) – you are limited in your options. You add web requests – you can parameterize input values by using Context Parameters with hard coded values, you can reuse values extracted from a previous request or you can use data from an external data source like a database, CSV or XML file.
Basic random numbers for VSTS Web Tests
One thing that I missed was the ability to use basic random values, e.g.: a random number from 1 to 5 that would be used for a quantity field of a web form or a random username with the pattern “testuserX” where X is in the range of my test user accounts.
In order to do that there only seems to be one way – implementing a WebTest or WebTestRequest Plugin that generates random data and makes it available as Context Parameter to the test.
Sample: Randomizing username and password
Lets get back to my username/password example. Following illustration shows my original Web Test:
I extracted the hard coded values from the recorded web request into a context parameter – but still using the hard coded values. One option that I would have here – as indicated in the first paragraph – would be to make this test data driven by binding the Context Parameters to an external data source. But this is not what I want in my example. I really want to randomize the input data so that every web request uses a new random value. I therefore created a WebTestRequestPlugin as shown in the following image:
My plugin defines two properties that can be configured with a Parameter Name (or names) and a random value pattern. In the PreRequest override I use a random value generator that generates the random value based on the passed pattern and put the generated value in the Test Context.
Now I can use the plugin in my webtest as follows:
Now I can run my Web Test either “standalone” by defining the number of iterations or using it in a Load Test. Every time the WebRequest will be executed the WebTestRequestPlugin will create a new random value that will then be used by the request. This allows me to run this test over and over again – always using a different username and password.
Conclusion
Check out the option to create your own WebTestRequestPlugin or WebTestPlugin. This extension option gives you more flexibility when creating web tests for Visual Studio.
Related posts:
- How to extend Visual Studio 2010 Web- and Load-Testing with Transactional Tracing Microsoft recently published the first official beta build of Visual...
- How to test jQuery enabled Apps using JSON with Visual Studio Visual Studio Team System offers a nice Web- and Load-Testing...
- Visual Studio Team System for Unit-, Web- and Load-Testing with dynaTrace Last week I was given the opportunity to meet the...
- Extending Visual Studio with Transactional Tracing Visual Studio is the number one development environment for Microsoft...
- Extending Visual Studio Unit Testing with Transactional Tracing In my previous blog entry I wrote about how to...











Randomizing Input Data for Visual Studio Load Tests…
Thank you for submitting this cool story – Trackback from DotNetShoutout…
[...] Randomizing Input Data for Visual Studio Load Tests Performance … [...]
Hi nice post on WebTestRequestPlugin …. Its really good Article that feed our mind thank you for the post. From macrotesting i got some useful Articles and Tools that helped me much in such things. http://www.macrotesting.com. The way you have explained the concepts and uses is very good. Thank you…
cheers
sakthi
Thanks for this post, was really useful for me!
The code as provided does not work. Either you keep the context variable in the webtest and it never gets updated, or you remove the context variable and the webtest crashes at that step saying cannot find context variable.
Hi Tyler
The Context variable is actually a property from the passed PreRequestEventArg parameter. I access it via e.WebTest.Context. This property should always be valid and contain the current context including all context variables.
You have to make sure to specify all your expected context parameters in your test definition as it can be seen in the first screenshot
The example works fine on my Visual Studio 2008 Service Pack 1
public override void PreRequest(object sender, PreRequestEventArgs e)
{
base.PreRequest(sender, e);
Guid guid = Guid.NewGuid();
string value = “tScrp” + guid.ToString().Replace(“-”, “”).Substring(2, 15);
foreach (string paramName in ParameterName.Split(‘,’))
{
e.WebTest.Context.Add(paramName, value);
}
}
the above code never updates the existing context parameter.
The add throws me off, it would seem like I would want to update the parameter as opposed to adding an identically named parameter?
I stepped through and watched the context variable being set to the random number, however the test used the static hard coded value.
I notice in your last image you do not have the Username context parameter defined anymore. Is this by design? it’s a bit confusing.
We are using VS2008 SP1. Typical MS stuff.
I don’t know how you got yours to work. This link illustrates my frustration.
http://social.msdn.microsoft.com/Forums/en-US/vstswebtest/thread/881b6969-2cd6-4e72-9417-8b70a22141a5
Hi Tyler
Send me an email – it is my firstname.lastname@dynatrace.com
I can then send you my complete project that contains several plugins that I’ve been working on
I can confirm that this doesn’t work. A WebTestRequestPlugin is triggered too late to change the context parameters for the current request.
If you make it a WebTestPlugin, you can set context parameters in the “PreWebTest” (NOT “PreRequest”), but of course at that stage you don’t have any preceding request information.
Probably fine for what you’re doing, but it doesn’t work for me where I’m requesting some data, then operating on that data to create input for the next request.
what version of visual studio do you use?
Didn’t work for me either. It seems that Tyler and Ben are correct. The context parameters value DO change but too late. When you analyze the results you see that the Request use the hardcoded parameters values, and when you see the context tab, the values for the paramters are changed with the values from the plugin.
Andreas can you be so kind to post a complete working sample, send me you test project as you offer to Tyler.
I’m using VS2008 SP1.
Best Regards.