php xhprof
While patience is a virtue, it’s generally not one inherent to today’s society when it comes to waiting for web pages to load. Studies conducted by big names in technology like Google, Microsoft, and Yahoo! have shown that even sub-second differences in page load time can have significant impact on conversions. If that’s not enough reason, Google incorporated page load time into their search rankings back in April 2010, so it also impacts SEO.
虽然耐心是一种美德,但在等待网页加载时,耐心通常并不是当今社会固有的。 由知名人士进行的研究,例如Google,Microsoft和Yahoo! 已经表明,即使页面加载时间不足一秒,也会对转化产生重大影响。 如果这还不够,Google会在2010年4月将网页加载时间纳入其搜索排名,因此也会影响SEO。
A number of factors can affect page load time: network latency, web server configuration or load, use of persistent storage, caching or lack thereof, and application response time. In general, application response time should be suspected and dealt with last as it’s a less common culprit than database servers or handling of static assets and efforts to tune it usually have a lower ROI. However, knowing how to locate issues affecting application performance – often called bottlenecks – is a good skill to have should you ever be required to do so.
许多因素会影响页面加载时间:网络延迟,Web服务器配置或负载,持久性存储的使用,缓存或缺少持久性存储以及应用程序响应时间。 通常,应该怀疑应用程序响应时间,并且应将其最后处理,因为它比数据库服务器或静态资产处理和调整它的努力不那么常见,因此投资回报率通常较低。 但是,如果需要,知道如何查找影响应用程序性能的问题(通常称为瓶颈)是一项很好的技能。
Profiling is the process of measuring the execution time of each function or method call involved in servicing a single page request. The data collected from this process can reveal issues such as a particular method taking a long time to execute or being called a large number of times.
分析是测量服务单个页面请求所涉及的每个函数或方法调用的执行时间的过程。 从此过程中收集的数据可能会显示一些问题,例如执行时间较长或被调用多次的特定方法。
Profiling a PHP script generally requires installing a PHP extension. The two more popular extensions for this purpose are Derick Rethans‘ Xdebug and Facebook’s XHProf. This article will focus on XHProf.
对PHP脚本进行性能分析通常需要安装PHP扩展。 为此目的,另外两个比较流行的扩展是Derick Rethans的Xdebug和Facebook的XHProf 。 本文将重点介绍XHProf。
XHProf is available via PECL, so installing it is pretty easy. For example, it can be installed with the following on Debian-based systems:
XHProf可通过PECL获得,因此安装非常简单。 例如,可以将其与以下产品一起安装在基于Debian的系统上:
sudo pecl install xhprof-beta echo 'extension=xhprof.so' | sudo tee /etc/php5/conf.d/xhprof.ini >/dev/null sudo apachectl restartThe raw profiling data generated by XHProf isn’t easily consumed at a glance. As such, an interface or visualization of some sort can be useful to view it. XHGui, a fork of XHProf, provides this.
XHProf生成的原始性能分析数据一目了然。 这样,某种界面或可视化视图对于查看它很有用。 XHProf的分支XHGui提供了此功能。
XHGui is hosted on GitHub, so you have two installation options. The first option involves using git to clone the repository, which provides an easy way to update it later.
XHGui托管在GitHub上,因此您有两个安装选项。 第一个选项涉及使用git克隆存储库,这提供了一种稍后更新它的简便方法。
git clone git://github.com/preinheimer/xhprof.gitIf you prefer not to use git, the second option is to simply download and extract a tarball reflecting the repository’s current state like so:
如果您不想使用git,则第二个选择是简单地下载并解压缩反映存储库当前状态的tarball,如下所示:
wget -O xhgui.tar.gz https://github.com/preinheimer/xhprof/tarball/master && tar -zxf xhgui.tar.gzAt this point, you can use directions in the INSTALL file to guide you through the rest of the process for your particular server environment. The xhprof_html directory contains the index.php file to load the XHGui interface, so make sure your web server is configured to expose that path within your host’s document root using a symbolic link, an Alias directive, or something of that sort.
此时,您可以使用INSTALL文件中的指示来指导您完成针对特定服务器环境的其余过程。 xhprof_html目录包含用于加载XHGui界面的index.php文件,因此请确保将您的Web服务器配置为使用符号链接,Alias指令或类似形式在主机文档根目录下公开该路径。
SQL to create the database table used for storing profiling data is located in comment blocks at the top of the PHP files in the xhprof_lib/utils/Db directory. Use the file appropriate for your environment’s database server to create that database table, then add credentials to access it to the XHGui config.php file.
用于创建用于存储概要分析数据的数据库表SQL位于xhprof_lib/utils/Db目录中PHP文件顶部的注释块中。 使用适合您环境的数据库服务器的文件创建该数据库表,然后添加凭据以将其访问XHGui config.php文件。
If your web server is not running on your local host, don’t forget to add your local host’s IP address to the $controlIPs variable in the XHGui config.php file, else you’ll get an error message stating “You do not have permission to view this page.”
如果您的Web服务器不在本地主机上运行,请不要忘记将本地主机的IP地址添加到XHGui config.php文件中的$controlIPs变量中,否则您将收到一条错误消息,指出“您没有有权查看此页面。”
Once all this is in place, XHGui should load successfully when you browse to it.
完成所有这些操作后,XHGui应该会在您浏览到它时成功加载。
For more information on other configuration settings offered by XHGui, see the PHP Advent blog post linked at the bottom of the INSTALL file.
有关XHGui提供的其他配置设置的更多信息, 请参阅 INSTALL文件底部链接PHP Advent博客文章 。
If you’re running XHGui in a development environment, one place where you can vary slightly from the instructions is in the directives to configure the auto_prepend_file PHP configuration setting. According to the PHP manual, this setting is configurable per directory, meaning you can use a .htaccess file rather than having to modify your virtual host configuration and bounce your web server if your application has a central endpoint for servicing requests. An example of this is shown below.
如果您在开发环境中运行XHGui,则可以在指令中稍有不同的地方是用于配置auto_prepend_file PHP配置设置的指令。 根据PHP手册 ,此设置可在每个目录中配置,这意味着您可以使用.htaccess文件,而无需修改虚拟主机配置并在应用程序具有用于服务请求的中央端点的情况下启动Web服务器。 下面是一个示例。
php_value auto_prepend_file "../../xhprof/external/header.php"This setting executes the specified PHP file before any PHP file is served. This particular file enables collection of profiling data before the code to be profiled is executed and sets up a shutdown function to store that data once the code has completed its execution.
此设置将在提供任何PHP文件之前执行指定PHP文件。 该特定文件可以在执行要分析的代码之前收集概要分析数据,并设置关闭功能以在代码完成执行后存储该数据。
For the purposes of this article, the skeleton example application provided for use with Zend Framework 2 is used as the subject of profiling. The .htaccess file above is placed in that application’s public directory, which is configured as the host document root. To enable profiling for a particular URL, append _profile=1 to its query string. For example, the environment used in this article uses a hostname of zf2.local, making the URL to access the application with profiling enabled http://zf2.local/?_profile=1. This will set a cookie and then redirect back to the application with that query string variable removed; with the cookie in place, you won’t need to modify the query string to enable profiling for subsequent requests.
出于本文的目的,将与Zend Framework 2一起使用的框架示例应用程序用作概要分析的主题。 上面的.htaccess文件位于该应用程序的公共目录中,该目录配置为主机文档根目录。 要启用对特定URL的概要分析,请将_profile=1附加到其查询字符串中。 例如,本文中使用的环境使用主机名zf2.local ,使URL能够在启用概要分析的情况下访问应用程序http://zf2.local/?_profile=1 。 这将设置一个cookie,然后重定向到删除了该查询字符串变量的应用程序; 有了Cookie后,您无需修改查询字符串即可对后续请求进行分析。
Once the application request has completed, the profiling data will be stored in the database you configured XHGui to use. Simply refresh the XHGui interface and its data grid will list the new profiling run near the top. The data grid also has links for easy filtering of runs by time period, memory usage, runtime, server, and domain.
一旦应用程序请求完成,分析数据将存储在您配置为XHGui使用的数据库中。 只需刷新XHGui界面,其数据网格就会在顶部附近列出新的性能分析。 数据网格还具有链接,可轻松按时间段,内存使用量,运行时,服务器和域过滤运行。
Click the timestamp listed for a run and XHGui will display a page detailing the data collected during that run. The table on the top left displays aggregated statistics such as total runtime, memory usage, and function call count. The top right shows tables of GET, POST, and cookie data as well as a pie chart detailing expensive function calls made during the request.
单击列出的运行时间戳,XHGui将显示一个页面,详细说明该运行期间收集的数据。 左上方的表格显示汇总的统计信息,例如总运行时间,内存使用情况和函数调用计数。 右上角显示GET,POST和Cookie数据表,以及详细说明在请求期间进行的昂贵函数调用的饼图。
Scroll down and you’ll find a data grid listing functions and their corresponding statistics. If you’re profiling for runtime, the two columns you’ll generally want to focus on are labeled “Exclusive Wall Time” and “Call Count.” For memory usage, look at the columns labeled “Exclusive Memory Usage” and “Exclusive Peak Memory” Usage. Click on any grid column to sort the grid’s data by that column. Click on an individual function name to view a detail page that includes that function’s statistics as well as those of its parent functions (i.e. functions that call it) and child functions (i.e. functions that it calls).
向下滚动,您会发现一个列出功能及其相应统计信息的数据网格。 如果要对运行时进行性能分析,则通常需要重点关注的两列分别标记为“独占时间”和“通话次数”。 有关内存使用情况,请查看标有“专有内存使用情况”和“专有峰值内存”使用情况的列。 单击任何网格列,以该列对网格的数据进行排序。 单击单个函数名称以查看详细信息页面,该页面包含该函数及其父函数(即调用它的函数)和子函数(即它调用的函数)的统计信息。
The common use case for profiling is to profile an application, make a change to that application’s source code, and then profile it again to see what performance impact the change made. XHProf and XHGui were designed to make doing this fairly easy.
概要分析的常见用例是对应用程序进行概要分析,对该应用程序的源代码进行更改,然后再次对其进行概要分析,以查看所做更改对性能有何影响。 XHProf和XHGui的设计使此操作相当容易。
Within XHGui, in the data grid for profiling runs, each run has a hash value shown underneath its timestamp. Choose one of two runs you’d like to compare and copy its hash value. Click the timestamp of the other run to bring up its detail page. Finally, at the bottom of the data table on the top left, enter the hash value into the text box labeled “Perform Delta” and hit the “Delta” button next to it.
在XHGui中,在用于分析运行的数据网格中,每个运行在其时间戳下方显示一个哈希值。 选择您要比较并复制其哈希值的两个运行之一。 单击另一个运行的时间戳以显示其详细信息页面。 最后,在左上角数据表的底部,将哈希值输入标有“ Perform Delta”的文本框中,然后单击旁边的“ Delta”按钮。
This displays a page where the run for which you entered the hash value is labeled “Run Two” and the run for which you viewed the detail page is labeled “Run One.” Like the detail page, it displays both aggregate statistics and statistics per function call. These function call statistics are computed by subtracting those for Run One from those for Run Two. For example, the “Wall Time” column value for the main() function row in the function statistics table corresponds to the difference of the values of the “Incl. Wall Time” cell for Run Two and Run One in the aggregated statistics tables.
这将显示一个页面,其中您为其输入哈希值的运行被标记为“运行两次”,而您查看其详细信息页面的运行被标记为“运行一次”。 与详细信息页面一样,它同时显示汇总统计信息和每个函数调用的统计信息。 这些函数调用统计信息是通过从运行二的统计值中减去运行一的统计值而得出的。 例如,函数统计信息表中main()函数行的“ Wall Time”列值对应于“ Incl。 汇总统计信息表中“运行2”和“运行1”的“时间间隔”单元。
It’s possible that you may profile an application twice without making any changes and see a significant difference in the two profiles. One possible reason for this is that PHP maintains its own stat cache of filesystem lookups to improve performance. To mitigate this difference in xhprof profiling runs, add a call to the clearstatcache function at the top of external/header.php in your XHGui installation.
您有可能两次对应用程序进行概要分析而没有进行任何更改,并且看到两个概要文件之间存在显着差异。 原因之一可能是PHP维护了自己的文件系统查找状态缓存以提高性能。 为了缓解xhprof分析运行中的这种差异,请在XHGui安装的external / header.php顶部添加对clearstatcache函数的调用。
Another feature offered by XHGui is generation of callgraphs, directed graphs that show calls between functions and visually highlight function calls that take up a large amount of runtime. XHGui shells out to the dot graphviz utility to do this. Below is the command to install the package for this on Debian-based systems:
XHGui提供的另一个功能是调用图的生成,有向图 ,这些图显示了函数之间的调用,并直观地突出显示了占用大量运行时间的函数调用。 XHGui使用dot graphviz实用程序来执行此操作。 以下是在基于Debian的系统上为此安装软件包的命令:
sudo apt-get install graphvizNote that you also need to uncomment the block that looks like the one below in the XHGui config.php file or else XHGui will return a rather unintuitive error message in place of the callgraph. Thanks to a blog post by Lorna Jane Mitchell for pointing this out.
请注意,您还需要取消注释XHGui config.php file中看起来像下面的块,否则XHGui将返回一个不太直观的错误消息来代替调用图。 感谢Lorna Jane Mitchell的博客文章指出了这一点。
<?php //These are good for linux and its derivatives. $_xhprof['dot_binary'] = '/usr/bin/dot'; $_xhprof['dot_tempdir'] = '/tmp'; $_xhprof['dot_errfile'] = '/tmp/xh_dot.err';Near the top of the detail page for an individual run or a difference between two runs, you’ll find a “View Callgraph” link. Clicking it will generate a call graph resembling the one shown below. Note that this may take a while for profile runs with a lot of function calls. You can also click on an individual function name and click the “View Callgraph” link on its detail page to view a callgraph specific to that function.
对于单个运行或两次运行之间的差异,在详细信息页面顶部附近,您会找到“查看通话记录”链接。 单击它会生成一个类似于下图的调用图。 请注意,使用大量函数调用的概要文件运行可能需要一段时间。 您也可以单击单个函数名称,然后单击其详细信息页面上的“查看Callgraph”链接以查看特定于该函数的Callgraph。
I hope you’ve enjoyed this overview of the functionality that XHProf and XHGui provide and that it’s provided insight into the value of an effective profiling toolset. While your application layer may prove an infrequent cause of performance bottlenecks, you should now have the knowledge necessary to isolate their causes and take further steps to eliminate them.
我希望您喜欢XHProf和XHGui提供的功能的概述,并且可以深入了解有效的分析工具集的价值。 尽管您的应用程序层可能很少会导致性能瓶颈,但您现在应该掌握必要的知识,以找出其原因并采取进一步措施消除它们。
Image via Fotolia
图片来自Fotolia
翻译自: https://www.sitepoint.com/the-need-for-speed-profiling-with-xhprof-and-xhgui/
php xhprof
相关资源:jdk-8u281-windows-x64.exe