Monday, 20 February 2012

Automating Web Performance Stats Collection in .NET

You have a web application. You're a .NET developer. Maybe you already have some automated UI testing in place via Selenium, or maybe you don't. What you want to do is automate the collection of some performance metrics about your application.

Q. How would you go about doing that in .NET?
A. Use the following recipe for success.

Ingredients

  • BrowserMob Proxy by Webmetrics, which (quote) is:
    A free utility to help web developers watch and manipulate network traffic from their web applications
  • Selenium, which (quote):
    automates browsers. That's it.
  • BrowserMob Proxy .NET Library, a .NET library to provide a simple way to work with BrowserMob Proxy and Selenium, written by David Burns (blog | twitter) and myself (you're already on my blog | twitter).

Preparation

  1. Download the BrowserMob Proxy from GitHub
  2. Download the BrowserMob Proxy .NET Library from GitHub (binaries, or get the source and build yourself)
  3. Reference Selenium in your .NET project (available via nuget, or from seleniumhq)
  4. Reference the BrowserMob Proxy .NET Library in your .NET project

Make and Bake

Example:
using AutomatedTester.BrowserMob.HAR;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;

namespace AutomatedTester.BrowserMob.Example
{
    public class ExampleClass
    {
        public void ExampleUse()
        {
            // Supply the path to the Browsermob Proxy batch file
            Server server = new Server(@"C:\BrowserMobProxy\bin\browsermob-proxy.bat");
            server.Start();

            Client client = server.CreateProxy();
            client.NewHar("google");

            var seleniumProxy = new Proxy { HttpProxy = client.SeleniumProxy };
            var profile = new FirefoxProfile();
            
            profile.SetProxyPreferences(seleniumProxy);
            // Navigate to the page to retrieve performance stats for
            IWebDriver driver = new FirefoxDriver(profile);
            driver.Navigate().GoToUrl("http://www.google.co.uk");

            // Get the performance stats
            HarResult harData = client.GetHar();           
 
            // Do whatever you want with the metrics here. Easy to persist 
            // out to a data store for ongoing metrics over time.
            
            driver.Quit();
            client.Close();
            server.Stop();
        }   
    }
}
What's great is that if you already have some Selenium tests in place, you can add in the collection of performance metrics quickly and easily. This gives you the ability to collate performance metrics over time - a perfect way to identify problem areas to investigate and to quantify performance improvements you make. To learn more about what is in the performance data, check out these links which go into more detail about the HAR format (HTTP Archive) - this is what Webmetric's BrowserMob Proxy returns, which we expand out into a POCO structure (HarResult type). BrowserMob Proxy allows you to do some pretty funky stuff, such as:
  • blacklisting / whitelisting content
  • simulate network traffic / latency
The .NET library wrapper we've made available supports this functionality. Hopefully, this will come in useful for those in the .NET world!