Advertisement
Pietu1998

PHP Simple Profiler

Sep 28th, 2014
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.16 KB | None | 0 0
  1. /* BEGIN PROFILER.PHP */
  2. $profiler_start;
  3. $profiler_phases = array();
  4. $profiler_current;
  5. function profiler_start($phase) {
  6.     global $profiler_start, $profiler_current;
  7.     $profiler_current = $phase;
  8.     $profiler_start = microtime();
  9. }
  10. function profiler_next($phase) {
  11.     global $profiler_start, $profiler_current, $profiler_phases;
  12.     $now = microtime();
  13.     $profiler_phases[] = $profiler_current . ": " . (($now - $profiler_start) / 1000) . "ms";
  14.     $profiler_current = $phase;
  15.     $profiler_start = $now;
  16. }
  17. function profiler_end() {
  18.     global $profiler_start, $profiler_current, $profiler_phases;
  19.     $profiler_phases[] = $profiler_current . ": " . ((microtime() - $profiler_start) / 1000) . "ms";
  20. }
  21. function profiler_result() {
  22.     global $profiler_phases;
  23.     return implode("\n", $profiler_phases);
  24. }
  25. /* END PROFILER.PHP */
  26.  
  27. /* BEGIN EXAMPLE USAGE */
  28. include "profiler.php";
  29. profiler_start("init");
  30. $f = fopen("file.txt", "w");
  31. profiler_next("write");
  32. fwrite($f, "Test String");
  33. profiler_next("close");
  34. fclose($f);
  35. profiler_end();
  36. echo profiler_result();
  37. /* END EXAMPLE USAGE */
  38.  
  39. /* BEGIN EXAMPLE RESULT
  40. init: 50ms
  41. write: 100ms
  42. close: 30ms
  43. /* END EXAMPLE RESULT */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement