同时检查多个URL的页面加载时间 [英] Checking page load time of several URL simultaneously

查看:161
本文介绍了同时检查多个URL的页面加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果URL作为输入给出,谁能指导我为获得每个URL的页面加载时间而编写什么C#代码?

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

如果可能的话,请提供给我指向执行此操作的任何软件的链接.任何使用多个URL作为输入并提供每个URL的页面加载时间的软件.

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.

推荐答案

您是要测量第一个请求得到响应所花费的时间,还是要包含样式和外部脚本的下载以及客户端渲染?

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?

可以通过使用 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();

对于后者,请放置 WebBrowser 在您创建用于使用

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);
    }
}

这篇关于同时检查多个URL的页面加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆