排查 Azure 应用服务中应用性能缓慢的问题Troubleshoot slow app performance issues in Azure App Service

本文帮助排查 Azure 应用服务中应用性能缓慢的问题。This article helps you troubleshoot slow app performance issues in Azure App Service.

如果在本文中有任何需要协助的地方,可以联系 MSDN Azure 和堆栈溢出论坛上的 Azure 专家。If you need more help at any point in this article, you can contact the Azure experts on the MSDN Azure and the Stack Overflow forums. 或者,也可以提出 Azure 支持事件。Alternatively, you can also file an Azure support incident. 请转到 Azure 支持站点,并单击“获取支持”。Go to the Azure Support site and click on Get Support.

症状Symptom

浏览应用时,页面加载缓慢,并且有时还会超时。When you browse the app, the pages load slowly and sometimes timeout.

原因Cause

此问题通常是应用程序级别的问题造成的,例如:This problem is often caused by application level issues, such as:

  • 网络请求耗时过长network requests taking a long time
  • 应用程序代码或数据库查询效率低下application code or database queries being inefficient
  • 应用程序的内存/CPU 使用率过高application using high memory/CPU
  • 应用程序因异常而崩溃application crashing due to an exception

疑难解答步骤Troubleshooting steps

故障排除可划分为三种不同的任务,依次为:Troubleshooting can be divided into three distinct tasks, in sequential order:

  1. 观察和监视应用程序行为Observe and monitor application behavior
  2. 收集数据Collect data
  3. 缓解问题Mitigate the issue

应用服务在每个步骤提供了多种选项。App Service gives you various options at each step.

1.观察和监视应用程序行为1. Observe and monitor application behavior

跟踪服务运行状况Track Service health

每次发生服务中断或性能下降时 Azure 会进行宣传。Azure publicizes each time there is a service interruption or performance degradation. 可以在 Azure 门户中跟踪服务的运行状况。You can track the health of the service on the Azure portal.

监视应用Monitor your app

此选项可让你找出应用程序是否存在任何问题。This option enables you to find out if your application is having any issues. 在应用的边栏选项卡中,单击“请求和错误”磁贴 。In your app's blade, click the Requests and errors tile. “指标”边栏选项卡显示所有可以添加的指标。 The Metric blade shows you all the metrics you can add.

可能需要在应用中监视的一些指标包括Some of the metrics that you might want to monitor for your app are

  • 平均内存工作集Average memory working set
  • 平均响应时间Average response time
  • CPU 时间CPU time
  • 内存工作集Memory working set
  • 请求Requests

监视应用性能

有关详细信息,请参阅:For more information, see:

监视 Web 终结点状态Monitor web endpoint status

如果在“标准”定价层中运行应用,应用服务允许从三个地理位置监视两个终结点。 If you are running your app in the Standard pricing tier, App Service lets you monitor two endpoints from three geographic locations.

终结点监视可从测试 Web URL 的响应时间和运行时间的分布式地理位置配置 Web 测试。Endpoint monitoring configures web tests from geo-distributed locations that test response time and uptime of web URLs. 该测试可对 Web URL 执行 HTTP GET 操作,以从每个位置确定响应时间和运行时间。The test performs an HTTP GET operation on the web URL to determine the response time and uptime from each location. 每个已配置位置每 5 分钟运行一次测试。Each configured location runs a test every five minutes.

使用 HTTP 响应代码监视运行时间,并且以毫秒为单位计算响应时间。Uptime is monitored using HTTP response codes, and response time is measured in milliseconds. 如果 HTTP 响应代码大于或等于 400 或响应时间超过 30 秒,则监视测试失败。A monitoring test fails if the HTTP response code is greater than or equal to 400 or if the response takes more than 30 seconds. 如果从所有指定的位置监视测试均成功,则终结点被视为可用。An endpoint is considered available if its monitoring tests succeed from all the specified locations.

有关详细信息,请参阅在 Azure 应用服务中监视应用To set it up, see Monitor apps in Azure App Service.

另外,有关终结点监视的视频,请参阅保持 Azure 网站运行以及终结点监视 - Stefan SchackowAlso, see Keeping Azure Web Sites up plus Endpoint Monitoring - with Stefan Schackow for a video on endpoint monitoring.

使用扩展的应用程序性能监视Application performance monitoring using Extensions

还可以使用站点扩展监视应用程序的性能。 You can also monitor your application performance by using a site extension.

每个应用服务应用都提供了一个可扩展的管理终结点,通过此终结点可使用一组作为站点扩展部署的功能强大的工具。Each App Service app provides an extensible management end point that allows you to use a powerful set of tools deployed as site extensions. 扩展包括:Extensions include:

  • 源代码编辑器,如 Azure DevOpsSource code editors like Azure DevOps.
  • 连接的资源(如连接到应用的 MySQL 数据库)的管理工具。Management tools for connected resources such as a MySQL database connected to an app.

2.收集数据2. Collect data

应用服务为 Web 服务器和 Web 应用程序中的日志记录信息提供诊断功能。App Service provides diagnostic functionality for logging information from both the web server and the web application. 此信息分为 Web 服务器诊断和应用程序诊断。The information is separated into web server diagnostics and application diagnostics.

启用 Web 服务器诊断Enable web server diagnostics

可以启用或禁用以下种类的日志:You can enable or disable the following kinds of logs:

  • 详细错误日志记录 - 指示故障的 HTTP 状态代码(状态代码 400 或更大数字)的详细错误消息。Detailed Error Logging - Detailed error information for HTTP status codes that indicate a failure (status code 400 or greater). 其中可能包含有助于确定服务器返回错误代码的原因的信息。This may contain information that can help determine why the server returned the error code.
  • 失败请求跟踪 - 有关失败请求的详细信息,包括对用于处理请求的 IIS 组件和每个组件所用的时间的跟踪。Failed Request Tracing - Detailed information on failed requests, including a trace of the IIS components used to process the request and the time taken in each component. 在尝试提高应用性能或查找导致特定 HTTP 错误的问题时,此信息很有用。This can be useful if you are attempting to improve app performance or isolate what is causing a specific HTTP error.
  • Web 服务器日志记录 - 使用 W3C 扩展日志文件格式的 HTTP 事务信息。Web Server Logging - Information about HTTP transactions using the W3C extended log file format. 这在确定总体应用指标(如处理的请求数量或来自特定 IP 地址的请求数)时非常有用。This is useful when determining overall app metrics, such as the number of requests handled or how many requests are from a specific IP address.

启用应用程序诊断Enable application diagnostics

可通过多种选项收集应用服务中的应用程序性能数据、从 Visual Studio 中实时分析应用程序,或修改应用程序代码以记录更多信息和跟踪。There are several options to collect application performance data from App Service, profile your application live from Visual Studio, or modify your application code to log more information and traces. 可以基于针对应用程序的访问权限和通过监视工具观察到的内容选择相关选项。You can choose the options based on how much access you have to the application and what you observed from the monitoring tools.

手动设置诊断跟踪Set up diagnostic traces manually

如果有权访问 Web 应用程序源代码,使用应用程序诊断可以捕获 Web 应用程序生成的信息。If you have access to the web application source code, Application diagnostics enables you to capture information produced by a web application. ASP.NET 应用程序可使用 System.Diagnostics.Trace 类将信息记录到应用程序诊断日志。ASP.NET applications can use the System.Diagnostics.Trace class to log information to the application diagnostics log. 但是需要更改代码并重新部署应用程序。However, you need to change the code and redeploy your application. 如果应用在测试环境中运行,则推荐使用此方法。This method is recommended if your app is running on a testing environment.

有关如何在应用程序中配置日志记录的详细说明,请参阅在 Azure 应用服务中启用应用的诊断日志记录For detailed instructions on how to configure your application for logging, see Enable diagnostics logging for apps in Azure App Service.

使用 Kudu 调试控制台Use the Kudu Debug Console

应用服务随附可用于调试、浏览和上传文件的调试控制台,以及用于获取环境相关信息的 JSON 终结点。App Service comes with a debug console that you can use for debugging, exploring, uploading files, as well as JSON endpoints for getting information about your environment. 此控制台称为应用的 Kudu 控制台或 SCM 仪表板。 This console is called the Kudu Console or the SCM Dashboard for your app.

转到链接 https://<Your app name>.scm.chinacloudsites.cn/ 即可访问此仪表板。You can access this dashboard by going to the link https://<Your app name>.scm.chinacloudsites.cn/.

Kudu 提供的一些信息和功能包括:Some of the things that Kudu provides are:

  • 应用程序的环境设置environment settings for your application
  • 日志流log stream
  • 诊断转储diagnostic dump
  • 调试控制台,可以在其中运行 Powershell cmdlet 和基本 DOS 命令。debug console in which you can run Powershell cmdlets and basic DOS commands.

Kudu 的另一项有用功能是,如果应用程序引发第一次异常,可以使用 Kudu 和 SysInternals 工具 Procdump 创建内存转储。Another useful feature of Kudu is that, in case your application is throwing first-chance exceptions, you can use Kudu and the SysInternals tool Procdump to create memory dumps. 这些内存转储是进程的快照,通常可帮助排查较复杂的应用问题。These memory dumps are snapshots of the process and can often help you troubleshoot more complicated issues with your app.

有关 Kudu 提供的功能的详细信息,请参阅你应该了解的 Azure DevOps 工具For more information on features available in Kudu, see Azure DevOps tools you should know about.

3.缓解问题3. Mitigate the issue

缩放应用Scale the app

在 Azure 应用服务中,为了提高性能和吞吐量,可以调整运行应用程序的规模。In Azure App Service, for increased performance and throughput, you can adjust the scale at which you are running your application. 纵向扩展应用涉及到两个相关操作:将应用服务计划更改为较高的定价层,以及在切换到较高的定价层后配置特定的设置。Scaling up an app involves two related actions: changing your App Service plan to a higher pricing tier, and configuring certain settings after you have switched to the higher pricing tier.

有关缩放的详细信息,请参阅缩放 Azure 应用服务中的应用For more information on scaling, see Scale an app in Azure App Service.

此外,可以选择在多个实例上运行应用程序。Additionally, you can choose to run your application on more than one instance. 扩展不仅能提供更强大的处理功能,而且还能提供一定程度的容错。Scaling out not only provides you with more processing capability, but also gives you some amount of fault tolerance. 如果进程在某个实例上中断,其他实例会继续处理请求。If the process goes down on one instance, the other instances continue to serve requests.

可以将缩放设置为手动或自动。You can set the scaling to be Manual or Automatic.

使用 AutoHealUse AutoHeal

AutoHeal 会根据所选设置(例如配置更改、请求、基于内存的限制或执行请求所需的时间)回收应用的工作进程。AutoHeal recycles the worker process for your app based on settings you choose (like configuration changes, requests, memory-based limits, or the time needed to execute a request). 在大多数情况下,回收进程是在出现问题后进行恢复的最快方式。Most of the time, recycle the process is the fastest way to recover from a problem. 尽管始终都可从 Azure 门户中直接重启应用,但 AutoHeal 可以自动执行此操作。Though you can always restart the app from directly within the Azure portal, AutoHeal does it automatically for you. 只需在应用的根 web.config 中添加一些触发器即可。All you need to do is add some triggers in the root web.config for your app. 即使应用程序并非 .NET 应用,这些设置的工作方式也仍然相同。These settings would work in the same way even if your application is not a .NET app.

有关详细信息,请参阅 自动修复 Azure 网站For more information, see Auto-Healing Azure Web Sites.

重启应用Restart the app

重启通常是在发生一次性问题后进行恢复的最简单方式。Restarting is often the simplest way to recover from one-time issues. Azure 门户的应用边栏选项卡中提供了用于停止或重启应用的选项。On the Azure portal, on your app's blade, you have the options to stop or restart your app.

重启应用以解决性能问题

还可以使用 Azure Powershell 管理应用。You can also manage your app using Azure Powershell. 有关详细信息,请参阅将 Azure PowerShell 与 Azure 资源管理器配合使用For more information, see Using Azure PowerShell with Azure Resource Manager.