我只是碰巧检查了我们正在构建的 ASP.NET MVC 应用程序的性能。我打算将部分视图插入循环中,出于好奇,我检查了渲染页面所需的时间。结果并不好。
我需要做更多结论性的调查,但以防万一有人有类似的问题或更深入的了解,这就是我迄今为止所掌握的。首先,我应该说所有结果和测量都是在多个页面加载后完成的,并且我在 web.config 中设置了
。
- 似乎单个渲染部分会产生大约 5 毫秒的命中(至少在我的环境中)。当我内联部分视图的实际内容时,我几乎得到了 0 毫秒。
- 当我将一个空的部分视图包含到大约 70 个元素的循环中时,总渲染时间增加了约 60 毫秒。所以大概有一些缓存,但这并不理想。
- 我调试了 ASP.NET MVC,发现部分视图被缓存,但它只缓存 ascx 的路径。然后每次使用 BuildManager.CreateInstanceFromVirtualPath 方法。
- 现在有趣的是:当使用 WebForms 语法 (
) 包含相同的部分视图时,额外的 60 毫秒就消失了。
因此,根据上面的观察,罪魁祸首似乎是 BuildManager.CreateInstanceFromVirtualPath 方法。也许,它不应该被多次调用。网络表单大概不使用它;或者以某种方式对每个 ascx 仅使用一次?
I just happen to check the performance of an ASP.NET MVC application we are building. I was going to insert a partial view into a loop, and just out of curiosity I checked how long it took to render the page. The result was not good.
I need to do more conclusive investigation, but just in case there was somebody with similar issues or more insight, here is what I have so far. First, I should say that all results and measurements were done after multiple page loads and that I have set <compilation debug="false">
in my web.config.
- It seems that a single render partial incurs about 5ms hit (at least in my environment). When I inline the actual content of the partial view, I get practically 0ms.
- When I include an empty partial view to a loop of about 70 elements, the total render time increases by ~ 60ms. So there is some caching presumably, but it's not ideal.
- I debugged ASP.NET MVC, and found out that partial views are cached, but it only caches the paths to the ascx's. The actual views are then instantiated every time using the BuildManager.CreateInstanceFromVirtualPath method.
- And now the interesting bit: When include the same partial view using the WebForms syntax (
<my:UserContol runat="server" />
), the extra 60ms go away.
So based on the observations above, it seems the culprit is the BuildManager.CreateInstanceFromVirtualPath method. Maybe, it was not meant to be called multiple times. Webforms presumably don't use it; or use it somehow only once for each ascx?
发布评论
评论(3)
我刚刚将 MVC2 视图从在循环中使用部分视图更改为单个视图,即:
其中 SomePartialView 包含在表中渲染单行的代码,例如:
到:
对于渲染页面渲染的 900 行的视图页面加载时间从 5 多分钟缩短到不到 30 秒,这非常有力地证明调用部分视图时会产生巨大的开销。我确信当您进行一次调用时,这是可以忽略不计的,但是在循环中,所有这些都会加起来,因此我建议如果可能的话,避免在循环中使用部分视图。
I've just changed a MVC2 view from using a partial view in a loop to a single view, i.e. :
Where SomePartialView contains the code to render a single row in a table, e.g. :
to :
for a view rendering 900 rows the page render time went down from 5+ minutes page load to less than 30 secs, pretty conclusive proof that there is a significant overhead when calling partial views. I'm sure this is negligible when you have a single call, however in a loop it all adds up so I'd recommended avoiding partial views in a loop if possible.
我猜答案是......这取决于?
部分视图会降低性能(实际调用的开销等)。
部分视图不会被缓存。
在循环内包含局部视图会降低性能,并且可以通过将循环移至局部视图内来再次加快速度。
可以找到一些示例阅读(引用视图路径的缓存)此处。
I am guessing the answer is ... it depends?
Partial views decrease performance (the overhead of the actual call etc).
Partial views are not cached.
Including a partial view inside a loop will decrease performance, and can be sped up again by moving the loop inside the partialview instead.
Some sample reading (which references the caching of the viewpath) can be found here.
60 毫秒是一个很小的间隔,对我来说听起来像是统计噪声,而不是性能差异的确凿证据。
60ms is such a small interval it sounds like statistical noise to me, not conclusive evidence of a performance difference.