ProfilerSimple.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup Profiler
  5. */
  6. if ( !class_exists( 'Profiler' ) ) {
  7. require_once(dirname(__FILE__).'/Profiler.php');
  8. }
  9. /**
  10. * Simple profiler base class.
  11. * @todo document methods (?)
  12. * @ingroup Profiler
  13. */
  14. class ProfilerSimple extends Profiler {
  15. var $mMinimumTime = 0;
  16. var $mProfileID = false;
  17. function __construct() {
  18. global $wgRequestTime, $wgRUstart;
  19. if (!empty($wgRequestTime) && !empty($wgRUstart)) {
  20. $this->mWorkStack[] = array( '-total', 0, $wgRequestTime,$this->getCpuTime($wgRUstart));
  21. $elapsedcpu = $this->getCpuTime() - $this->getCpuTime($wgRUstart);
  22. $elapsedreal = microtime(true) - $wgRequestTime;
  23. $entry =& $this->mCollated["-setup"];
  24. if (!is_array($entry)) {
  25. $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
  26. $this->mCollated["-setup"] =& $entry;
  27. }
  28. $entry['cpu'] += $elapsedcpu;
  29. $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
  30. $entry['real'] += $elapsedreal;
  31. $entry['real_sq'] += $elapsedreal*$elapsedreal;
  32. $entry['count']++;
  33. }
  34. }
  35. function setMinimum( $min ) {
  36. $this->mMinimumTime = $min;
  37. }
  38. function setProfileID( $id ) {
  39. $this->mProfileID = $id;
  40. }
  41. function getProfileID() {
  42. if ( $this->mProfileID === false ) {
  43. return wfWikiID();
  44. } else {
  45. return $this->mProfileID;
  46. }
  47. }
  48. function profileIn($functionname) {
  49. global $wgDebugFunctionEntry;
  50. if ($wgDebugFunctionEntry) {
  51. $this->debug(str_repeat(' ', count($this->mWorkStack)).'Entering '.$functionname."\n");
  52. }
  53. $this->mWorkStack[] = array($functionname, count( $this->mWorkStack ), microtime(true), $this->getCpuTime());
  54. }
  55. function profileOut($functionname) {
  56. global $wgDebugFunctionEntry;
  57. if ($wgDebugFunctionEntry) {
  58. $this->debug(str_repeat(' ', count($this->mWorkStack) - 1).'Exiting '.$functionname."\n");
  59. }
  60. list($ofname, /* $ocount */ ,$ortime,$octime) = array_pop($this->mWorkStack);
  61. if (!$ofname) {
  62. $this->debug("Profiling error: $functionname\n");
  63. } else {
  64. if ($functionname == 'close') {
  65. $message = "Profile section ended by close(): {$ofname}";
  66. $functionname = $ofname;
  67. $this->debug( "$message\n" );
  68. $this->mCollated[$message] = array(
  69. 'real' => 0.0, 'count' => 1);
  70. }
  71. elseif ($ofname != $functionname) {
  72. $message = "Profiling error: in({$ofname}), out($functionname)";
  73. $this->debug( "$message\n" );
  74. $this->mCollated[$message] = array(
  75. 'real' => 0.0, 'count' => 1);
  76. }
  77. $entry =& $this->mCollated[$functionname];
  78. $elapsedcpu = $this->getCpuTime() - $octime;
  79. $elapsedreal = microtime(true) - $ortime;
  80. if (!is_array($entry)) {
  81. $entry = array('cpu'=> 0.0, 'cpu_sq' => 0.0, 'real' => 0.0, 'real_sq' => 0.0, 'count' => 0);
  82. $this->mCollated[$functionname] =& $entry;
  83. }
  84. $entry['cpu'] += $elapsedcpu;
  85. $entry['cpu_sq'] += $elapsedcpu*$elapsedcpu;
  86. $entry['real'] += $elapsedreal;
  87. $entry['real_sq'] += $elapsedreal*$elapsedreal;
  88. $entry['count']++;
  89. }
  90. }
  91. function getFunctionReport() {
  92. /* Implement in output subclasses */
  93. }
  94. function getCpuTime($ru=null) {
  95. if ( function_exists( 'getrusage' ) ) {
  96. if ( $ru == null )
  97. $ru = getrusage();
  98. return ($ru['ru_utime.tv_sec'] + $ru['ru_stime.tv_sec'] + ($ru['ru_utime.tv_usec'] +
  99. $ru['ru_stime.tv_usec']) * 1e-6);
  100. } else {
  101. return 0;
  102. }
  103. }
  104. /* If argument is passed, it assumes that it is dual-format time string, returns proper float time value */
  105. function getTime($time=null) {
  106. if ($time==null)
  107. return microtime(true);
  108. list($a,$b)=explode(" ",$time);
  109. return (float)($a+$b);
  110. }
  111. }