Project

General

Profile

PHP Profiling » History » Revision 2

Revision 1 (Carsten Rose, 06.01.2024 14:02) → Revision 2/6 (Carsten Rose, 06.01.2024 14:11)

h1. PHP Profiling 

 h2. xdebug 

 * https://xdebug.org/docs/profiler 
 * "Xdebug 3":https://www.youtube.com/playlist?list=PLg9Kjjye-m1g_eXpdaifUqLqALLqZqKd4 

 h2. via php 

 No extensions are needed, just use these two functions for simple profiling. 

 <pre> 
 // Call this at each point of interest, passing a descriptive string 
 function 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 results 
 function 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>"; 
 } 
 </pre> 

 Here is an example, calling prof_flag() with a description at each checkpoint, and prof_print() at the end: 

 <pre> 
 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 connection 

 prof_flag("Done"); 
 prof_print(); 
 </pre> 

 Output looks like this: 

 <pre> 
 Start 
    0.004303 
 Connect to DB 
    0.003518 
 Perform query 
    0.000308 
 Retrieve data 
    0.000009 
 Close DB 
    0.000049 
 Done 
 </pre>