为了账号安全,请及时绑定邮箱和手机立即绑定

分析PHP脚本的最简单方法

分析PHP脚本的最简单方法

PHP
胡说叔叔 2019-06-25 16:12:41
分析PHP脚本的最简单方法对PHP脚本进行分析的最简单方法是什么?我很乐意在上面添加一些东西,显示所有函数调用的转储,以及它们花了多长时间,但我也不介意在特定函数周围放置一些东西。我试着用微时间职能:$then = microtime();myFunc();$now = microtime();echo sprintf("Elapsed:  %f", $now-$then);但这有时会给我负面的结果。另外,在我的代码中洒上这些也是很麻烦的。
查看完整描述

3 回答

?
万千封印

TA贡献1891条经验 获得超3个赞

你想要的x调试我认为。安装在服务器上,打开它,泵输出通过苦磨(适用于Linux)或风磨(对于windows),它将向您展示一些漂亮的图表,详细说明确切的时间、计数和内存使用情况(但您需要另一个扩展)。



查看完整回答
反对 回复 2019-06-25
?
大话西游666

TA贡献1817条经验 获得超14个赞

不需要扩展,只需使用这两个函数进行简单的分析。

// Call this at each point of interest, passing a descriptive stringfunction prof_flag($str){
    global $prof_timing, $prof_names;
    $prof_timing[] = microtime(true);
    $prof_names[] = $str;}// Call this when you're done and want to see the resultsfunction prof_print(){
    global $prof_timing, $prof_names;
    $size = count($prof_timing);
    for($i=0;$i<$size - 1; $i++)
    {
        echo "<b>{$prof_names[$i]}</b><br>";
        echo sprintf("&nbsp;&nbsp;&nbsp;%f<br>", $prof_timing[$i+1]-$prof_timing[$i]);
    }
    echo "<b>{$prof_names[$size-1]}</b><br>";}

下面是一个示例,在每个检查点使用描述调用prof_频标(),并在末尾调用prof_print():

prof_flag("Start");

   include '../lib/database.php';
   include '../lib/helper_func.php';prof_flag("Connect to DB");

   connect_to_db();prof_flag("Perform query");

   // Get all the data

   $select_query = "SELECT * FROM data_table";
   $result = mysql_query($select_query);prof_flag("Retrieve data");

   $rows = array();
   $found_data=false;
   while($r = mysql_fetch_assoc($result))
   {
       $found_data=true;
       $rows[] = $r;
   }prof_flag("Close DB");

   mysql_close();   //close database connectionprof_flag("Done");prof_print();

输出如下:

启动
   0.004303
连接到DB
   0.003518
执行查询
   0.000308
检索数据
   0.000009
关闭DB
   0.000049
已完成


查看完整回答
反对 回复 2019-06-25
  • 3 回答
  • 0 关注
  • 387 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信