php 性能分析_PHP-代码分析和性能分析

php 性能分析

Let's say you have a PHP script that is performing slow. You've gone through all the steps you can think of to figure out what is wrong, or it's just too complex of an application. You need help, and it not only exists, but it's fairly easy to get!

假设您有一个运行缓慢PHP脚本。 您已经完成了所有可以想到的步骤,以找出问题所在,或者这对于应用程序来说太复杂了。 您需要帮助,它不仅存在,而且很容易获得!

The solution here is called cachegrind. It's a bit of an obscure name, but stems from how it was originally used. The concept is that you can have PHP (via an extension called XDebug) keep track of how long it took for every single line of code in a script to run, and to dump that information into a specially-formatted file called a cachegrind file. The script itself should run exactly as you would normally run it, so that the profiling process is virtually invisible.

这里的解决方案称为cachegrind。 这个名字有点晦涩,但源于它最初的用法。 这个概念是,您可以让PHP(通过名为XDebug的扩展名)跟踪脚本中每行代码运行所花费的时间,并将信息转储到特殊格式的文件(称为cachegrind文件)中。 脚本本身应与正常运行时完全相同,因此概要分析过程实际上是不可见的。

This resulting cachegrind file is pretty difficult to read without any sort of tools, so we use some existing, free tools to read the cachegrind files for us and present a much easier-to-use interface for seeing how our script performed.

如果没有任何工具,则很难读取此生成的cachegrind文件,因此,我们使用一些现有的免费工具为我们读取cachegrind文件,并提供了一个更易于使用的界面来查看脚本的性能。

It's a very simple process and can be set up from beginning to end in 3 steps:

这是一个非常简单的过程,可以通过3个步骤从头到尾进行设置:

步骤1:安装XDebug (Step 1: Install XDebug)

不用担心,这始终是最复杂的阶段,但还算不错。 如果您需要的话,这里有大量的文档和大量的帮助!

For Linux platforms, read this page:

对于Linux平台,请阅读以下页面:

http://www.xdebug.org/docs/install http://www.xdebug.org/docs/install

For Windows platforms, just copy and paste the phpinfo() screen contents into this page:

对于Windows平台,只需将phpinfo()屏幕内容复制并粘贴到此页面中:

http://www.xdebug.org/wizard.php http://www.xdebug.org/wizard.php

...and you'll get the exact instructions of what you need to do.

...您将获得所需操作的确切说明。

Also, set up your php.ini file with a few options to enable you to turn on profiling on demand and where to put the files that will be generated. My php.ini file on Windows looks like this:

另外,使用一些选项设置您的php.ini文件,以使您能够按需打开配置文件以及将生成文件的位置。 我在Windows上的php.ini文件如下所示:

xdebug.profiler_enable_trigger=1

xdebug.profiler_enable_tri gger = 1

xdebug.profiler_output_dir=D:\Temp

xdebug.profiler_output_dir = D:\ Temp

On Linux, it would look like:

在Linux上,它看起来像:

xdebug.profiler_enable_trigger=1

xdebug.profiler_enable_tri gger = 1

xdebug.profiler_output_dir=/tmp

xdebug.profiler_output_dir = / tmp

Make sure you restart your web server/service after changing the php.ini file!

确保更改php.ini文件后重新启动Web服务器/服务!

步骤2:生成代码配置文件 (Step 2: Generate the Code Profile)

在第1步中安装了XDebug之后,您要做的就是运行脚本并传递XDEBUG_PROFILE = 1作为查询参数。 例如,假设您的普通脚本是这样访问的:

http://www.mysite.com/script.php?something=abc

http://www.mysite.com/script.php?something=abc

You would simply append the XDEBUG_PROFILE parameter like this:

您只需像这样附加XDEBUG_PROFILE参数:

http://www.mysite.com/script.php?something=abc&XDEBUG_PROFILE=1

http://www.mysite.com/script.php?something=abc&XDEBUG_PROFILE=1

That triggers the code profiler to start. When the script ends, you will have a cachegrind-compatible file ready for analyzing. In my case, I accessed:

这将触发代码分析器启动。 脚本结束后,将准备好与cachegrind兼容的文件以供分析。 就我而言,我访问了:

http://localhost/date.php?XDEBUG_PROFILE=1 http://localhost/date.php?XDEBUG_PROFILE = 1

...and ended up with:

...最后以:

D:\Temp\cachegrind.out.8392 (because 8392 was the process ID)

D:\ Temp \ cachegrind.out.839 2(因为8392是进程ID)

步骤3:分析结果 (Step 3: Analyze the Results)

打开KCacheGrind或WinCacheGrind或MacCallGrind(或用于分析文件的任何应用程序),并告诉它加载该文件。

Each application will have a different layout, but each application should give you the ability to view the amount of time that each step took - both the individual call time and the time it took for that section to complete. For example, if your script includes a file that connects to a database, you should see a small amount of time (a couple of milliseconds) to include the file, and a cumulatively larger amount of time that it took overall because of the database connection.

每个应用程序都有不同的布局,但是每个应用程序都应该使您能够查看每个步骤所花费的时间-各个调用时间以及该部分完成所花费的时间。 例如,如果您的脚本包括一个连接到数据库的文件,那么您应该会花很少的时间(几毫秒)来包含该文件,并且由于数据库的连接,整个过程所花费的时间累计会更长。

You can use a treeview-like interface to drill down into each section of code to gain more insight into where any performance bottlenecks might be occurring.

您可以使用类似树形视图的界面来向下钻取代码的每个部分,以更深入地了解可能出现任何性能瓶颈的地方。

Here is a screenshot of a sample application with millions of lines of code that was profiled in a matter of seconds:

这是一个示例应用程序的屏幕截图,其中包含了数百万行的代码,这些代码在几秒钟内即可完成概要分析:

WinCacheGrind

You'll see the treeview navigation on the left that allows me to drill down into any particular section of the code flow. I've selected the {main} section on that left side to see the top-level details of my script so I can see where the major bottlenecks are.

您将在左侧看到树视图导航,该导航使我可以深入到代码流的任何特定部分。 我选择了左侧的{main}部分,以查看脚本的顶级详细信息,以便了解主要瓶颈在哪里。

On the right side, I can see that while including the header.inc.php file only took 8 milliseconds, something inside the header.inc.php file took a whopping 1.6 seconds to run! So if I navigate to the header.inc.php node on the left navigation pane, I can then see a breakdown of that time:

在右侧,我可以看到虽然包含header.inc.php文件仅花费了8毫秒,但是header.inc.php文件内部的内容却花费了1.6秒才能运行! 因此,如果导航到左侧导航窗格中的header.inc.php节点,则可以看到该时间的细分:

WinCacheGrind - Detailed Breakdown

I can continue to drill down into each node if I want to keep getting further details about the performance, until I've learned exactly which line of code is responsible for the slowdowns. In the above examples, the slowdowns happen to be related to database connections.

如果我想继续获得有关性能的更多详细信息,那么我可以继续深入研究每个节点,直到我确切地了解到导致减速的代码行。 在以上示例中,速度下降恰好与数据库连接有关。

其他选择 (Other Options)

XDebug除具有cachegrind代码概要分析外,还具有许多其他选项和功能,因此在此处将它们全部覆盖是不切实际的。 确保您查看其主要文档页面以获取其他帮助和示例!

Main Documentation Page:

主要文档页面:

http://www.xdebug.org/docs/ http://www.xdebug.org/docs/

Profiling Page:

分析页面:

http://www.xdebug.org/docs/profiler http://www.xdebug.org/docs/profiler

翻译自: https://www.experts-exchange.com/articles/12179/PHP-Code-Profiling-and-Analyzing-Performance.html

php 性能分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值