Exemplo n.º 1
0
 public static function output()
 {
     self::shutDown();
     if (!self::$_enabled) {
         return;
     }
     self::disable();
     self::$_logEnabled = false;
     $execTime = microtime(true) - self::$startTime;
     if (function_exists('memory_get_peak_usage')) {
         $memoryUsage = memory_get_peak_usage();
     } else {
         $memoryUsage = memory_get_usage();
     }
     $load = @file_get_contents('/proc/loadavg');
     $load = explode(' ', $load);
     $load = $load[0];
     $benchmarkOutput = array();
     if ($execTime < 1) {
         $benchmarkOutput[] = round($execTime * 1000) . " msec";
     } else {
         $benchmarkOutput[] = round($execTime, 3) . " sec";
     }
     if ($load) {
         $benchmarkOutput[] = "Load: " . $load;
     }
     $benchmarkOutput[] = "Memory: " . round($memoryUsage / 1024) . " kb";
     if (Zend_Registry::get('dao') && Zend_Registry::get('dao')->hasDb() && Zend_Registry::get('db')) {
         if (Zend_Registry::get('db')->getProfiler() && method_exists(Zend_Registry::get('db')->getProfiler(), 'getQueryCount')) {
             $benchmarkOutput[] = "DB-Queries: " . Zend_Registry::get('db')->getProfiler()->getQueryCount();
         } else {
             $benchmarkOutput[] = "DB-Queries: (no profiler used)";
         }
     } else {
         $benchmarkOutput[] = "DB-Queries: (none)";
     }
     if (PHP_SAPI != 'cli' && (Kwf_Config::getValue('debug.benchmark') || isset($_REQUEST['KWF_BENCHMARK']))) {
         $up = Kwf_Config::getValue('application.uniquePrefix');
         if ($up) {
             $up .= '-';
         }
         echo '<div class="' . $up . 'benchmarkBox" data-benchmark-type="server">';
         echo '<div class="' . $up . 'benchmarkBoxContent">';
         foreach ($benchmarkOutput as $line) {
             echo "{$line}<br />\n";
         }
     }
     if (Kwf_Config::getValue('debug.benchmarklog')) {
         $out = date('Y-m-d H:i:s') . "\n";
         $out .= Kwf_Setup::getRequestPath() . "\n";
         $out .= implode("\n", $benchmarkOutput) . "\n";
         file_put_contents('benchmarklog', $out);
     }
     if ((Kwf_Config::getValue('debug.benchmark') || isset($_REQUEST['KWF_BENCHMARK'])) && PHP_SAPI != 'cli') {
         echo self::_getCounterOutput(self::$_counter, true);
         if (self::$benchmarks) {
             echo "<br /><b>Benchmarks:</b><br/>";
             foreach (self::$benchmarks as $i) {
                 echo "<a style=\"display:block;\" href=\"#\" onclick=\"if(this.nextSibling.nextSibling.style.display=='none') { this.open=true; this.nextSibling.nextSibling.style.display='block'; this.nextSibling.style.display=''; } else { this.open=false; this.nextSibling.nextSibling.style.display='none';this.nextSibling.style.display='none'; } return(false); }\"\n                                                                onmouseover=\"if(!this.open) this.nextSibling.style.display=''\"\n                                                                onmouseout=\"if(!this.open) this.nextSibling.style.display='none'\">";
                 echo "{$i->identifier} (" . round($i->duration, 3) . " sec)</a>";
                 echo "<div style=\"display:none;margin-left:10px\">";
                 echo $i->addInfo . "<br/>";
                 echo implode('<br />', $i->getOutput());
                 echo "</div>";
                 echo "<div style=\"display:none;margin-left:10px\">";
                 echo self::_getCounterOutput($i->counter, true);
                 echo "</div>";
             }
         }
     }
     if (Kwf_Config::getValue('debug.benchmarklog')) {
         $out = self::_getCounterOutput(self::$_counter, false);
         if (self::$benchmarks) {
             $out .= "\nBenchmarks:\n";
             foreach (self::$benchmarks as $i) {
                 $out .= "{$i->identifier} (" . round($i->duration, 3) . " sec)\n";
                 $out .= "    " . $i->addInfo . "\n";
                 $out .= implode("\n    ", $i->getOutput());
                 $out .= self::_getCounterOutput($i->counter, true);
                 $out .= "\n";
             }
         }
         file_put_contents('benchmarklog', $out, FILE_APPEND);
     }
     if ((Kwf_Config::getValue('debug.benchmark') || isset($_REQUEST['KWF_BENCHMARK'])) && PHP_SAPI != 'cli') {
         echo "<table style=\"font-size: 10px\">";
         echo "<tr><th>ms</th><th>%</th><th>Checkpoint</th></tr>";
         $sum = 0;
         foreach (self::$_checkpoints as $checkpoint) {
             $sum += $checkpoint[0];
         }
         foreach (self::$_checkpoints as $i => $checkpoint) {
             echo "<tr>";
             echo "<td>" . round($checkpoint[0] * 1000) . "</td>";
             echo "<td>" . round($checkpoint[0] / $sum * 100) . "</td>";
             echo "<td>" . $checkpoint[1] . "</td>";
             echo "</tr>";
             if (isset(self::$_subCheckpoints[$i])) {
                 $subCheckpoints = array();
                 foreach (self::$_subCheckpoints[$i] as $cp) {
                     $subCheckpoints[0][] = $cp[0];
                     $subCheckpoints[1][] = $cp[1];
                 }
                 array_multisort($subCheckpoints[0], SORT_DESC, SORT_NUMERIC, $subCheckpoints[1]);
                 foreach (array_keys($subCheckpoints[0]) as $k) {
                     $percent = $subCheckpoints[0][$k] / $sum * 100;
                     if ($percent > 1) {
                         echo "<tr>";
                         echo "<td>" . round($subCheckpoints[0][$k] * 1000) . "</td>";
                         echo "<td>" . round($percent) . "</td>";
                         echo "<td>&nbsp;&nbsp;" . $subCheckpoints[1][$k] . "</td>";
                         echo "</tr>";
                     }
                 }
             }
         }
         echo "</table>";
         echo "</div>";
         echo "</div>";
     }
     if (Kwf_Config::getValue('debug.benchmarklog')) {
         $out = "\n" . self::getCheckpointOutput();
         file_put_contents('benchmarklog', $out, FILE_APPEND);
     }
 }