找出您的 PHP 代码在何处变慢(性能问题)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/55720/
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-08-24 21:19:52  来源:igfitidea点击:

Find out where your PHP code is slowing down (Performance Issue)

phpmysqlperformanceprofiling

提问by Rushi

Here's my first question at SO.

这是我在 SO 的第一个问题。

I have a internal application for my company which I've been recently ask to maintain. The applications is built in PHP and its fairly well coded (OO, DB Abstraction, Smarty) nothing WTF-ish.

我最近要求维护我公司的内部应用程序。这些应用程序是用 PHP 构建的,它的编码相当不错(OO、DB Abstraction、Smarty),没有什么是 WTF 风格的。

The problem is the applications is very slow.

问题是应用程序很慢

How do I go about finding out what's slowing the application down? I've optimized the code to make very few DB queries, so I know that it is the PHP code which is taking a while to execute. I need to get some tools which can help me with this and need to devise a strategy for checking my code.

我如何去找出导致应用程序变慢的原因?我已经优化了代码以进行很少的数据库查询,所以我知道是 PHP 代码需要一段时间才能执行。我需要一些工具来帮助我解决这个问题,并且需要设计一个策略来检查我的代码。

I can do the checking/strategy work myself, but I need more PHP tools to figure out where my app is crapping up.

我可以自己做检查/策略工作,但我需要更多 PHP 工具来找出我的应用程序出错的地方。

Thoughts?

想法?

回答by Juan

I've used XDebug profilingrecently in a similiar situation. It outputs a full profile report that can be read with many common profiling apps ( Can't give you a list though, I just used the one that came with slackware ).

我最近在类似的情况下使用了XDebug 分析。它输出一个完整的配置文件报告,可以用许多常见的配置文件应用程序读取(虽然不能给你一个列表,我只是使用了 slackware 附带的一个)。

回答by MattBelanger

As Juan mentioned, xDebug is excellent. If you're on Windows, WinCacheGrindwill let you look over the reports.

正如 Juan 所提到的,xDebug 非常出色。如果您使用的是 Windows,WinCacheGrind将让您查看报告。

回答by Patrick Hogan

Watch this presentation by Rasmus Lerdorf (creator of PHP). He goes into some good examples of testing PHP speed and what to look for as well as some internals that can slow things down. XDebug is one tool he uses. He also makes a very solid point about knowing what performance cost you're getting into with frameworks.

观看 Rasmus Lerdorf(PHP 的创建者)的演示。他介绍了一些很好的例子来测试 PHP 速度和寻找什么以及一些可以减慢速度的内部结构。XDebug 是他使用的一种工具。他还就了解框架的性能成本提出了非常明确的观点。

Video: http://www.archive.org/details/simple_is_hard

视频:http: //www.archive.org/details/simple_is_hard

Slides (since it's hard to see on the video): http://talks.php.net/show/drupal08/1

幻灯片(因为很难在视频中看到):http: //talks.php.net/show/drupal08/1

回答by giltotherescue

There are many variables that can impact your application's performance. I recommend that you do not instantly assume PHP is the problem.

有许多变量会影响应用程序的性能。我建议您不要立即假设 PHP 是问题所在。

First, how are you serving PHP? Have you tried basic optimization of Apache or IIS itself? Is the server busy processing other kinds of requests? Have you taken advantage of a PHP code accelerator? One way to test whether the server is your bottleneck is to try running the application on another server.

首先,您如何为 PHP 提供服务?您是否尝试过 Apache 或 IIS 本身的基本优化?服务器是否忙于处理其他类型的请求?您是否利用过PHP 代码加速器?测试服务器是否是瓶颈的一种方法是尝试在另一台服务器上运行应用程序。

Second, is performance of the entire application slow, or does it only seem to affect certain pages? This could give you an indication of where to start analyzing performance. If the entire application is slow, the problem is more likely in the underlying server/platform or with a global SQL query that is part of every request (user authentication, for example).

其次,整个应用程序的性能是否很慢,或者它似乎只影响某些页面?这可以为您指明从哪里开始分析性能。如果整个应用程序很慢,则问题更有可能出现在底层服务器/平台或作为每个请求(例如用户身份验证)一部分的全局 SQL 查询中。

Third, you mentioned minimizing the number of SQL queries, but what about optimizing the existing queries? If you are using MySQL, are you taking advantage of the various strengths of each storage system? Have you run EXPLAINon your most important queries to make sure they are properly indexed? This is critical on queries that access big tables; the larger the dataset, the more you will notice the effects of poor indexing. Luckily, there are many articles such as this onewhich explain how to use EXPLAIN.

第三,您提到了尽量减少 SQL 查询的数量,但是如何优化现有查询呢?如果您使用 MySQL,您是否利用了每个存储系统的各种优势?您是否对最重要的查询运行了EXPLAIN以确保它们被正确索引?这对于访问大表的查询至关重要;数据集越大,您就越会注意到不良索引的影响。幸运的是,有很多文章(例如这篇文章)解释了如何使用 EXPLAIN。

Fourth, a common mistake is to assume that your database server will automatically use all of the resources available to the system. You should check to make sure you have explicitly allocated sufficient resources to your database application. In MySQL, for example, you'll want to add custom settings (in your my.cnf file) for things like key buffer, temp table size, thread concurrency, innodb buffer pool size, etc.

第四,一个常见的错误是假设您的数据库服务器会自动使用系统可用的所有资源。您应该检查以确保已为数据库应用程序明确分配了足够的资源。例如,在 MySQL 中,您需要为键缓冲区、临时表大小、线程并发性、innodb 缓冲池大小等内容添加自定义设置(在 my.cnf 文件中)。

If you've double-checked all of the above and are still unable to find the bottleneck, a code profiler like Xdebug can definitely help. Personally, I prefer the Zend Studio profiler, but it may not be the best option unless you are already taking advantage of the rest of the Zend Platform stack. However, in my experience it is very rare that PHP itself is the root cause of slow performance. Often, a code profiler can help you determine with more precision which DB queries are to blame.

如果您已经仔细检查了以上所有内容,但仍然无法找到瓶颈,那么像 Xdebug 这样的代码分析器绝对可以提供帮助。就个人而言,我更喜欢 Zend Studio 分析器,但它可能不是最佳选择,除非您已经在利用 Zend 平台堆栈的其余部分。但是,根据我的经验,PHP 本身是性能缓慢的根本原因是非常罕见的。通常,代码分析器可以帮助您更精确地确定应该归咎于哪些数据库查询。

回答by Fedir RYKHTIK

Also You could use APD(Advanced PHP Debugger).

您也可以使用APD(高级 PHP 调试器)。

It's quite easy to make it work.

让它工作很容易。

$ php apd-test.php

$ pprofp -l pprof.SOME_PID

Trace for /Users/martin/develop/php/apd-test/apd-test.php
Total Elapsed Time = 0.12
Total System Time  = 0.01
Total User Time    = 0.07


         Real         User        System             secs/    cumm
%Time (excl/cumm)  (excl/cumm)  (excl/cumm) Calls    call    s/call  Memory Usage Name
--------------------------------------------------------------------------------------
71.3 0.06 0.06  0.05 0.05  0.01 0.01  10000  0.0000   0.0000            0 in_array
27.3 0.02 0.09  0.02 0.07  0.00 0.01  10000  0.0000   0.0000            0 my_test_function
 1.5 0.03 0.03  0.00 0.00  0.00 0.00      1  0.0000   0.0000            0 apd_set_pprof_trace
 0.0 0.00 0.12  0.00 0.07  0.00 0.01      1  0.0000   0.0000            0 main

There is a nice tutorial how to compile APD and make profiling with it : http://martinsikora.com/compiling-apd-for-php-54

有一个很好的教程如何编译 APD 并使用它进行分析:http: //martinsikora.com/compiling-apd-for-php-54

回答by Alister Bulman

Xdebug profile is definitely the way to go. Another tip - WincacheGrind is good, but not been updated recently. http://code.google.com/p/webgrind/- in a browser may be an easy and quick alternative.

Xdebug 配置文件绝对是要走的路。另一个提示 - WincacheGrind 很好,但最近没有更新。 http://code.google.com/p/webgrind/- 在浏览器中可能是一种简单快捷的替代方法。

Chances are though, it's still the database anyway. Check for relevant indexes - and that it has sufficient memory to cache as much of the working data as possible.

不过有可能,它仍然是数据库。检查相关索引 - 并且它有足够的内存来缓存尽可能多的工作数据。

回答by RobS

phpED (http://www.nusphere.com/products/phped.htm) also offers great debugging and profiling, and the ability to add watches, breakpoints, etc in PHP code. The integrated profiler directly offers a time breakdown of each function call and class method from within the IDE. Browser plugins also enable quick integration with Firefox or IE (i.e. visit slow URL with browser, then click button to profile or debug).

phpED ( http://www.nusphere.com/products/phped.htm) 还提供了很好的调试和分析,以及在 PHP 代码中添加监视、断点等的能力。集成的分析器直接从 IDE 中提供每个函数调用和类方法的时间细分。浏览器插件还支持与 Firefox 或 IE 的快速集成(即使用浏览器访问慢速 URL,然后单击按钮进行配置或调试)。

It's been very useful in pointing out where the app is slow in order to concentrate most coding effort; and it avoids wasting time optimising already fast code. Having tried Zend and Eclipse, I've now been sold on the ease of use of phpED.

它在指出应用程序缓慢的地方以集中大部分编码工作方面非常有用;它避免了浪费时间优化已经很快的代码。尝试过 Zend 和 Eclipse 后,我现在已经被 phpED 的易用性所吸引。

Bear in mind both Xdebug and phpED (with DBG) will require an extra PHP module installed when debugging against a webserver. phpED also offers (untried by me) a local debugging option too.

请记住,在针对 Web 服务器进行调试时,Xdebug 和 phpED(带 DBG)都需要安装额外的 PHP 模块。phpED 还提供(我没有尝试过)本地调试选项。

回答by SeanDowney

you can also try using the register_tick_function function in php. which tells php to call a certain function periodcally through out your code. You could then keep track of which function is currently running and the amount of time between calls. then you could see what's taking the most time. http://www.php.net/register_tick_function

您也可以尝试在 php 中使用 register_tick_function 函数。它告诉 php 在你的代码中定期调用某个函数。然后,您可以跟踪当前正在运行的函数以及调用之间的时间量。然后你就可以看到什么花费的时间最多。 http://www.php.net/register_tick_function

回答by Brendon-Van-Heyzen

ifs its a large code base try apc if you're not already.

如果它是一个大型代码库,如果您还没有尝试 apc。

http://pecl.php.net/package/APC

http://pecl.php.net/package/APC

回答by Mark

We use Zend Development Environment (windows). We resolved a memory usage spike yesterday by stepping through the debugger while running Process Explorer to watch the memory/cpu/disk activity as each line was executed.

我们使用 Zend 开发环境(windows)。昨天,我们通过在运行 Process Explorer 的同时逐步调试调试器来观察内存/CPU/磁盘活动,从而解决了内存使用高峰的问题,因为每一行都被执行了。

Process Explorer: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx.

进程资源管理器:http: //technet.microsoft.com/en-us/sysinternals/bb896653.aspx

ZDE includes a basic performance profiler that can show time spent in each function call during page requests.

ZDE 包含一个基本的性能分析器,可以显示页面请求期间每个函数调用所花费的时间。