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.
我只是碰巧检查了ASP的性能。我们正在构建的NET MVC应用程序。我要将局部视图插入到循环中,出于好奇,我检查了渲染页面需要多长时间。结果并不好。
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.
我需要做更多的结论性调查,但万一有人有类似的问题或更深刻的见解,以下是我到目前为止得到的结论。首先,我应该说所有的结果和测量都是在多次页面加载之后完成的,并且我在我的web.config中设置了< compile debug="false">。
- 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.
- 似乎一个渲染部分会造成5ms的命中(至少在我的环境中)。当我内联部分视图的实际内容时,我实际上得到了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.
- 当我将一个空的局部视图包含到大约70个元素的循环中时,总呈现时间增加了大约60ms。这里可能有一些缓存,但并不理想。
- 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.
- 我调试ASP。发现部分视图被缓存,但它只缓存到ascx的路径。然后每次使用BuildManager实例化实际的视图。CreateInstanceFromVirtualPath方法。
- And now the interesting bit: When include the same partial view using the WebForms syntax (
<my:UserContol runat="server" />
), the extra 60ms go away. -
现在有趣的是:当使用WebForms语法包含相同的部分视图时(
),额外的60ms就消失了。
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?
根据上面的观察,罪魁祸首似乎是建筑经理。CreateInstanceFromVirtualPath方法。也许,它不应该被多次调用。网络表单大概不会使用它;或者对每个ascx只使用一次?
3 个解决方案
#1
8
I've just changed a MVC2 view from using a partial view in a loop to a single view, i.e. :
我刚刚将MVC2视图从循环中使用局部视图更改为单个视图,即:
<table>
foreach(var a in items)
{
<%: Html.Partial("SomePartialView",a) %>
}
</table>
Where SomePartialView contains the code to render a single row in a table, e.g. :
其中SomePartialView包含用于在表中呈现一行的代码,例如:
<tr><td>Model.Name</td><td>Model.description</td></tr>
to :
:
foreach(var a in items)
{
<tr><td>a.Name</td><td>a.description</td></tr>
}
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.
对于视图呈现900行,页面呈现时间从5分钟以上的页面加载减少到小于30秒,这是调用部分视图时存在显著开销的确凿证据。我确信,当您有一个单独的调用时,这是可以忽略的,但是在一个循环中,它都是加起来的,所以我建议如果可能的话,在循环中避免部分视图。
#2
2
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.
在循环中包含部分视图会降低性能,并且可以通过在partialview中移动该循环来加快速度。
Some sample reading (which references the caching of the viewpath) can be found here.
这里可以找到一些示例读取(它引用viewpath的缓存)。
#3
2
60ms is such a small interval it sounds like statistical noise to me, not conclusive evidence of a performance difference.
60毫秒是一个很小的间隔,听起来像是统计噪音,而不是性能差异的决定性证据。
#1
8
I've just changed a MVC2 view from using a partial view in a loop to a single view, i.e. :
我刚刚将MVC2视图从循环中使用局部视图更改为单个视图,即:
<table>
foreach(var a in items)
{
<%: Html.Partial("SomePartialView",a) %>
}
</table>
Where SomePartialView contains the code to render a single row in a table, e.g. :
其中SomePartialView包含用于在表中呈现一行的代码,例如:
<tr><td>Model.Name</td><td>Model.description</td></tr>
to :
:
foreach(var a in items)
{
<tr><td>a.Name</td><td>a.description</td></tr>
}
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.
对于视图呈现900行,页面呈现时间从5分钟以上的页面加载减少到小于30秒,这是调用部分视图时存在显著开销的确凿证据。我确信,当您有一个单独的调用时,这是可以忽略的,但是在一个循环中,它都是加起来的,所以我建议如果可能的话,在循环中避免部分视图。
#2
2
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.
在循环中包含部分视图会降低性能,并且可以通过在partialview中移动该循环来加快速度。
Some sample reading (which references the caching of the viewpath) can be found here.
这里可以找到一些示例读取(它引用viewpath的缓存)。
#3
2
60ms is such a small interval it sounds like statistical noise to me, not conclusive evidence of a performance difference.
60毫秒是一个很小的间隔,听起来像是统计噪音,而不是性能差异的决定性证据。