使用 Azure Application Insights 查找和诊断性能问题Find and diagnose performance issues with Azure Application Insights

Azure Application Insights 从应用程序收集遥测,以帮助分析操作和性能。Azure Application Insights collects telemetry from your application to help analyze its operation and performance. 可以使用此信息确定可能发生的问题,或识别对用户影响最大的应用程序改进。You can use this information to identify problems that may be occurring or to identify improvements to the application that would most impact users. 本教程将从应用程序服务器组件和客户端这两方面介绍分析性能的过程。This tutorial takes you through the process of analyzing the performance of both the server components of your application and the perspective of the client. 学习如何:You learn how to:

  • 识别服务器端操作的性能Identify the performance of server-side operations
  • 分析服务器操作,确定性能减慢的根本原因Analyze server operations to determine the root cause of slow performance
  • 识别最慢的客户端操作Identify slowest client-side operations
  • 使用查询语言分析页面视图的详细信息Analyze details of page views using query language


完成本教程:To complete this tutorial:

登录 AzureLog in to Azure

通过 https://portal.azure.cn 登录到 Azure 门户。Log in to the Azure portal at https://portal.azure.cn.

识别缓慢的服务器操作Identify slow server operations

Application Insights 收集应用程序中不同操作的性能详细信息。Application Insights collects performance details for the different operations in your application. 识别持续时间最长的操作,可以诊断潜在问题,或更有针对性地进行开发,以提高应用程序总体性能。By identifying those operations with the longest duration, you can diagnose potential problems or best target your ongoing development to improve the overall performance of the application.

  1. 选择“Application Insights”,然后选择订阅。Select Application Insights and then select your subscription.

  2. 选择“调查”下的“性能”菜单或单击“服务器响应时间”图,打开“性能”面板 。To open the Performance panel either select Performance under the Investigate menu or click the Server Response Time graph.


  3. “性能”面板将显示应用程序每项操作的计数和平均持续时间。The Performance panel shows the count and average duration of each operation for the application. 此信息可用于识别对用户影响最大的操作。You can use this information to identify those operations that most impact users. 在此示例中,很可能应调查“GET Customers/Details”和“GET Home/Index”,因为它们持续时间相对较长、调用次数相对较多 。In this example, the GET Customers/Details and GET Home/Index are likely candidates to investigate because of their relatively high duration and number of calls. 其他操作可能持续时间更长,但调用次数极少,因此改进它们可能收效甚微。Other operations may have a higher duration but were rarely called, so the effect of their improvement would be minimal.


  4. 此图正显示一段时间内所选操作的平均持续时间。The graph currently shows the average duration of the selected operations over time. 可以切换到第 95 个百分点值,以便查找性能问题。You can switch to the 95th percentile to find the performance issues. 将要调查的操作固定到图中以添加该操作。Add the operations that you're interested in by pinning them to the graph. 此图显示,存在值得调查的峰值。This shows that there are some peaks worth investigating. 在图上缩短时间窗口,进一步隔离出这些峰值。Isolate this further by reducing the time window of the graph.


  5. 右侧的性能面板显示所选操作的不同请求的持续时间的分布情况。The performance panel on the right shows distribution of durations for different requests for the selected operation. 缩小窗口,从大约第 95 个百分点值开始。Reduce the window to start around the 95th percentile. “前 3 个依赖项”见解卡大致说明了外部依赖项可能是造成事务速度慢的原因。The "Top 3 dependencies" insights card, can tell you at a glance that the external dependencies are likely contributing to the slow transactions. 单击带示例数的按钮即可查看示例列表。Click on the button with number of samples to see a list of the samples. 然后即可选择任意示例来查看事务详细信息。You can then select any sample to see transaction details.

  6. 可以大致看到,调用 Fabrikamaccount Azure 表所花的时间占了事务总持续时间的大部分。You can see at a glance that the call to Fabrikamaccount Azure Table is contributing most to the total duration of the transaction. 也可看到有一个异常导致其失败。You can also see that an exception caused it to fail. 可以单击列表中的任意项,然后就可以在右侧查看其详细信息。You can click on any item in the list to see its details on the right side. 详细了解事务诊断体验Learn more about the transaction diagnostics experience


  7. 探查器可显示操作所运行的实际代码和每步所需的时间,因而可以更深入地进行代码级别的诊断。The Profiler helps get further with code level diagnostics by showing the actual code that ran for the operation and the time required for each step. 由于探查器周期性运行,所以可能未记录到某些操作。Some operations may not have a trace since the profiler runs periodically. 随着时间推移,应可记录到更多操作。Over time, more operations should have traces. 若要对操作启用探查器,请单击“探查器跟踪”。To start the profiler for the operation, click Profiler traces.

  8. 跟踪显示每个操作的单个事件,以便诊断整个操作持续时间的根本原因。The trace shows the individual events for each operation so you can diagnose the root cause for the duration of the overall operation. 单击持续时间最长的示例。Click one of the top examples, which have the longest duration.

  9. 单击“热路径”,以突出显示对操作总持续时间贡献最大的一系列特定事件。Click Hot Path to highlight the specific path of events that most contribute to the total duration of the operation. 在此示例中,可以看到最慢的调用来自 FabrikamFiberAzureStorage.GetStorageTableData 方法。In this example, you can see that the slowest call is from FabrikamFiberAzureStorage.GetStorageTableData method. 最耗时的部分是 CloudTable.CreateIfNotExist 方法。The part that takes most time is the CloudTable.CreateIfNotExist method. 如果每次调用该函数时都执行这行代码,则将使用不必要的网络调用和 CPU 资源。If this line of code is executed every time the function gets called, unnecessary network call and CPU resource will be consumed. 修复该代码的最佳方式是将此行放入仅执行一次的某个启动方法中。The best way to fix your code is to put this line in some startup method that only executes once.


  10. 屏幕顶部的“性能提示”对“持续时间过长是由等待造成的”这一评估结果提供了支持。The Performance Tip at the top of the screen supports the assessment that the excessive duration is due to waiting. 单击“等待”链接可获得解释不同事件类型的文档。Click the waiting link for documentation on interpreting the different types of events.


  11. 如需进一步分析,可单击“下载跟踪”以下载跟踪。For further analysis, you can click Download Trace to download the trace. 可以使用 PerfView 查看此数据。You can view this data using PerfView.

使用服务器的日志数据Use logs data for server

日志提供丰富的查询语言,可用于分析 Application Insights 收集的所有数据。Logs provides a rich query language that allows you to analyze all data collected by Application Insights. 可以用它对请求和性能数据进行深度分析。You can use this to perform deep analysis on request and performance data.

  1. 返回到“操作详细信息”面板,然后单击“日志”图标“在日志中查看(分析)”Return to the operation detail panel and click Logs iconView in Logs (Analytics)

  2. 日志随即打开,包含对面板中每个视图的查询。Logs opens with a query for each of the views in the panel. 可以直接运行这些查询,或根据需要对其进行修改。You can run these queries as they are or modify them for your requirements. 第一个查询显示此操作随时间推移的持续时间。The first query shows the duration for this operation over time.


标识缓慢的客户端操作Identify slow client operations

Application Insights 不仅可以识别要优化的服务器进程,还可以从客户端浏览器的角度进行分析。In addition to identifying server processes to optimize, Application Insights can analyze the perspective of client browsers. 这有助于确定应对客户端组件进行的改进,甚至识别与不同浏览器或不同位置有关的问题。This can help you identify potential improvements to client components and even identify issues with different browsers or different locations.

  1. 在“调查”下选择“浏览器”,然后单击“浏览器性能”,或者在“调查”下选择“性能”,然后切换到“浏览器”选项卡,只需单击右上角的服务器/浏览器切换按钮以打开浏览器性能摘要即可。 Select Browser under Investigate then click Browser Performance or select Performance under Investigate and switch to the Browser tab by clicking the server/browser toggle button in the top right to open the browser performance summary. 这从浏览器的角度提供了应用程序各项遥测的可视化摘要。This provides a visual summary of various telemetries of your application from the perspective of the browser.


  2. 选择一个操作名称,然后单击右下角的蓝色示例按钮,选择一个操作。Select on one of the operation names then click the blue samples button in the bottom right and select an operation. 这样会显示端到端事务详细信息,你可以在右侧查看“页面视图属性”。This will bring up the end-to-end transaction details and on the right side you can view the Page View Properties. 这样就可以查看客户端请求页面的详细信息,包括浏览器类型及其位置。This allows you to view details of the client requesting the page including the type of browser and its location. 此信息有助于确定性能问题是否与特定类型的客户端相关。This information can assist you in determining whether there are performance issues related to particular types of clients.


使用客户端的日志数据Use logs data for client

与针对服务器性能收集的数据一样,Application Insights 允许使用日志深度分析所有客户端数据。Like the data collected for server performance, Application Insights makes all client data available for deep analysis using Logs.

  1. 返回到浏览器摘要,单击“日志”图标“在日志(Analytics)中查看”Return to the browser summary and click Logs icon View in Logs (Analytics)

  2. 日志随即打开,包含对面板中每个视图的查询。Logs opens with a query for each of the views in the panel. 第一个查询显示不同页面视图随时间推移的持续时间。The first query shows the duration for different page views over time.