Question

Can anyone guide me what C# code should I write for getting page load time of each URL if the URLs are given as an input ?

OR

if possible please provide me link to any software that does that. Any software that takes several URL as input and provides the page load time for each URL.

Was it helpful?

Solution

Do you want to measure the time it takes for the first request to be answered, or do you want to include the downloading of style and external scripts and clientside rendering?

The first can simply be solved by using a WebClient.

WebClient client = new WebClient ();

// Add a user agent header in case the 
// requested URI contains a query.

client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();

Stream data = client.OpenRead (@"your-url-here");
StreamReader reader = new StreamReader (data);
string s = reader.ReadToEnd();

stopwatch.Stop();
Console.WriteLine("Time elapsed: {0}", stopwatch.Elapsed);

data.Close();
reader.Close();

While for the latter, put a WebBrowser on a form where you create a method for consuming the DocumentCompleted event:

// in your form declarations
Stopwatch _stopwatch = new Stopwatch();
String _url = @"your-url-here";

// in the button-click or whenever you want to start the test
_stopwatch.Start();
this.WebBrowser1.Navigate(_url);

// The DocumentCompled event handler
private void WebBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url == _url)
    {  
        _stopwatch.Stop();
        Console.WriteLine("Time elapsed: {0}", _stopwatch.Elapsed);
    }
}

OTHER TIPS

If I am understanding the question correctly, then Apache JMeter could do this: http://jmeter.apache.org/

It is scriptable, and you can set up a variety of scenarios for load testing.

Try this

var URLs = new[] 
{ 
    "http://www.google.com", 
    "http://www.microsoft.com", 
    "http://www.slashdot.org"
};

var tasks = URLs.Select(
url => Task.Factory.StartNew(task => 
    {
        using (var client = new WebClient())
        {
            var t = (string)task;
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.Start();
        String result = client.DownloadString(t);
        stopwatch.Stop();
            Console.WriteLine(String.Format("{0} = {1}", url, stopwatch.Elapsed));
        }
    }, url)
    ).ToArray();
    Task.WaitAll(tasks);

With this I get

http://www.microsoft.com = 00:00:05.1784172 milliseconds
http://www.slashdot.org = 00:00:09.9922422 milliseconds
http://www.google.com = 00:00:10.8720623 milliseconds

I recommend YSlow ,is very useful tools for checking website performance ,YSlow

I'm sure your familiar with the Web debugging proxy Fiddler. There is a lot in Fiddler that you will not need (like a UI) for your application, however they provide a .NET library that can be included in your project that will give you all the HTTPness that you could ever want.

FiddlerCore is currently provided as a .NET class library that can be consumed by any .NET application. FiddlerCore is designed for use in special-purpose applications that run with either no user-interface (e.g. test automation), or a UI which is so specialized that a Fiddler Addon would not be a suitable option (e.g. a WPF traffic visualization).

There is a sample app included in the download that I modified in a few minutes that will give you access to the same information shown in the fiddler UI under the statistics tab. Check Session.Timers object in the fiddlercore dll for these values.

ClientConnected: 15:03:05.017
ClientBeginRequest: 15:03:05.056
ClientDoneRequest: 15:03:05.076
Determine Gateway: 0ms
DNS Lookup: 3ms
TCP/IP Connect: 20ms
HTTPS Handshake: 0ms
ServerConnected: 15:03:05.151
FiddlerBeginRequest: 15:03:05.152
ServerGotRequest: 15:03:05.157
ServerBeginResponse: 15:03:05.292
ServerDoneResponse: 15:03:05.314
ClientBeginResponse: 15:03:05.331
ClientDoneResponse: 15:03:05.333
Overall Elapsed: 00:00:00.2770277

Hope this helps, you will need to figure out how to create your own session instead of using the proxy but that is a stated feature of the product and should not require much time.

You'll want a Stopwatch, and either a WebClient or a WebRequest.

I am using this to keep internal web sites active and measure/log response time, kind of a keep alive service:

static void Main(string[] args)
{
    LoggingManager.ConfigureAtStartup();

    ErrorLogger.LogInformation("STARTED");

    try
    {
        if (args.Length < 1)
        {
            ErrorLogger.LogInformation("No parameters provided...");
            return;
        }

        int pingTimeoutMilliseconds = Convert.ToInt32(ConfigurationManager.AppSettings["pingTimeoutMilliseconds"]);

        var urls = args[0].Split(';');

        foreach (string url in urls)
        {
            if (string.IsNullOrWhiteSpace(url))
            {
                continue;
            }

            ErrorLogger.LogInformation(String.Format("Pinging url: {0}", url));

            using (var client = new WebClient())
            {
                client.Credentials = CredentialCache.DefaultCredentials;

                var stopW = new Stopwatch();

                stopW.Start();

                string result = client.DownloadString(url);

                var elapsed = stopW.ElapsedMilliseconds;
                stopW.Stop();

                if (elapsed > pingTimeoutMilliseconds)
                {
                    ErrorLogger.LogWarning(String.Format("{0} - took: {1} milliseconds to answer!", url, elapsed.ToString("N0")));

                    ErrorLogger.LogInformation(String.Format("Response was: {0} chars long", result.Length.ToString("n0")));
                }                        
            }
        }
    }
    catch(Exception exc)
    {
        ErrorLogger.LogError(exc);
    }
    finally
    {
        ErrorLogger.LogInformation("COMPLETED");

        LoggingManager.ShutdownOnExit();
    }
}

ErrorLogger is a little wrapper I made around Log4Net.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top