public
Last active

Ultra Simple PHP Profiler

  • Download Gist
profile.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
<?php
$_start = 0;
$_subdata = '';
 
function profile_start() {
global $profile;
if ( $profile ) {
global $_start, $_subdata;
$_start = microtime(true);
$_subdata = '';
}
}
 
function profile_end( $data ) {
global $profile, $_subdata;
if ( $profile ) {
global $_start;
// open file
$fd = fopen("/tmp/profile.csv", "a");
// write string
fwrite($fd, ( microtime(true) - $_start ) . "," . $data . "\n");
fwrite($fd, $_subdata );
// close file
fclose($fd);
}
}
 
function profile_info( $q ) {
global $profile, $_subdata, $_start;
if ( $profile ) {
$_subdata .= (microtime(true) - $_start) . ",\"- " . $q . "\"\n";
}
}
 
function mysql_queryx( $q ) {
global $profile, $_subdata;
if ( $profile ) {
$pre = microtime(true);
$result = mysql_query( $q );
$_subdata .= (microtime(true) - $pre) . ",\"- " . $q . "\"\n";
return $result;
}
else {
return mysql_query( $q );
}
}
?>
profiler_usage.php
PHP
1 2 3 4 5 6 7 8 9 10 11
<?php
$profile = true;
require_once "profile.php";
profile_start();
// ...
$query = mysql_queryx( "select status from table" );
// ...
profile_info( "checkpoint" );
// ...
profile_end( "page" );
?>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.