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!

9 comments:

  1. is there a java version for this? Ariel

    ReplyDelete
  2. Hi Ariel, I'm not 100% sure tbh. I know on GitHub there are Python and Ruby equivalent wrappers but as I'm from a .NET background, I haven't looked into Java

    ReplyDelete
  3. hey Adrian,
    i think that this: https://github.com/webmetrics/browsermob-proxy
    may be what i was looking for.
    i will try to use it.
    Thanks,
    Ariel

    ReplyDelete
  4. Thanks Adrian, this has helped me immensely. I've linked from my question here: http://sqa.stackexchange.com/questions/2915/how-can-i-automate-testing-of-http-requests
    Steve

    ReplyDelete
  5. Hey Adrian,

    Thanks for the post. I was wondering if you know if it is possible to modify headers using Browsermob in C#.

    Thanks,

    ReplyDelete
    Replies
    1. Hi,

      I'm afraid I don't know. Best place for that kind of question would be on the BrowserMob proxy mailing list: https://groups.google.com/forum/?fromgroups#!forum/browsermob-proxy

      Delete
  6. There is a Java version as well:
    http://roydekleijn.github.io/BrowsermobHTTPRESTclient/

    But keep in mind, BrowserMob Proxy provides a default Embeddeder.

    And,

    For sure you can modify headers.

    ReplyDelete
  7. browser-mob used with selenium c# do not create proper har file ...It do not record some attributes correctly like page properties, response headers many other which is very important in order to analyze har file..... Please let me know what to do if you have tackle these problems .....and one more do we have documentation available to use browser-mob with c#............
    let me know if any alternative present to do performance testing for websites with selenium

    ReplyDelete