Permalink
Browse files

changes to the display of profile info. moving it to a route.

  • Loading branch information...
1 parent 3e62b74 commit a306cbe802738d396035f3e2730eaabdcb0b0782 @tmaiaroto committed Jan 7, 2012
@@ -5,9 +5,6 @@
//$li3_perf_start = microtime(true);
Data::set('timers', array('li3_perf_start' => microtime(true)));
-// Include webgrind
-require LITHIUM_APP_PATH . '/libraries/li3_perf/extensions/webgrind/library/bootstrap.php';
-
// Get the queries
require __DIR__ . '/bootstrap/queries.php';
@@ -36,6 +36,10 @@
// Apply a filter that will render the toolbar and mark some timers.
Dispatcher::applyFilter('run', function($self, $params, $chain) {
+ if(substr($params['request']->url, 0, 17) == '/li3_perf/profile') {
+ return $chain->next($self, $params, $chain);
+ }
+
Data::append('timers', array('li3_perf_start_dispatch' => microtime(true)));
$result = $chain->next($self, $params, $chain);
View
@@ -3,6 +3,8 @@
use lithium\action\Response;
use lithium\core\Environment;
use li3_perf\extensions\util\Asset;
+use li3_perf\extensions\webgrind\library\Webgrind;
+use li3_perf\extensions\webgrind\library\FileHandler;
// Assets (can't symlink in the VM because Windows host)
Router::connect("/{:library}/{:asset_type:js|img|css}/{:args}", array(), function($request) {
@@ -14,28 +16,128 @@
// This one is cool. It outputs the last few lines of a log file.
// If CCZE is installed the log output will be in HTML and styles can be altered easily with CSS.
Router::connect('/li3_perf/tail/{:file}/{:lines}', array('lines' => 25, 'file' => LITHIUM_APP_PATH . '/resources/tmp/logs/debug.log'), function($request) {
- $lines = $request->params['lines'];
- $logfile = $request->params['file'];
-
- header("Content-type: text/html");
- // echo `tail -n 50 /var/log/php-fpm/www-error.log | ccze -h`;
+ $lines = $request->params['lines'];
+ $logfile = $request->params['file'];
+
+ header("Content-type: text/html");
+ // echo `tail -n 50 /var/log/php-fpm/www-error.log | ccze -h`;
+
+ $options = '-n ' . $lines;
+ $command = 'tail ' . $options . ' ' . $logfile . ' | ccze -h';
+ $output = shell_exec($command);
+
+ if($output == null) {
+ $output = '';
+ $command = 'tail ' . $options . ' ' . $logfile;
+ $lines = explode("\n", shell_exec($command));
+ if(!empty($lines)) {
+ foreach($lines as $line) {
+ $output .= $line . "<br />";
+ }
+ }
+ }
+
+ echo $output;
+ exit();
+});
+
+Router::connect('/{:library}/profile/trace/{:file}', array('file' => 'latest'), function($request) {
+ // If for some reason a profile session is active, do NOT run the code in this route.
+ // It would cause some pretty big issues =)
+ if(isset($_GET['XDEBUG_PROFILE']) || isset($_COOKIE['XDEBUG_PROFILE'])) {
+ echo 'You can\'t attempt to get the latest profile information while the profiler is active.';
+ return false;
+ }
+
+ // webgrind bootstrap/config process
+ require LITHIUM_APP_PATH . '/libraries/li3_perf/extensions/webgrind/library/bootstrap.php';
+
+ $trace_files = FileHandler::getInstance()->getTraceList(1);
+ if(is_array($trace_files)) {
+ $dataFile = $trace_files[0]['filename'];
+ // I've seen this work before and then sometimes not...Sometimes it needs the slash. Weird.
+ if(!file_exists(Webgrind::$config->xdebugOutputDir . $dataFile)){
+ $dataFile = '/' . $dataFile;
+ }
+ $costFormat = Webgrind::$config->defaultCostformat;
+ $reader = FileHandler::getInstance()->getTraceReader(
+ $dataFile, $costFormat
+ );
- $options = '-n ' . $lines;
- $command = 'tail ' . $options . ' ' . $logfile . ' | ccze -h';
- $output = shell_exec($command);
+ $result = array();
+ $functions = array();
+ $shownTotal = 0;
+ $breakdown = array('internal' => 0, 'procedural' => 0, 'class' => 0, 'include' => 0);
+ $functionCount = $reader->getFunctionCount();
- if($output == null) {
- $output = '';
- $command = 'tail ' . $options . ' ' . $logfile;
- $lines = explode("\n", shell_exec($command));
- if(!empty($lines)) {
- foreach($lines as $line) {
- $output .= $line . "<br />";
+ $result['moodpik'] = array('function_calls' => 0, 'total_cost' => 0);
+ for ($i = 0; $i < $functionCount; $i++) {
+ $functionInfo = $reader->getFunctionInfo($i);
+ //var_dump($functionInfo['functionName']);
+ if(strstr($functionInfo['functionName'], 'moodpik\\')) {
+ $result['moodpik']['function_calls']++;
+ $result['moodpik']['total_cost'] += $functionInfo['summedSelfCost'];
+ }
+
+ $isInternal = (strpos($functionInfo['functionName'], 'php::') !== false);
+
+ if ($isInternal) {
+ if (get('hideInternals', false)) {
+ continue;
}
+
+ $breakdown['internal'] += $functionInfo['summedSelfCost'];
+ $humanKind = 'internal';
+ } elseif (false !== strpos($functionInfo['functionName'], 'require_once::') ||
+ false !== strpos($functionInfo['functionName'], 'require::') ||
+ false !== strpos($functionInfo['functionName'], 'include_once::') ||
+ false !== strpos($functionInfo['functionName'], 'include::')) {
+ $breakdown['include'] += $functionInfo['summedSelfCost'];
+ $humanKind = 'include';
+ } elseif (false !== strpos($functionInfo['functionName'], '->') ||
+ false !== strpos($functionInfo['functionName'], '::')) {
+ $breakdown['class'] += $functionInfo['summedSelfCost'];
+ $humanKind = 'class';
+ } else {
+ $breakdown['procedural'] += $functionInfo['summedSelfCost'];
+ $humanKind = 'procedural';
}
+
+ $shownTotal += $functionInfo['summedSelfCost'];
+ $functions[$i] = $functionInfo;
+ $functions[$i]['nr'] = $i;
+ $functions[$i]['humanKind'] = $humanKind;
}
-
- echo $output;
+
+ usort($functions, 'costCmp');
+
+ $remainingCost = $shownTotal * get('showFraction');
+
+ $result['functions'] = array();
+ foreach ($functions as $function) {
+
+ $remainingCost -= $function['summedSelfCost'];
+ $function['file'] = urlencode($function['file']);
+ $result['functions'][] = $function;
+ if ($remainingCost < 0) {
+ break;
+ }
+ }
+
+ $result['summedInvocationCount'] = $reader->getFunctionCount();
+ $result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec');
+ $result['dataFile'] = $dataFile;
+ $result['invokeUrl'] = $reader->getHeader('cmd');
+ $result['runs'] = $reader->getHeader('runs');
+ $result['breakdown'] = $breakdown;
+ $result['mtime'] = date(Webgrind::$config->dateFormat, filemtime(Webgrind::$config->xdebugOutputDir.'/'.$dataFile));
+
+ $creator = preg_replace('/[^0-9\.]/', '', $reader->getHeader('creator'));
+ $result['linkToFunctionLine'] = version_compare($creator, '2.1') > 0;
+
+
+ var_dump($result);
exit();
- });
+ }
+});
?>
@@ -0,0 +1,108 @@
+<?php
+/**
+ * Lithium: the most rad php framework
+ *
+ * @copyright Copyright 2011, Union of RAD (http://union-of-rad.org)
+ * @license http://opensource.org/licenses/bsd-license.php The BSD License
+ */
+
+use lithium\analysis\Debugger;
+use lithium\analysis\Inspector;
+
+$exception = $info['exception'];
+$replace = array('&lt;?php', '?&gt;', '<code>', '</code>', "\n");
+$context = 5;
+
+/**
+ * Set Lithium-esque colors for syntax highlighing.
+ */
+ini_set('highlight.string', '#4DDB4A');
+ini_set('highlight.comment', '#D42AAE');
+ini_set('highlight.keyword', '#D42AAE');
+ini_set('highlight.default', '#3C96FF');
+ini_set('highlight.htm', '#FFFFFF');
+
+$stack = Debugger::trace(array('format' => 'array', 'trace' => $exception->getTrace()));
+
+array_unshift($stack, array(
+ 'functionRef' => '[exception]',
+ 'file' => $exception->getFile(),
+ 'line' => $exception->getLine()
+));
+
+?>
+<h3>Exception</h3>
+
+<div class="lithium-exception-class">
+ <?=get_class($exception);?>
+
+ <?php if ($code = $exception->getCode()): ?>
+ <span class="code">(code <?=$code; ?>)</span>
+ <?php endif ?>
+</div>
+
+<div class="lithium-exception-message"><?=$exception->getMessage(); ?></div>
+
+<h3 id="source">Source</h3>
+
+<div id="sourceCode"></div>
+
+<h3>Stack Trace</h3>
+
+<div class="lithium-stack-trace">
+ <ol>
+ <?php foreach ($stack as $id => $frame): ?>
+ <?php
+ $location = "{$frame['file']}: {$frame['line']}";
+ $lines = range($frame['line'] - $context, $frame['line'] + $context);
+ $code = Inspector::lines($frame['file'], $lines);
+ ?>
+ <li>
+ <tt><a href="#source" id="<?=$id; ?>" class="display-source-excerpt">
+ <?=$frame['functionRef']; ?>
+ </a></tt>
+ <div id="sourceCode<?=$id; ?>" style="display: none;">
+
+ <div class="lithium-exception-location">
+ <?=$location; ?>
+ </div>
+
+ <div class="lithium-code-dump">
+ <pre><code><?php
+ foreach ($code as $num => $content):
+ $numPad = str_pad($num, 3, ' ');
+ $content = str_ireplace(array('<?php', '?>'), '', $content);
+ $content = highlight_string("<?php {$numPad}{$content} ?>", true);
+ $content = str_replace($replace, '', $content);
+
+ if ($frame['line'] === $num):
+ ?><span class="code-highlight"><?php
+ endif;?><?php echo "{$content}\n"; ?><?php
+ if ($frame['line'] === $num):
+ ?></span><?php
+ endif;
+
+ endforeach;
+ ?></code></pre>
+ </div>
+ </div>
+ </li>
+ <?php endforeach; ?>
+ </ol>
+</div>
+
+<script type="text/javascript">
+ window.onload = function() {
+ var $ = function() { return document.getElementById.apply(document, arguments); };
+ var links = document.getElementsByTagName('a');
+
+ for (i = 0; i < links.length; i++) {
+ if (links[i].className.indexOf('display-source-excerpt') >= 0) {
+ links[i].onclick = function() {
+ $('sourceCode').innerHTML = $('sourceCode' + this.id).innerHTML;
+ }
+ }
+ }
+ $('sourceCode').innerHTML = $('sourceCode0').innerHTML;
+ }
+</script>
@@ -3,83 +3,4 @@
This page displays the performance over time.
</p>
<?php
-use li3_perf\extensions\webgrind\library\Webgrind;
-use li3_perf\extensions\webgrind\library\FileHandler;
-
-$trace_files = FileHandler::getInstance()->getTraceList(1);
-if(is_array($trace_files)) {
- $dataFile = $trace_files[0]['filename'];
-
- $costFormat = Webgrind::$config->defaultCostformat;
- $reader = FileHandler::getInstance()->getTraceReader(
- $dataFile, $costFormat
- );
-
- $functions = array();
- $shownTotal = 0;
- $breakdown = array('internal' => 0, 'procedural' => 0, 'class' => 0, 'include' => 0);
- $functionCount = $reader->getFunctionCount();
-
- for ($i = 0; $i < $functionCount; $i++) {
- $functionInfo = $reader->getFunctionInfo($i);
- $isInternal = (strpos($functionInfo['functionName'], 'php::') !== false);
-
- if ($isInternal) {
- if (get('hideInternals', false)) {
- continue;
- }
-
- $breakdown['internal'] += $functionInfo['summedSelfCost'];
- $humanKind = 'internal';
- } elseif (false !== strpos($functionInfo['functionName'], 'require_once::') ||
- false !== strpos($functionInfo['functionName'], 'require::') ||
- false !== strpos($functionInfo['functionName'], 'include_once::') ||
- false !== strpos($functionInfo['functionName'], 'include::')) {
- $breakdown['include'] += $functionInfo['summedSelfCost'];
- $humanKind = 'include';
- } elseif (false !== strpos($functionInfo['functionName'], '->') ||
- false !== strpos($functionInfo['functionName'], '::')) {
- $breakdown['class'] += $functionInfo['summedSelfCost'];
- $humanKind = 'class';
- } else {
- $breakdown['procedural'] += $functionInfo['summedSelfCost'];
- $humanKind = 'procedural';
- }
-
- $shownTotal += $functionInfo['summedSelfCost'];
- $functions[$i] = $functionInfo;
- $functions[$i]['nr'] = $i;
- $functions[$i]['humanKind'] = $humanKind;
- }
-
- usort($functions, 'costCmp');
-
- $remainingCost = $shownTotal * get('showFraction');
-
- $result['functions'] = array();
- foreach ($functions as $function) {
-
- $remainingCost -= $function['summedSelfCost'];
- $function['file'] = urlencode($function['file']);
- $result['functions'][] = $function;
- if ($remainingCost < 0) {
- break;
- }
- }
-
- $result['summedInvocationCount'] = $reader->getFunctionCount();
- $result['summedRunTime'] = $reader->formatCost($reader->getHeader('summary'), 'msec');
- $result['dataFile'] = $dataFile;
- $result['invokeUrl'] = $reader->getHeader('cmd');
- $result['runs'] = $reader->getHeader('runs');
- $result['breakdown'] = $breakdown;
- $result['mtime'] = date(Webgrind::$config->dateFormat, filemtime(Webgrind::$config->xdebugOutputDir.'/'.$dataFile));
-
- $creator = preg_replace('/[^0-9\.]/', '', $reader->getHeader('creator'));
- $result['linkToFunctionLine'] = version_compare($creator, '2.1') > 0;
-
-
- var_dump($result);
-
-}
?>
@@ -8,7 +8,7 @@
?>
<h2>Time to Load</h2>
-<?php var_dump(xdebug_time_index()); ?>
+<?php //var_dump(xdebug_time_index()); ?>
<div id="holder" style="height: 205px;"></div>
<p>Note: The time to route and call the code for the request should just about equal the complete dispatch cycle. If not, something is wrong.</p>
Oops, something went wrong.

0 comments on commit a306cbe

Please sign in to comment.