简体   繁体   English

找出PHP代码减速的位置(性能问题)

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

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-ish。

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. 我已经优化了代码,只进行了很少的数据库查询,所以我知道这是需要一段时间才能执行的PHP代码。 I need to get some tools which can help me with this and need to devise a strategy for checking my code. 我需要一些可以帮助我的工具,并且需要设计一个检查我的代码的策略。

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? 思考?

I've used XDebug profiling recently in a similiar situation. 我最近在一个类似的情况下使用过XDebug分析 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 ). 它输出一个完整的配置文件报告,可以使用许多常见的配置文件应用程序读取(虽然不能给你一个列表,我只使用了slackware附带的那个)。

As Juan mentioned, xDebug is excellent. 正如Juan所说,xDebug很棒。 If you're on Windows, WinCacheGrind will let you look over the reports. 如果你在Windows上, WinCacheGrind会让你查看报告。

Watch this presentation by Rasmus Lerdorf (creator of PHP). 观看Rasmus Lerdorf(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. 他介绍了一些测试PHP速度和寻找内容的好例子,以及一些可以减慢速度的内部组件。 XDebug is one tool he uses. XDebug是他使用的一种工具。 He also makes a very solid point about knowing what performance cost you're getting into with frameworks. 他还非常清楚地了解您使用框架获得的性能成本。

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

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? 首先,你如何服务PHP? Have you tried basic optimization of Apache or IIS itself? 您是否尝试过Apache或IIS本身的基本优化? Is the server busy processing other kinds of requests? 服务器是否忙于处理其他类型的请求? Have you taken advantage of a PHP code accelerator ? 你有没有利用PHP代码加速器 One way to test whether the server is your bottleneck is to try running the application on another server. 测试服务器是否是您的瓶颈的一种方法是尝试在另一台服务器上运行该应用程序。

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? 第三,您提到最小化SQL查询的数量,但是优化现有查询呢? If you are using MySQL, are you taking advantage of the various strengths of each storage system? 如果您使用的是MySQL,您是否正在利用每个存储系统的各种优势? Have you run EXPLAIN on your most important queries to make sure they are properly indexed? 您是否对最重要的查询运行EXPLAIN以确保它们已正确编入索引? 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 one which explain how to use 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. 如果您已经仔细检查了上述所有内容并且仍然无法找到瓶颈,那么像Xdebug这样的代码分析器肯定会有所帮助。 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. 就个人而言,我更喜欢Zend Studio分析器,但它可能不是最好的选择,除非你已经利用了Zend Platform堆栈的其余部分。 However, in my experience it is very rare that PHP itself is the root cause of slow performance. 但是,根据我的经验,PHP本身很少是导致性能下降的根本原因。 Often, a code profiler can help you determine with more precision which DB queries are to blame. 通常,代码分析器可以帮助您更准确地确定哪些数据库查询应该受到责备。

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

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. phpED( http://www.nusphere.com/products/phped.htm )还提供了出色的调试和分析功能,以及在PHP代码中添加监视,断点等功能。 The integrated profiler directly offers a time breakdown of each function call and class method from within the IDE. 集成的分析器直接在IDE中提供每个函数调用和类方法的时间细分。 Browser plugins also enable quick integration with Firefox or IE (ie visit slow URL with browser, then click button to profile or debug). 浏览器插件还可以与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. 请记住,在对Web服务器进行调试时,Xdebug和phpED(使用DBG)都需要安装额外的PHP模块。 phpED also offers (untried by me) a local debugging option too. phpED也提供(未经我未尝试过)本地调试选项。

Xdebug profile is definitely the way to go. Xdebug简介绝对是您要走的路。 Another tip - WincacheGrind is good, but not been updated recently. 另一个提示 - WincacheGrind很好,但最近没有更新。 http://code.google.com/p/webgrind/ - in a browser may be an easy and quick alternative. 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. 检查相关索引 - 并且它有足够的内存来尽可能多地缓存工作数据。

ifs its a large code base try apc if you're not already. ifs是一个很大的代码库,如果你还没有尝试apc。

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

you can also try using the register_tick_function function in php. 你也可以尝试在php中使用register_tick_function函数。 which tells php to call a certain function periodcally through out your code. 它告诉php在你的代码中调用某个函数。 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 http://www.php.net/register_tick_function

You can also look at the HA Proxy or any other load balancing solution if your server degraded performance is the cause of the application slow processing. 如果服务器性能下降是导致应用程序处理速度缓慢的原因,您还可以查看HA代理或任何其他负载平衡解决方案。 server. 服务器。

We use Zend Development Environment (windows). 我们使用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. 我们昨天解决了内存使用率峰值,在运行Process Explorer时逐步调试调试器,以便在执行每一行时观察内存/ CPU /磁盘活动。

Process Explorer: http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx . Process Explorer: 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包括一个基本的性能分析器,可以显示页面请求期间每个函数调用所花费的时间。

I use a combination of PEAR Benchmark and log4php . 我使用PEAR Benchmarklog4php的组合。

At the top of scripts I want to profile I create an object that wraps around a Benchmark_Timer object. 在我想要配置文件的顶部,我创建了一个包装Benchmark_Timer对象的对象。 Throughout the code, I add in $object->setMarker("name"); 在整个代码中,我添加了$object->setMarker("name"); calls, especially around suspect code. 调用,尤其是可疑代码。

The wrapper class has a destroy method that takes the logging information and writes it to log4php. 包装类有一个destroy方法,它接收日志信息并将其写入log4php。 I typically send this to syslog (many servers, aggregates to one log file on one server). 我通常将此发送到syslog(许多服务器,聚合到一个服务器上的一个日志文件)。

In debug, I can watch the log files and see where I need to improve things. 在调试中,我可以查看日志文件,看看我需要改进的地方。 Later on in production, I can parse the log files and do performance analysis. 稍后在生产中,我可以解析日志文件并进行性能分析。

It's not xdebug, but it's always on and gives me the ability to compare any two executions of the code. 它不是xdebug,但它始终打开并且让我能够比较代码的任何两次执行。

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

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