欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php运行状态报告工具:php-timer

程序员文章站 2022-04-08 08:32:50
...
最近写了一个异步mysql客户端的封装,想与传统的串行方式做下性能对比。包括运行时间、内存使用情况等信息。在github和packagist上搜索并没有找到自己想要的,xhprof又太大了,结果也太复杂,不符合现在的需要。所以决定自己写一个package,来实现对php脚本运行时间和内存使用情况的监控,并生成报告。

示例代码:

requiredirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR .'vendor'. DIRECTORY_SEPARATOR .'autoload.php';//初始化,设置内存单位$timer=new\Jenner\Timer(\Jenner\Timer::UNIT_KB);//记录a状态$timer->mark('a');sleep(2);//记录b状态$timer->mark('b');sleep(3);//记录c状态$timer->mark('c');sleep(4);//记录d状态$timer->mark('d');//打印总体报告(不包含差值)$timer->printReport();//获取总体报告,返回数组$report=$timer->getReport();//获取一个mark的报告$a_report=$timer->getReport('a');print_r($a_report);//打印a状态和b状态的差异信息,包含运行时间、使用内存等$timer->printDiffReportByStartAndEnd('a','b');//获取a状态和b状态的差异报告$ab_diff_report=$timer->getDiffReportByStartAndEnd('a','b');//打印第一个mark和最后一个mark之间的差异信息$timer->printDiffReport();//获取第一个mark和最后一个mark之间的差异信息$diff_report=$timer->getDiffReport();

输出结果:

 ------------------------------------------mark:atime:1437535424.9998smemory_real:1280KBmemory_emalloc:833.046875KBmemory_peak_real:1280KBmemory_peak_emalloc:843.2890625KB------------------------------------------mark:btime:1437535427smemory_real:1280KBmemory_emalloc:834.2265625KBmemory_peak_real:1280KBmemory_peak_emalloc:843.2890625KB------------------------------------------mark:ctime:1437535430.0002smemory_real:1280KBmemory_emalloc:835.1875KBmemory_peak_real:1280KBmemory_peak_emalloc:843.2890625KB------------------------------------------mark:dtime:1437535434.0004smemory_real:1280KBmemory_emalloc:836.1484375KBmemory_peak_real:1280KBmemory_peak_emalloc:843.2890625KBArray(    [time] => 1437535424.9998    [memory_real] => 1310720    [memory_emalloc] => 853040    [memory_peak_real] => 1310720    [memory_peak_emalloc] => 863528)------------------------------------------mark:[diff] start_mark:a end_mark:btime:2.0001850128174smemory_real:0KBmemory_emalloc:1.1796875KBmemory_peak_real:0KBmemory_peak_emalloc:0KB------------------------------------------mark:[totaldiff]time:9.0006000995636smemory_real:0KBmemory_emalloc:3.1015625KBmemory_peak_real:0KBmemory_peak_emalloc:0KB

项目主页:http://www.open-open.com/lib/view/home/1437969396019