View from the Ivory Tower

Chris Kadel's attempt at a blog with an ironic title.

Team Test – Selective "Dependent Requests" during Load Tests

Posted by cbkadel on August 4, 2008

I haven’t found a lot of blog postings about this topic so I thought it would be useful to have this point out there.  When a web test is run, it makes a request and then asynchronously – it will follow and request images, stylesheets, JavaScript external files and download them.  During a load test, making these requests (especially back to the server you’re monitoring) are indeed important to keep.  What if you have dependent requests (images/scripts) to external servers that you do not care as much about?  And what if those requests are failing (especially on a network for load testing) that doesn’t allow external requests through.

You can do a few things when you want to ignore those requests:

  1. 1.) Set “Parse Dependent Requests” property to false in the request properties screen.
  2. 2.) Use a Web Test Request Plugin which can ignore selective requests.

Option 1 may work just fine especially if you don’t have a lot requests coming back to your servers and mostly have the external references that you do not care about.  Option 1 may be a very bad thing – especially if your dependent requests are in fact (in large part) going back to the server you’re monitoring (you want those requests to be made still).  But if you want to just ignore one or two of many – Option 1 may not be the best choice to make.

Option 2 – this requires very little code and can save a lot of time and help to ensure your load test statistics are accurate.  The downside is that it’s a little bit extra code that you have to support.  It’s very trivial though, I should add.

How the Web Test Request Plugin Fits In:

Before getting into the code, let’s first have a quick look at where events are fired and how you can use the web test request plugin (at least from the perspective of this goal).  There are two events that you can override in a Web Test Request Plugin:

        • - PreRequest
        • - PostRequest

image

In the above flow, the Parse Dependent Requests happens after a response is returned from the web server.  Thereafter, Web Test will asynchronously make those dependent requests.  Therefore, below overlayed with the process is where you can inject functionality through the web test request plugin:

image

Therefore, the PostRequest method is the place to perform our override.  This will happen before the dependent requests have been made to the server, but after the first request has been made.

Below is some simple code with this process included.

class DependentRequestsFilter : WebTestRequestPlugin
    {
        private string _excludeStrings;

        public string ExcludeStrings
        {
            get { return _filter; }
            set { _filter = value; }
        }

        public override void PostRequest(object sender, PostRequestEventArgs e)
        {
            List<WebTestRequest> requestsToRemove = new List<WebTestRequest>();
            foreach (WebTestRequest request in e.Request.DependentRequests)
            {
                foreach (string excludeUrl in ExcludeStrings.Split(new char[] { ','} ))
                {
                    if (request.Url.ToLower().Contains(excludeUrl.ToLower()))
                    {
                        requestsToRemove.Add(request);
                    }
                }
            }

            foreach (WebTestRequest requestToRemove in requestsToRemove)
            {
                e.Request.DependentRequests.Remove(requestToRemove);
            }
        }

A few things to point out.  If you make a public property on your plugin, you’ll see that when you add that plug into your web test.  The Visual Studio Property window recognizes it and allows entry.  Secondly, notice that I split the ExcludeStrings with a comma… that’s to allow multiple filters.  Sometimes in web tests, you may have 10s or 100s of requests and several that need to be filtered out.  This is of course for demo purposes only, but it could be a little more robust with a check for null in there.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>