PageRenderTime 39ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/oyejorge/less.php/test/tracefile-analyser.php

https://gitlab.com/yousafsyed/easternglamor
PHP | 207 lines | 180 code | 14 blank | 13 comment | 3 complexity | 170c054a023e390e288b550cec867632 MD5 | raw file
  1. <?php
  2. $fileName = '/tmp/trace.2043925204.xt';
  3. $sortKey = 'time-own'; //array( 'calls','time-inclusive' , 'memory-inclusive', 'time-own', 'memory-own','time-own-percall' )
  4. $elements = 50;
  5. set_time_limit(120);
  6. echo '<pre>';
  7. $o = new drXdebugTraceFileParser( $fileName );
  8. $o->parse();
  9. $functions = $o->getFunctions( $sortKey );
  10. // find longest function name
  11. $maxLen = 0;
  12. foreach( $functions as $name => $f )
  13. {
  14. if ( strlen( $name ) > $maxLen )
  15. {
  16. $maxLen = strlen( $name );
  17. }
  18. }
  19. echo "Showing the {$elements} most costly calls sorted by '{$sortKey}'.\n\n";
  20. echo " ", str_repeat( ' ', $maxLen - 8 ), " Inclusive Own \n";
  21. echo "function", str_repeat( ' ', $maxLen - 8 ), "#calls time memory time memory time-percall\n";
  22. echo "--------", str_repeat( '-', $maxLen - 8 ), "-----------------------------------------------------------\n";
  23. // display functions
  24. $c = 0;
  25. foreach( $functions as $name => $f ){
  26. $c++;
  27. if ( $c > $elements ){
  28. break;
  29. }
  30. printf( "%-{$maxLen}s %6d %8.4f %8d %8.4f %8d %8.4f\n", $name, $f['calls'], $f['time-inclusive'], $f['memory-inclusive'], $f['time-own'], $f['memory-own'], $f['time-own-percall'] );
  31. }
  32. function showUsage()
  33. {
  34. echo "usage:\n\tphp run-cli tracefile [sortkey] [elements]\n\n";
  35. echo "Allowed sortkeys:\n\tcalls, time-inclusive, memory-inclusive, time-own, memory-own\n";
  36. die();
  37. }
  38. class drXdebugTraceFileParser
  39. {
  40. protected $handle;
  41. /**
  42. * Stores the last function, time and memory for the entry point per
  43. * stack depth. int=>array(string, float, int).
  44. */
  45. protected $stack;
  46. /**
  47. * Stores per function the total time and memory increases and calls
  48. * string=>array(float, int, int)
  49. */
  50. protected $functions;
  51. /**
  52. * Stores which functions are on the stack
  53. */
  54. protected $stackFunctions;
  55. public function __construct( $fileName ){
  56. $this->handle = fopen( $fileName, 'r' );
  57. if ( !$this->handle ){
  58. throw new Exception( "Can't open '$fileName'" );
  59. }
  60. $this->stack[-1] = array( '', 0, 0, 0, 0 );
  61. $this->stack[ 0] = array( '', 0, 0, 0, 0 );
  62. $this->stackFunctions = array();
  63. $header1 = fgets( $this->handle );
  64. $header2 = fgets( $this->handle );
  65. if ( !preg_match( '@Version: 2.*@', $header1 ) || !preg_match( '@File format: 2@', $header2 ) ){
  66. echo "\nThis file is not an Xdebug trace file made with format option '1'.\n";
  67. showUsage();
  68. }
  69. }
  70. public function parse()
  71. {
  72. echo "\nparsing...\n";
  73. $c = 0;
  74. $size = fstat( $this->handle );
  75. $size = $size['size'];
  76. $read = 0;
  77. while ( !feof( $this->handle ) )
  78. {
  79. $buffer = fgets( $this->handle, 4096 );
  80. $read += strlen( $buffer );
  81. $this->parseLine( $buffer );
  82. $c++;
  83. if ( $c % 25000 === 0 )
  84. {
  85. //printf( " (%5.2f%%)\n", ( $read / $size ) * 100 );
  86. }
  87. }
  88. echo "\nDone.\n\n";
  89. }
  90. private function parseLine( $line )
  91. {
  92. /*
  93. if ( preg_match( '@^Version: (.*)@', $line, $matches ) )
  94. {
  95. }
  96. else if ( preg_match( '@^File format: (.*)@', $line, $matches ) )
  97. {
  98. }
  99. else if ( preg_match( '@^TRACE.*@', $line, $matches ) )
  100. {
  101. }
  102. else // assume a normal line
  103. */
  104. {
  105. $parts = explode( "\t", $line );
  106. if ( count( $parts ) < 5 )
  107. {
  108. return;
  109. }
  110. $depth = $parts[0];
  111. $funcNr = $parts[1];
  112. $time = $parts[3];
  113. $memory = $parts[4];
  114. if ( $parts[2] == '0' ) // function entry
  115. {
  116. $funcName = $parts[5];
  117. $intFunc = $parts[6];
  118. $this->stack[$depth] = array( $funcName, $time, $memory, 0, 0 );
  119. array_push( $this->stackFunctions, $funcName );
  120. }
  121. else if ( $parts[2] == '1' ) // function exit
  122. {
  123. list( $funcName, $prevTime, $prevMem, $nestedTime, $nestedMemory ) = $this->stack[$depth];
  124. // collapse data onto functions array
  125. $dTime = $time - $prevTime;
  126. $dMemory = $memory - $prevMem;
  127. $this->stack[$depth - 1][3] += $dTime;
  128. $this->stack[$depth - 1][4] += $dMemory;
  129. array_pop( $this->stackFunctions );
  130. $this->addToFunction( $funcName, $dTime, $dMemory, $nestedTime, $nestedMemory );
  131. }
  132. }
  133. }
  134. protected function addToFunction( $function, $time, $memory, $nestedTime, $nestedMemory )
  135. {
  136. if ( !isset( $this->functions[$function] ) )
  137. {
  138. $this->functions[$function] = array( 0, 0, 0, 0, 0 );
  139. }
  140. $elem = &$this->functions[$function];
  141. $elem[0]++;
  142. if ( !in_array( $function, $this->stackFunctions ) ) {
  143. $elem[1] += $time;
  144. $elem[2] += $memory;
  145. $elem[3] += $nestedTime;
  146. $elem[4] += $nestedMemory;
  147. }
  148. }
  149. public function getFunctions( $sortKey = null )
  150. {
  151. $result = array();
  152. foreach ( $this->functions as $name => $function )
  153. {
  154. $result[$name] = array(
  155. 'calls' => $function[0],
  156. 'time-inclusive' => $function[1],
  157. 'memory-inclusive' => $function[2],
  158. //'time-children' => $function[3],
  159. //'memory-children' => $function[4],
  160. 'time-own' => $function[1] - $function[3],
  161. 'memory-own' => $function[2] - $function[4],
  162. 'time-own-percall' => ($function[1] - $function[3])/$function[0],
  163. );
  164. }
  165. if ( $sortKey !== null )
  166. {
  167. uasort( $result,
  168. function( $a, $b ) use ( $sortKey )
  169. {
  170. return ( $a[$sortKey] > $b[$sortKey] ) ? -1 : ( $a[$sortKey] < $b[$sortKey] ? 1 : 0 );
  171. }
  172. );
  173. }
  174. return $result;
  175. }
  176. }