同时检查多个 URL 的页面加载时间

发布于 2024-12-15 17:33:56 字数 139 浏览 1 评论 0原文

如果 URL 作为输入给出,谁能指导我应该编写哪些 C# 代码来获取每个 URL 的页面加载时间?

或者

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

温柔戏命师 2024-12-22 17:33:57

我用它来保持内部网站活跃并测量/记录响应时间,这是一种保持活动的服务:

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 是我围绕 Log4Net 制作的一个小包装器。

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.

帅的被狗咬 2024-12-22 17:33:57

您需要一个Stopwatch,以及一个WebClientWebRequest

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

欲拥i 2024-12-22 17:33:57

我相信您熟悉 Web 调试代理 Fiddler。 Fiddler 中有很多内容是您的应用程序不需要的(例如 UI),但是它们提供了一个可以包含在您的项目中的 .NET 库,该库将为您提供您可能想要的所有 HTTPness。

FiddlerCore 目前作为 .NET 类库提供,
可由任何 .NET 应用程序使用。 FiddlerCore 专为
在运行时不运行的特殊用途应用程序中使用
用户界面(例如测试自动化),或如此专业的用户界面
Fiddler 插件不是一个合适的选择(例如 WPF 流量可视化)。

下载中包含一个示例应用程序,我在几分钟内修改了它,您可以通过它访问统计选项卡下的 fiddler UI 中显示的相同信息。检查 fiddlercore dll 中的 Session.Timers 对象是否有这些值。

客户端连接:15:03:05.017
客户端开始请求:15:03:05.056
客户端完成请求:15:03:05.076
确定网关:0ms
DNS 查找:3 毫秒
TCP/IP 连接:20ms
HTTPS 握手:0 毫秒
服务器已连接:15:03:05.151
FiddlerBeginRequest:15:03:05.152
服务器得到请求:15:03:05.157
服务器开始响应:15:03:05.292
服务器完成响应:15:03:05.314
客户端开始响应:15:03:05.331
客户端完成响应:15:03:05.333
总体已用时间:00:00:00.2770277

希望这会有所帮助,您需要弄清楚如何创建自己的会话而不是使用代理,但这是产品的既定功能,不需要太多时间。

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.

仅此而已 2024-12-22 17:33:57

我推荐YSlow,是检查网站性能非常有用的工具,YSlow

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

水波映月 2024-12-22 17:33:57

试试这个

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

有了这个我得到

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

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
安静 2024-12-22 17:33:56

如果我正确理解了这个问题,那么 Apache JMeter 可以这样做: http://jmeter.apache.org/

它是可编写脚本的,您可以设置各种负载测试场景。

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.

等风来 2024-12-22 17:33:56

您是否想要测量响应第一个请求所需的时间,或者是否想要包括样式和外部脚本的下载以及客户端渲染?

第一个可以简单地通过使用 网络客户端

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

对于后者,请在一个表单,您可以在其中创建使用 DocumentCompleted 事件:

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

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文