Saturday, January 18, 2014

Calculate the page load time for both cached and un-cached pages using System.Net.WebClient

Recently I was trying to create a console application for calculating the page load time for sharePoint site pages. I need both the cached and un-cached page load time. Here is the simplest way, using System.Net.WebClient and System.Diagnostics.stopwatch to calculate the page load time. 
Code Sample
class Program
    static void Main(string[] args)

        string urls = "";
        using (WebClient client = new WebClient())
            int i = 0;
            for (i = 0; i < 2; i++)
        //Get uncached data for the first time and next time get data from cache
                if (i == 0)
                    client.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.BypassCache);
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();// start System.Diagnostics.Stopwatch before downloading url
                client.Credentials = new System.Net.NetworkCredential("Username", "Password", "Domain");
                String result = client.DownloadString(urls);
                Console.WriteLine(String.Format("{0} = {1}", urls, stopwatch.Elapsed.TotalSeconds));



No comments:

Post a Comment