You can use basic stuff like storing timestamps or microtime() before and after an operation to calculate the time needed. That's easy to do, but not very accurate. Maybe a better solution is Xdebug, i've never worked with it but it seems to be the best-known PHP debugger/profiler I can find.
What I generally do is not enable the profiler by default (它会生成相当大的文件,并减慢速度), but use the possibility to send a parameter called XDEBUG_PROFILE as GET data, to activate profiling just for the page I need.
我的 php.ini 中与分析相关的部分如下所示:
xdebug.profiler_enable = 0 ; Profiling not activated by default
xdebug.profiler_enable_trigger = 1 ; Profiling activated when requested by the GET parameter
xdebug.profiler_output_dir = /tmp/ouput_directory
xdebug.profiler_output_name = files_names
(阅读文档了解更多信息)
This screenshot is from a C++ program in KcacheGrind : (资料来源: sourceforge.net)
This allows you to get a nice view of what takes time in your application -- and it sometimes definitly helps to locate 的 function that is slowing everything down ^^
Note that Xdebug counts the CPU time spent by PHP ; when PHP is waiting for an answer from a Database (for instance), it is not working ; only waiting. So Xdebug will think the DB request doesn't take much time !
这应该在 SQL 服务器上进行配置,而不是 PHP,所以..。
$th = new TimingHelper();
<..code being mesured..>
echo $th->time();
$th->start(); // if it's the case
<..code being mesured..>
echo $th->time();
// result: 4d 17h 34m 57s 0.00095367431640625ms
Ruxit has been bought by Dynatrace. No shocker here as Ruxit is built by ex Dynatrace Employees. This allowed Dynatrace to transform to a truly SaaS model for better. Say goodbye to that bulky Java client if you'd like to.
There are free/open-source options now as well. Checkout 阿帕奇天行者 which is very popular in China among their top tech companies and PinPoint which offers a demo that you can try before installing. Both of these require you manage hosting so get ready to spin up few Virtual Machine and spend some time with installation and configuration.
Why not use an Application Performance Monitoring (APM) tool which are build exactly for that and so much more. Check out NewRelic, AppDynamics, Ruxit (all have free version) to monitor the execution time, resource usage, throughput of every application to the method level.