October 5, 2015 .NET, Performance, VS
October 5, 2015 .NET, Performance, VS
Load testing is a great way of finding out if there are any performance issues with your application. If you don’t know what a load test in VS is, please read this detailed article on MSND on how to use it.
I have experience with creating load tests for high load web services in entertainment industry. At the moment I’m working on internal web application in which users exclusively “check out” and “check in” big sets of data (I will call them “reports” here). In this post I want to describe one specific use case for load testing.
In our application only one user can work at a time with a particular report. Because of this, load tests cannot be utilizing simulation of multiple users for same data. Basically, for one particular user and report, testing has to be synchronous. User finds a report, checks it out, checks it in and so on for the same report. We want to create multiple such scenarios and run them in parallel simulating same user working on different sets of data. Can be extended to really simulate multiple users if we implement some windows impersonation as well.
There are other requirements to this performance testing. We want to quickly switch web server we are running this tests against. This also means that database will be different, therefore we cannot supply requests with hardcoded data.
I’ve started with creating a first web performance test using web browser recording. It records all requests with the server in IE add-on. I recorded one scenario.
Depending on your application you might get a lot of requests recorded. I only left those that are most important and time consuming. One of the first things you want to do is to “Parameterize Web Servers…”. This will extract your server name into separate “Context Parameter” which you can later easily change. Also in my case, most of the requests are report specific, so I added another parameter called “ReportId” and then used “Find and Replace in Request…” to replace concrete id with “{{ReportId}}” parameter.
Recorder obviously records everything “as is” by embedding concrete report’s json into “String Body”. I want to avoid this by extracting “ReportJson” into a parameter and then using it in PUT requests. You can do this using “Add Extraction Rule…” on GET request and specify that you want to save response into a parameter. Now you can use “{{ReportJson}}” in String Body of put requests. As simple as that.
Unfortunately, not everything is that straight forward. When our application does a PUT request it generates a correlationId that is later used to update client on the processing progress. To do custom actions you can write plugins. I’ve added two of them. One is basically to skip some of the dependant requests (you can use similar to skip requests to external systems referenced from you page).
The other plug-in I’ve implemented is to take parameter “ReportJson” and update it with new generated correlationId. Here it is:
using System; using Microsoft.VisualStudio.TestTools.WebTesting; namespace YourAppLoadTesting { public class AddCorrelationIdToReportPutRequest: WebTestPlugin { public string ApplyToRequestsThatContain { get; set; } public string BodyStringParam { get; set; } public override void PreRequest(object sender, PreRequestEventArgs e) { if (e.Request.Url.Contains(ApplyToRequestsThatContain) && e.Request.Method == "PUT") { var requestBody = new StringHttpBody(); requestBody.ContentType = "application/json; charset=utf-8"; requestBody.InsertByteOrderMark = false; requestBody.BodyString = e.WebTest.Context[BodyStringParam].ToString() .Replace("\"correlationId\":null", string.Format("\"correlationId\":\"{0}\\\\0\"", Guid.NewGuid())); e.Request.Body = requestBody; } base.PreRequest(sender, e); } } }
In pluging configuration I set ApplyToRequestsThatContains to “api/report” and also BodyStringParam to “ReportJson”.
To finish with my load test all I had to do is to copy-paste few of this webtests and change ReportIds. After that I added webtests to a load test as separate scenarios making sure that every scenario has constant load of only 1 user. This makes sure that each scenario runs synchronous operations on individual reports.
I was actually very surprised how flexible and extensive load tests in VS are. You can even generate code from your webtest to have complete control over your requests. At least I would recommend you to generate code at least once to understand what it does under the hood.
I hope this helps someone.
code
more code
~~~~