ProfilerSimpleTrace.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Profiler
  5. */
  6. if ( !class_exists( 'ProfilerSimple' ) ) {
  7. require_once(dirname(__FILE__).'/ProfilerSimple.php');
  8. }
  9. /**
  10. * Execution trace
  11. * @todo document methods (?)
  12. * @ingroup Profiler
  13. */
  14. class ProfilerSimpleTrace extends ProfilerSimple {
  15. var $mMinimumTime = 0;
  16. var $mProfileID = false;
  17. var $trace = "";
  18. var $memory = 0;
  19. function __construct() {
  20. global $wgRequestTime, $wgRUstart;
  21. if (!empty($wgRequestTime) && !empty($wgRUstart)) {
  22. $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
  23. $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
  24. $elapsedreal = microtime(true) - $wgRequestTime;
  25. }
  26. $this->trace .= "Beginning trace: \n";
  27. }
  28. function profileIn($functionname) {
  29. global $wgDebugFunctionEntry;
  30. $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
  31. $this->trace .= " " . sprintf("%6.1f",$this->memoryDiff()) . str_repeat( " ", count($this->mWorkStack)) . " > " . $functionname . "\n";
  32. }
  33. function profileOut($functionname) {
  34. global $wgDebugFunctionEntry;
  35. if ($wgDebugFunctionEntry) {
  36. $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
  37. }
  38. list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
  39. if (!$ofname) {
  40. $this->trace .= "Profiling error: $functionname\n";
  41. } else {
  42. if ($functionname == 'close') {
  43. $message = "Profile section ended by close(): {$ofname}";
  44. $functionname = $ofname;
  45. $this->trace .= $message . "\n";
  46. }
  47. elseif ($ofname != $functionname) {
  48. $self->trace .= "Profiling error: in({$ofname}), out($functionname)";
  49. }
  50. $elapsedcpu = $this->getCpuTime() - $octime;
  51. $elapsedreal = microtime(true) - $ortime;
  52. $this->trace .= sprintf("%03.6f %6.1f",$elapsedreal,$this->memoryDiff()) . str_repeat(" ",count($this->mWorkStack)+1) . " < " . $functionname . "\n";
  53. }
  54. }
  55. function memoryDiff() {
  56. $diff = memory_get_usage() - $this->memory;
  57. $this->memory = memory_get_usage();
  58. return $diff/1024;
  59. }
  60. function getOutput() {
  61. print "<!-- \n {$this->trace} \n -->";
  62. }
  63. }