.Net:如何模拟和测量完整的网络请求?

发布于 2024-08-17 04:44:30 字数 1300 浏览 6 评论 0 原文

我正在尝试使用 WebRequest 测量请求,

但与使用 FireBug 测量相比,我得到的结果要小得多。

我猜这是因为某些内容(例如图像和 CSS)未包含在内。

有没有办法测量完整的网络请求?

我的代码:

        public string GetPageHtmlTime(string strUrl)
    {
        WebRequest request = null;
        WebResponse response = null;
        HttpWebResponse httpCurrentWeResponse = null;

        try
        {
            //making a request to the file.
            request = WebRequest.Create(strUrl);
            //set 5 seconds timeout for the request
            request.Timeout = 5 * 1000;

            //Stopwatch
            Stopwatch sw = new Stopwatch();
            sw.Start();

            //get the server response
            response = request.GetResponse();
            httpCurrentWeResponse = (HttpWebResponse)response;
            sw.Stop();

            //if the http response return any type of failure
            if (httpCurrentWeResponse.StatusCode != HttpStatusCode.OK || response == null)
                return "Error: " + httpCurrentWeResponse.StatusCode;

            response.Close();

            //Return time:
            return "OK time=" + sw.ElapsedMilliseconds.ToString("0,0");

        }
        catch (System.Exception ex)
        {
            return "Error: ex=" + ex.Message;
        }

    }

I’m trying to measure a request with WebRequest,

But I’m getting significant smaller results then measuring with FireBug.

I guessing it’s because some content like Images and CSS isn’t included.

Is there a way to measure a full web request?

My code:

        public string GetPageHtmlTime(string strUrl)
    {
        WebRequest request = null;
        WebResponse response = null;
        HttpWebResponse httpCurrentWeResponse = null;

        try
        {
            //making a request to the file.
            request = WebRequest.Create(strUrl);
            //set 5 seconds timeout for the request
            request.Timeout = 5 * 1000;

            //Stopwatch
            Stopwatch sw = new Stopwatch();
            sw.Start();

            //get the server response
            response = request.GetResponse();
            httpCurrentWeResponse = (HttpWebResponse)response;
            sw.Stop();

            //if the http response return any type of failure
            if (httpCurrentWeResponse.StatusCode != HttpStatusCode.OK || response == null)
                return "Error: " + httpCurrentWeResponse.StatusCode;

            response.Close();

            //Return time:
            return "OK time=" + sw.ElapsedMilliseconds.ToString("0,0");

        }
        catch (System.Exception ex)
        {
            return "Error: ex=" + ex.Message;
        }

    }

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

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

发布评论

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

评论(2

凉墨 2024-08-24 04:44:30

我不知道这是否适合您,但您可以使用 WebBrowser 控件,因为它将在触发 DocumentCompleted 事件之前请求页面的所有元素。

I don't know if it's an option for you, but you can use the WebBrowser control, as it will request all the elements of the page before firing the DocumentCompleted event.

甜妞爱困 2024-08-24 04:44:30

您的代码只会测量代码完成所需的时间,代码不会等待所有字节到达客户端,这将比代码花费更长的时间。

采取何种措施以及在何处采取措施取决于您希望在何处进行优化。如果您想在服务器轻负载时改善客户端的体验,那么 Firebug(或 Fiddler)将是一个很好的测量位置。如果您不想在服务器负载较重时提高服务器的性能,那么代码分析器将是您需要的工具。

Your code will only measure how long it takes for the code complete, the code will not wait for all the bytes to arrive at the client which will take significantly longer than the code.

What and where measure depends on where you expect to make optimisations. If you want to improve the experience at the client when the server is under light load then Firebug (or Fiddler) would be a good place to be measuring. If you wan't to improve performance on the server when its under heavy load then code profilers would the sort of tool you would be needing.

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