沙尘暴 / phpprofiler
收集计时器和xhprof跟踪的基础包。可以与sandstorm/plumber一起使用进行可视化。
This package is auto-updated.
Last update: 2024-09-13 12:28:27 UTC
README
-- 测量应用程序的流程 --
PhpProfiler是一个性能分析和跟踪工具,它可以测量应用程序流程中各个部分所花费的时间,并且可以利用XHProf来分析应用程序。
它以Plumber理解的格式存储数据,也可以存储到XHProf.io(http://xhprof.io/)和XHGui(https://github.com/preinheimer/xhgui)所使用的数据库。
安装
要安装,只需使用composer
composer require --dev sandstorm/phpprofiler ^3.0.0
系统将自动安装PhpProfiler,如果已安装XHProf,则使用XHProf。
配置
这是默认配置
PhpProfiler:
plumber:
profilePath: '%FLOW_PATH_DATA%Logs/Profiles'
# xhprof.io settings (see http://xhprof.io/)
'xhprof.io':
enable: false
dsn: 'mysql:dbname=xhprofio;host=localhost;charset=utf8'
username: ''
password: ''
# preinheimer-xhgui settings (see https://github.com/preinheimer/xhgui)
'xhgui':
enable: false
host: 'mongodb://localhost:27017'
dbname: 'xhprof'
要启用XHProf.io和XHGui后端,根据需要调整配置,但请注意,任何必要的设置(例如数据库创建)都需要按照相应文档中的描述进行。
限制性能分析运行的几率
使用环境变量PHPPROFILER_SAMPLINGRATE
可以改变运行性能分析的几率。如果没有设置该变量,则每个运行都会进行性能分析。如果提供一个介于0和1之间的浮点数,则代表每个运行触发性能分析的概率在0%到100%之间。
如果将概率限制在足够低的值,则可以在生产实例上运行PhpProfiler。
性能分析自定义代码
PhpProfiler收集常规的XHProf数据以及一些针对TYPO3 Flow、Neos和CMS的特定数据。
要收集自定义应用程序关键部分的性能分析信息,有多种选项可供选择。
使用方面(NEW!)进行方法调用性能分析
您可以在方法上使用Sandstorm\PhpProfiler\Annotations\Profile
注解来对其进行性能分析
class MyClass { /** * @Sandstorm\PhpProfiler\Annotations\Profile */ public function myMethod() { } }
添加自定义计时器
在寻找性能瓶颈时,在应用程序中添加自定义计时器通常是有意义的。这样做非常简单,以下示例将展示这一点
\Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->startTimer('My Timer'); // run some code \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->stopTimer('My Timer');
如果计时器名称包含冒号(:
),则在用户界面中将相关计时器分组在一起
\Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->startTimer('Security: Authentication'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->stopTimer('Security: Authentication'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->startTimer('Security: Authorization'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->stopTimer('Security: Authorization');
如果同时激活多个计时器,则没有问题;相同的计时器也可以同时多次激活。以下示例完全有效
\Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->startTimer('t1'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->startTimer('t1'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->stopTimer('t1'); \Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->stopTimer('t1');
此外,startTimer
允许一个包含额外信息的第二个array
参数,这些信息将在UI中显示。
设置选项
此外,您还可以设置当前运行(目前称为options
)的元信息
\Sandstorm\PhpProfiler\Profiler::getInstance()->getRun()->setOption('context', 'DEV');
查看结果
要安装Plumber UI,请按照其手册中的说明进行操作。
对于XHProf.ui和XHGui,请遵循项目网站上的说明。
鸣谢
最初由Sebastian Kurfürst开发,Sandstorm Media UG(haftungsbeschränkt)
包含用于存储数据的XHProf.io和XHGui项目代码。
许可证
所有代码均采用GPL许可证。