我目前正在测量从 C# 程序加载网页所花费的时间。
Visual Studio 解决方案有一个控制台程序和一个只有一页的 ASP.NET 网站。该网站托管在 ASP.NET 开发服务器上。控制台应用程序如下查询网页:
bool isSuccess;
Stopwatch timeSpentToDownloadPage = Stopwatch.StartNew();
WebRequest request = HttpWebRequest.Create(new Uri("http://localhost:12345/Test.aspx", UriKind.Absolute));
request.Timeout = 200;
using (WebResponse response = request.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
string responseText = sr.ReadToEnd().Trim();
isSuccess = (responseText == "Hello World");
}
timeSpentToDownloadPage.Stop();
该网页没有什么特别的,只是加载时的响应,没有 ASP.NET 代码:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello World");
}
现在,每次代码花费 0 毫秒时,秒表都会显示。完成所有工作(查询服务器、获取响应等)。
怎么可能呢?难道我做的事情有什么问题吗?真的,我预计至少 10-20 毫秒。执行所有客户端代码,然后 100 毫秒。 to:
- [客户端]从uri中找到要查询的页面(不需要DNS查询,所以速度相当快),
- [客户端]做请求,
- [服务器端]初始化ASP.NET引擎,
- [服务器端]处理请求,
- [服务器端]查找并读取.aspx文件并执行编译的代码(至少读取文件可能需要几毫秒。),
- [服务器端]构建响应,包括标头,然后发送它,
- [客户端]接收响应并对其进行处理(修剪)。
那么为什么它的速度如此之快呢?是否存在一个棘手的缓存,它只是跳过所有/大部分步骤并将“Hello World”返回给客户端?
如果存在“隐藏”缓存,它在哪里以及如何禁用它来测量所花费的“真实”时间?
I'm currently measuring the time spent to load a web page from a C# program.
The Visual Studio solution has a console program and an ASP.NET website with just one page. The website is hosted on ASP.NET Development Server. The console application queries the web page like this:
bool isSuccess;
Stopwatch timeSpentToDownloadPage = Stopwatch.StartNew();
WebRequest request = HttpWebRequest.Create(new Uri("http://localhost:12345/Test.aspx", UriKind.Absolute));
request.Timeout = 200;
using (WebResponse response = request.GetResponse())
{
Stream responseStream = response.GetResponseStream();
StreamReader sr = new StreamReader(responseStream);
string responseText = sr.ReadToEnd().Trim();
isSuccess = (responseText == "Hello World");
}
timeSpentToDownloadPage.Stop();
The web page has nothing special, just a response on load and no ASP.NET code:
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("Hello World");
}
Now, the stopwatch shows every time that the code spent 0 ms. to do all the work (querying the server, getting the response, etc.).
How is it possible? Isn't there something wrong with what I'm doing? Really, I expected at least 10-20 ms. to execute all client-side code, then 100 ms. to:
- [client side] Find the page to query from uri (which does not require DNS query, so it's quite fast),
- [client side] Do the request,
- [server side] Initialize ASP.NET engine,
- [server side] Process the request,
- [server side] Find and read .aspx file and execute compiled code (at least reading file may cost several ms.),
- [server side] Build response, including headers, then send it,
- [client side] Receive the response and process it (trim).
So why is it so extremely fast? Is there a tricky cache which just skips all/most of the steps and return "Hello World" to the client?
If there is a "hidden" cache, where is it and how can I disable it to measure the "real" time spent?
发布评论
评论(1)
是的,这可以而且必须是 0 毫秒!因为您的服务器位于
localhost
上。尝试在全球网络的其他位置获取 ASP.NET 服务器。并且您不会解析 localhost,因为这是
127.0.0.1
的常量地址。与该地址的通信仅由操作系统内核操作。
Yes this can and must be 0 ms! Because your server is on a
localhost
. Try to get ASP.NET server somewhere else in a world wide network.And you don't resolving localhost because this is a constant address to
127.0.0.1
.And communication to this address is operated only by the your OS kernel.