如何分析 WPF 4.5 UI 性能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33468572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:30:48  来源:igfitidea点击:

How to profile WPF 4.5 UI performance?

.netwpfmvvmprofiling.net-4.5

提问by Alexander Abakumov

I'm developing a .NET 4.5 WPF app and having UI rendering performance issues.

我正在开发一个 .NET 4.5 WPF 应用程序并遇到 UI 呈现性能问题。

After some googling, I came across WPF Performance Suitepage which describes exactly the tool I need - Visual Profiler. It allows to view WPF elements tree and analyze the contribution of each element to the total rendering time.

经过一番谷歌搜索后,我发现了WPF Performance Suite页面,该页面准确描述了我需要的工具 - Visual Profiler。它允许查看 WPF 元素树并分析每个元素对总渲染时间的贡献。

The only problem is that the page states that the tool is contained in Microsoft Windows SDK v7.1 which is targeting Windows 7 and .NET 4.0.

唯一的问题是该页面指出该工具包含在针对 Windows 7 和 .NET 4.0 的 Microsoft Windows SDK v7.1 中。

Since my app is for .NET 4.5 and I'm on Windows 8.1, I've installed Windows SDK for Windows 8.1. To my surprise, it doesn't seem to contain the WPF Performance Suite at all and that tool in particular.

由于我的应用程序适用于 .NET 4.5 并且我在 Windows 8.1 上,我已经安装了Windows SDK for Windows 8.1。令我惊讶的是,它似乎根本不包含 WPF Performance Suite,尤其是该工具。

Then, I've tried to install the WPF Performance Suitefrom thisanswer, but it works only with .NET 4.0 apps.

然后,我尝试从这个答案安装WPF 性能套件但它仅适用于 .NET 4.0 应用程序

So, where do you get the WPF Performance Suite for .NET 4.5 apps?
Or, to be more general, how do you profile WPF UI rendering performance of .NET 4.5 apps to find out which elements in the tree have highest performance impact in complex UIs?

那么,您从哪里获得适用于 .NET 4.5 应用程序的 WPF 性能套件?
或者,更笼统地说,您如何分析 .NET 4.5 应用程序的 WPF UI 渲染性能,以找出树中哪些元素对复杂 UI 的性能影响最大?

回答by Alexander Abakumov

Eventually, I've found a tool that I was looking for and that was really helpful for me.

最终,我找到了一个我一直在寻找的工具,它对我很有帮助。

To get an idea of where exactly is the bottleneck in rendering your WPF layout, you want to:

要了解渲染 WPF 布局的瓶颈究竟在哪里,您需要:

  1. Install the Visual Studio 2015, if you don't have one yet :)
  2. Go to Debug-> Start Diagnostic Tools Without Debugging
    (NOTE: This seems to be changed to Debug-> Profiler-> Performance Profiler...).
  3. Check Application Timelinetool and hit Startin this view:
  1. 安装Visual Studio 2015,如果你还没有 :)
  2. 转到Debug-> Start Diagnostic Tools without Debugging
    注意:这似乎已更改为Debug-> Profiler-> Performance Profiler...)。
  3. 检查应用程序时间线工具并在此视图中点击开始

Tool settings

工具设置

  1. When your app starts, do the actions causing issues you're interested in.
  2. Then hit Stop recordingand you'll eventually get your elements tree with times spent for rendering every node. Then, you only need to sort it by Duration (total)and expand slowest nodes until you find the problem:
  1. 当您的应用程序启动时,执行导致您感兴趣的问题的操作。
  2. 然后点击停止记录,你最终会得到你的元素树,其中包含渲染每个节点所花费的时间。然后,你只需要按Duration (total)排序并展开最慢的节点,直到找到问题:

Result

结果

In conclusion, it would much easier to correlate the nodes from tree above with your layout if you set names for your controls using the Nameattribute like the following:

总之,如果您使用如下Name属性为控件设置名称,将上面树中的节点与您的布局相关联会容易得多:

<TextBlock Name="OwnerContact">

回答by Alexandru

As it turns out, later versions of Visual Studio have this built in, so right now with Visual Studio 2013, I can create a new profiler session by opening up Visual Studio, and from the top menu there is an Analyze drop down, just go to Analyze -> Profiler -> Attach/Detach to attach it to an existing process running in Debug mode (I seem to have to do this inside of another Visual Studio window or else its grayed out). But, side note: its really bad and not very intuitive...I would recommend using some nicer, commercial profilers instead because they are much simpler.

事实证明,更高版本的 Visual Studio 内置了这个功能,所以现在使用 Visual Studio 2013,我可以通过打开 Visual Studio 创建一个新的分析器会话,从顶部菜单中有一个分析下拉菜单,就去分析 -> 探查器 -> 附加/分离以将其附加到在调试模式下运行的现有进程(我似乎必须在另一个 Visual Studio 窗口内执行此操作,否则它会变灰)。但是,旁注:它真的很糟糕而且不是很直观......我建议使用一些更好的商业分析器,因为它们更简单。