How can I get the loading time and volume of a web

2020-04-28 07:44发布

问题:

I'm working on a program that should measure loading time and volume of a website that I give as an input.

Here I have some code that returns just response time of website but I want the total loading time and total volume of items such (pictures, JavaScript, HTML, etc.).

public string Loading_Time(string url)
{
    Stopwatch stopwatch = new Stopwatch();

    WebClient client = new WebClient();
    client.Credentials = CredentialCache.DefaultCredentials;

    stopwatch.Start();
    string result = client.DownloadString(url);
    stopwatch.Stop();

    return stopwatch.Elapsed.Milliseconds.ToString();
}

How can I achieve that?

回答1:

This is going to be a little bit tough. Start by using something like HTMLAgilityPack or something similar to parse the returned html from your original request (dont try to parse HTML yourself!)

Scan through the object representation of the HTML once parsed, and decide what you want to measure the size of. Typically this will be

  • Includes, such as CSS, or javascript
  • Images in IMG and BUTTON elements, as well as background images

The difficulty is that often images are specified as part of a css stylesheet - so are you going to try to parse every css file to obtain these too?

The original request you made for the HTML you could have obtained the byte size of the downloaded string. Start with this number as your "volume".

Now make a separate request for each js, css, image etc file in the same way. But all you're interested in is the byte size of each download - its readily available when you make an HTTP request. Add each item's byte size to the total.

When you're finished you will have the total byte size for all artifacts of that web page.



标签: c# asp.net