xhprof管理

来源:互联网 发布:手机美发软件 编辑:程序博客网 时间:2024/04/30 14:20

上次在虚拟机的linux系统中安装好了xhprof,相当强大,程序的不足之处一目了然,但是有个问题就是不方便对xhprof生成的日志进行管理,肉眼根本看不出哪条记录是哪天的,更不说哪条日志对应哪个url,所以闲暇之余写了个管理脚本,这样再做成一个表,就非常方便了
其实xhprof是可以自定义文件名字的,设置方法就是在save_run的时候加一个参数

  1. $xhprof_runs->save_run($xhprof_data,'xhprof',$filename);

这样虽然可以很方便的根据保存的文件名来判断日志文件,但是对我们来说仍然不够方便,像开始说的,没办法知道记录对应的url,url状态等等。下面来说说怎么自己做一个管理脚本

首先,在调用xhprof的程序中加几行代码

  1. //xhprof以10分之一的几率开启记录,放在程序执行开头
  2. $xhprof_on = false;
  3. if(rand(1,10)==1){
  4. xhprof_enable(XHPROF_FLAGS_CPU + XHPROF_FLAGS_MEMORY);
  5. $xhprof_on = true;
  6. }
  7.  
  8. ...
  9. ...
  10. 这里是程序执行代码过程
  11. ...
  12. ...
  13.  
  14. //xhprof日志记录,这里增加了写入url信息
  15. if($xhprof_on){
  16. $xhprof_data = xhprof_disable();
  17. include_once ROOT_PATH.'xhprof_lib/utils/xhprof_lib.php';
  18. include_once ROOT_PATH.'xhprof_lib/utils/xhprof_runs.php';
  19. $xhprof_runs = new XHProfRuns_Default();
  20. $run_id = $xhprof_runs->save_run($xhprof_data,'milan20');
  21. //===封装SERVER信息===
  22. $host = $_SERVER['HTTP_HOST'];
  23. $url = $_SERVER['REQUEST_URI'];
  24. $runtime = (microtime(true)-SCRIPT_TIME_START)*1000;//ms
  25. $requestTime = $_SERVER['REQUEST_TIME'];
  26. $status = $_SERVER['REDIRECT_STATUS'];
  27. //$filePath = $_SERVER['SCRIPT_FILENAME'];
  28. $data = array('url'=>$url,'runtime'=>$runtime,'host'=>$host,'requestTime'=>$requestTime,'status'=>$status);
  29. $string = "< ?php\n return ";
  30. $string .= var_export($data,TRUE);
  31. $string .= "\n?>";
  32. file_put_contents('/home/www/xhprof/logurl/'.$run_id.'.log',$string);
  33. //===封装完毕===
  34. }

通过以上代码,我们就可以在/home/www/xhprof/logurl/目录下生成与xhprof日志同名的.log文件,我的xhprof日志放在/home/www/xhprof/log/中,而/home/www/xhprof/是我专门用来管理xhprof的web目录
在上面添加的代码中,我从$_SERVER中取出了页面url,请求信息,请求状态等信息,并记录了页面执行时间,这里我计算执行时间可能不适用于其他人,不过程序原理一样,就是在程序开头记录一个程序开始的时间戳,然后知道程序执行最后再取一个时间戳,俩个时间戳对比就得出程序执行时间了。,最后把这些信息组装进日志文件中。

第二步就是重点了,自己写脚本来分析生成好的日志文件

  1. < ?php
  2. //声明编码为utf-8
  3. header("Content-type:text/html; charset='UTF-8'");
  4. if($_POST['df']){
  5. if($_POST['df']=='对比'){//这里是处理俩个或多个日志对比
  6. $diff = $_POST['diff'];
  7. if(is_array($diff)){
  8. foreach($diff as $k=>$v){
  9. if($k==0){
  10. $run = '?run'.($k+1).'='.$v;
  11. }else{
  12. $run .= '&run'.($k+1).'='.$v;
  13. }
  14.  
  15. }
  16. }
  17. header("Location:http://xhprof.cj.com/xhprof_html/index.php".$run."&source=milan20");
  18. exit();
  19. }elseif($_POST['df']=='汇总'){//这里是处理俩个或多个日志汇总比较
  20. $diff = $_POST['diff'];
  21. if(is_array($diff)){
  22. $run = '?run=';
  23. foreach($diff as $k=>$v){
  24. $run .= $v.',';
  25. }
  26. $run = substr($run,-1) == ',' ? substr($run , 0,-1) : $run;
  27. }
  28. header("Location:http://xhprof.cj.com/xhprof_html/index.php".$run."&source=milan20");
  29. exit();
  30. }
  31. }
  32. //以下就是从/home/www/xhprof/logurl/中读取同名的.log日志并进行分析,以表格的形式列出
  33. $root = dirname(__FILE__).'/';
  34. $logRoot = $root.'log/';
  35. $handle = dir($logRoot);
  36. $table = '<form name="form1" action="" method="post" target="_blank">';
  37. $table .= '<table cellpadding="5" border="0" cellspacing="1"><tr><td colspan="8"><h2>XHPROF 日志列表</h2></td></tr><tr style="background:#ccc; text-align:center;"><td>选择</td><td>Location</td><td>Name</td><td>处理时间(ms)</td><td>请求时间</td><td>请求状态</td><td>Url</td></tr>';
  38. while(false!==($log=$handle->read())){
  39. if($log != '.' && $log != '..'){
  40. $table .= '<tr style="background:#eee;">';
  41. if(file_exists('/home/www/xhprof/logurl/'.basename($log,'.milan20').'.log')){
  42. $info = include('/home/www/xhprof/logurl/'.basename($log,'.milan20').'.log');
  43. }else{
  44. $info = '/';
  45. }
  46. $table .= '<td><input type="checkbox" name="diff[]" value="'.basename($log,'.milan20').'"/></td>';
  47. $table .= '<td>http://'.$info['host'].$info['url'].'</td>';
  48. $table .= '<td>'.$log.'</td>';
  49. $table .= '<td>'.$info['runtime'].'</td>';
  50. $table .= '<td>'.date('Y-m-d H:i:s',$info['requestTime']).'</td>';
  51. $table .= '<td>'.$info['status'].'</td>';
  52. /*$table .= '<td>'.$info['filePath'].'</td>';*/
  53. /*$table .= '<td>'.date('Y-m-d H:i:s',filemtime($logRoot.$log)).'</td>';*/
  54. $table .= '<td><a href="http://xhprof.cj.com/xhprof_html/index.php?run='.basename($log,'.milan20').'&source=milan20" target="_blank">click</a></td>';
  55.  
  56. }
  57. $table .= '</tr>';
  58. }
  59. $table .= '<tr colspan="8"><td><input type="submit" value="汇总" name="df" /><input type="submit" value="对比" name="df" /></td></tr>';
  60. $table .= '</table>';
  61. $table .= '</form>';
  62. echo $table;
  63. ?>

至此,整个管理脚本就完成了,可以访问地址看看效果了,我的效果

xhprof

xhprof日志列表

转载于:http://blog.sochiba.com/archives/198
原创粉丝点击