如何衡量网页的响应和加载时间? [英] How can I measure the response and loading time of a webpage?

查看:123
本文介绍了如何衡量网页的响应和加载时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要构建一个Windows窗体应用程序来测量完全加载网页所需的时间,什么是最好的方法?

I need to build a windows forms application to measure the time it takes to fully load a web page, what's the best approach to do that?

这个小应用程序的目的是在预定的时间间隔内监视网站中的某些页面,以便能够提前知道网络服务器或数据库服务器是否出了问题.

The purpose of this small app is to monitor some pages in a website, in a predetermined interval, in order to be able to know beforehand if something is going wrong with the webserver or the database server.

其他信息:

我不能使用商业应用程序,我需要开发它,以便能够将结果保存到数据库并基于此信息创建一系列报告.

I can't use a commercial app, I need to develop this in order to be able to save the results to a database and create a series of reports based on this info.

webrequest解决方案似乎是我要使用的方法,但是,能够测量完全加载页面(图像,css,javascript等)所花费的时间会很好.知道怎么做吗?

The webrequest solution seems to be the approach I'm goint to be using, however, it would be nice to be able to measure the time it takes to fully load the the page (images, css, javascript, etc). Any idea how that could be done?

推荐答案

如果您只想记录获取基本页面源代码所花费的时间,则可以将HttpWebRequest包装在秒表周围.例如

If you just want to record how long it takes to get the basic page source, you can wrap a HttpWebRequest around a stopwatch. E.g.

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(address);

System.Diagnostics.Stopwatch timer = new Stopwatch();
timer.Start();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();

timer.Stop();

TimeSpan timeTaken = timer.Elapsed;

但是,这不会考虑下载额外内容(例如图像)的时间.

However, this will not take into account time to download extra content, such as images.

[edit]作为替代方案,您也许可以使用WebBrowser控件,并测量执行.Navigate()和触发事件之间的时间.我认为这还将包括额外内容的下载和渲染时间.但是,我没有大量使用WebBrowser控件,只是不知道是否在重复请求同一页面时是否必须清除缓存.

[edit] As an alternative to this, you may be able to use the WebBrowser control and measure the time between performing a .Navigate() and the DocumentCompleted event from firing. I think this will also include the download and rendering time of extra content. However, I haven't used the WebBrowser control a huge amount and only don't know if you have to clear out a cache if you are repeatedly requesting the same page.

这篇关于如何衡量网页的响应和加载时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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