08
Aug

einfacher PHP Profiler

Ich suchte nach einem “einfachen” Weg um in meinen PHP Scripts herauszufinden wo sich langsame Teile eingeschlichen haben, um diese dann zu beschleunigen. Dann bin auch auf den Artikel von Michael auf PHP Gangsta gestoßen, PHP hat Ticks. Nun habe heute mal eben schnell meinen ganz eigenen “PHP Profiler” gebastelt. Er ist nichts besonderes, für mich reichts. Euch möchte ich den Profiler natürlich nicht vorenthalten.

PHP Profiler

Folgenden Code einfach als “debug.php” auf deinem Server speichern.

function debug() {
 
 static $start_time = NULL;
 static $start_code_line = 0;
 global $lines;
 
 $call_info = array_shift( debug_backtrace() );
 $code_line = $call_info['line'];
 $file = array_pop( explode('/', $call_info['file']));
 ob_end_clean();
 if( $start_time === NULL ) {
 $path = $call_info['file'];
 $handle = fopen($path, "r");
 $lines=file($path);
 header('Content-type: text/plain');
 echo "debug ".$file."> initialize\n";
 ob_start();
 $start_time = time() + microtime();
 return 0;
 }
 
 printf("%s:%d time: %.6f memory: %d KB -> (%s)\n", $file, $code_line, (time() + microtime() - $start_time), ceil( memory_get_usage()/1024), htmlentities(trim($lines[$code_line-1])));
 ob_start();
 $start_time = time() + microtime();
 
}

Initialize:

Folgendes einfach ganz am Anfang in das zu Profilende PHP Script einbauen.

include_once('./debug.php');
ob_start();
declare(ticks = 1);
register_tick_function('debug');

Das war’s, jetzt einfach das zu testende Script aufrufen, und fertig!

Beispiel Ausgabe:

debug test.php> initialize
test.php:8 time: 0.000106 memory: 82 KB -> (echo 'hallo'; )
test.php:9 time: 0.000040 memory: 83 KB -> ($a = 5+3; )
test.php:10 time: 0.000075 memory: 83 KB -> (echo $a; )
test.php:11 time: 0.001986 memory: 571 KB -> (include_once('muh.txt'); )
test.php:12 time: 0.000039 memory: 572 KB -> ($b=6546841321681435836138135864146*34616561616165645135181035413516; )
test.php:13 time: 0.000043 memory: 572 KB -> ($b=$b+$a; )
test.php:14 time: 0.000070 memory: 572 KB -> ($x = md5($a); )
test.php:15 time: 0.000047 memory: 572 KB -> ($y = md5('tasdasdasdext'); )
test.php:16 time: 0.000069 memory: 572 KB -> ($z = md5('teasdasdasdasdasdasdasdasxt'); )
test.php:17 time: 0.000047 memory: 573 KB -> ($f = md5('teasdasdasdasdasdasdasdasdasdasdasdasdasdasdasxt'); )
test.php:18 time: 0.000093 memory: 573 KB -> ($c = md5($b); )
test.php:19 time: 0.003771 memory: 573 KB -> (usleep(23); )
test.php:24 time: 0.000354 memory: 574 KB -> ( $m['a'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($txt)))))))))))))))))))))))); )
test.php:25 time: 0.000213 memory: 574 KB -> ( $m['b'] = md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1($m['a'])))))))))))))))); )
test.php:26 time: 0.000309 memory: 575 KB -> ( $m['c'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($m['b'])))))))))))))))))))))))); )
test.php:20 time: 0.000038 memory: 575 KB -> ($fgfg= test($f); )
test.php:24 time: 0.000299 memory: 575 KB -> ( $m['a'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($txt)))))))))))))))))))))))); )
test.php:25 time: 0.000203 memory: 575 KB -> ( $m['b'] = md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1($m['a'])))))))))))))))); )
test.php:26 time: 0.000295 memory: 575 KB -> ( $m['c'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($m['b'])))))))))))))))))))))))); )
test.php:21 time: 0.000036 memory: 575 KB -> ($sdf = test($x); )
test.php:24 time: 0.000296 memory: 576 KB -> ( $m['a'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($txt)))))))))))))))))))))))); )
test.php:25 time: 0.000197 memory: 576 KB -> ( $m['b'] = md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1(md5(sha1($m['a'])))))))))))))))); )
test.php:26 time: 0.000294 memory: 576 KB -> ( $m['c'] = sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1(sha1($m['b'])))))))))))))))))))))))); )
test.php:22 time: 0.000036 memory: 576 KB -> ($dfhgh =test($y); )
test.php:29 time: 0.000034 memory: 576 KB -> (} )
test.php:31 time: 0.000034 memory: 576 KB -> (?>)

hinterlasse eine Nachricht