简体   繁体   English

如何在PHP中追踪慢速代码

[英]How to track down slow code in PHP

I installed an open source PHP project on my Ubuntu server and got it running. 我在我的Ubuntu服务器上安装了一个开源PHP项目并使其运行。 However, pages load extremely slow. 但是,页面加载速度极慢。 A phpinfo() page and a basic test page connecting to the mysql db all load very fast. phpinfo()页面和连接到mysql db的基本测试页面加载速度非常快。 There are tons of include files, log files and some curl calls in this open source code. 这个开源代码中有大量的包含文件,日志文件和一些curl调用。 What strategies/tools can I use to track down the source of the slowness? 我可以使用哪些策略/工具来追踪缓慢的来源?

You can use xdebug and run it through the profiler. 您可以使用xdebug并通过分析器运行它。 You can visualize the profiler results via webgrind. 您可以通过webgrind可视化分析器结果。

The following tutorials and tools will be useful: 以下教程和工具将非常有用:

You need to profile pages, that have performance problems. 您需要对具有性能问题的页面进行概要分析。 It can be done with php-extension xdebug and kcachegrind . 它可以用php-extension xdebugkcachegrind完成

Follow this steps: 请遵循以下步骤:

  1. Install php5-xdebug package on your Ubuntu server via: sudo apt-get install php5-xdebug . 通过以下方式在您的Ubuntu服务器上安装php5-xdebug软件包: sudo apt-get install php5-xdebug
  2. Configure xdebug.profiler_enable_trigger parameter. 配置xdebug.profiler_enable_trigger参数。 Look at official manual . 官方手册
  3. Install on your local computer kcachegrind package: sudo apt-get install kcachegrind . 在本地计算机上安装kcachegrind包: sudo apt-get install kcachegrind
  4. Start slow page with GET parameter XDEBUG_PROFILE=1 . 使用GET参数XDEBUG_PROFILE=1启动慢速页面。 See related question . 相关问题
  5. Copy generated profile log to your local comp and open it in kcachegrind. 将生成的配置文件日志复制到本地comp并在kcachegrind中打开它。

Some notes: 一些说明:

  • After php5-xdebug package installation you'll need to restart http-server (or php-fpm daemon. Depends on installed software on your server). 在php5-xdebug软件包安装之后,您需要重新启动http-server(或php-fpm守护程序。取决于您服务器上安装的软件)。
  • Right place for profiling procedure is a test environment, not production. 分析过程的正确位置是测试环境,而不是生产。

Install debug kit 安装调试工具包

The definitive answer is to use xdebug profiling, which gives you a great amount of information as to what's happening. 明确的答案是使用xdebug分析,它可以为您提供有关正在发生的事情的大量信息。 There is however also DebugKit . 但是还有DebugKit

DebugKit is a must for any CakePHP developer, and is very easy to install (it's just a plugin). DebugKit是任何CakePHP开发人员必须的,并且非常容易安装(它只是一个插件)。

The feature most relevant to the question is the timer panel: 与问题最相关的功能是计时器面板:

在此输入图像描述

Knowing which part of the request is slow, will permit you to focus your attention on where the problem is. 知道请求的哪一部分很慢,将允许您将注意力集中在问题所在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM