Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking page load time of several URL simultaneously

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.

like image 710
Vaibhav Avatar asked Dec 21 '22 06:12

Vaibhav


1 Answers

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);
    }
}
like image 156
CodeCaster Avatar answered Dec 28 '22 22:12

CodeCaster