Textpattern PHP Cross Reference Content Management Systems

Source: /textpattern/lib/class.trace.php - 163 lines - 4140 bytes - Summary - Text - Print

   1  <?php
   2  
   3  class Trace
   4  {
   5      private static $quiet = false;
   6      private $bigBang;
   7      private $memFunc;
   8      private $memPeak = 0;
   9      private $memWhere = array();
  10      private $queries = 0;
  11      private $queryTime = 0;
  12      private $trace = array();
  13      private $nest = array();
  14  
  15      public function __construct()
  16      {
  17          $this->bigBang = isset($_SERVER['REQUEST_TIME_FLOAT']) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime(true);
  18          $this->memFunc = is_callable('memory_get_peak_usage');
  19      }
  20  
  21      public static function setQuiet($quiet)
  22      {
  23          self::$quiet = $quiet;
  24      }
  25  
  26      private function traceAdd($msg, $query = false)
  27      {
  28          $trace['level'] = sizeof($this->nest);
  29          $trace['begin'] = microtime(true);
  30          $trace['query'] = $query;
  31          $trace['msg']   = $msg;
  32          array_push($this->trace, $trace);
  33      }
  34  
  35      private function isPeak()
  36      {
  37          if ($this->memFunc) {
  38              $peak = memory_get_peak_usage();
  39  
  40              if ($peak > $this->memPeak) {
  41                  $this->memPeak = $peak;
  42  
  43                  return true;
  44              }
  45          }
  46  
  47          return false;
  48      }
  49  
  50      public function start($msg, $query = false)
  51      {
  52          if (self::$quiet) {
  53              return;
  54          }
  55  
  56          $start = sizeof($this->trace);
  57  
  58          if ($this->isPeak()) {
  59              $this->memWhere = array($start-1, $start);
  60          }
  61  
  62          $this->traceAdd($msg, $query);
  63          array_push($this->nest, $start);
  64      }
  65  
  66      public function stop($msg = null)
  67      {
  68          if (self::$quiet) {
  69              return;
  70          }
  71  
  72          $start = assert_int(array_pop($this->nest));
  73          $this->trace[$start]['end'] = microtime(true);
  74  
  75          if ($this->trace[$start]['query']) {
  76              $this->queries++;
  77              $this->queryTime += $this->trace[$start]['end'] - $this->trace[$start]['begin'];
  78          }
  79  
  80          if ($this->isPeak()) {
  81              $this->memWhere = array($start);
  82  
  83              if (null !== $msg) {
  84                  array_push($this->memWhere, sizeof($this->trace));
  85              }
  86          }
  87  
  88          if (null !== $msg) {
  89              $this->traceAdd($msg);
  90          }
  91      }
  92  
  93      public function log($msg)
  94      {
  95          if (self::$quiet) {
  96              return;
  97          }
  98  
  99          $start = sizeof($this->trace);
 100  
 101          if ($this->isPeak()) {
 102              $this->memWhere = array($start-1, $start);
 103          }
 104  
 105          $this->traceAdd($msg);
 106      }
 107  
 108      private function out($str)
 109      {
 110          if (self::$quiet) {
 111              return '';
 112          }
 113  
 114          return "\n<!-- ".str_replace('--', '- - ', $str)."-->\n";
 115      }
 116  
 117      public function summary($array = false)
 118      {
 119          $summary = array(
 120              'Runtime'    => sprintf('%4.2f', (microtime(true) - $this->bigBang) * 1000).' ms',
 121              'Query time' => sprintf('%4.2f', $this->queryTime * 1000).' ms',
 122              'Queries'    => $this->queries,
 123          );
 124  
 125          if ($this->memFunc) {
 126              $summary['Memory (*)'] = ceil(memory_get_peak_usage() / 1024).' kB';
 127          }
 128  
 129          $out = "Trace summary:\n";
 130          foreach ($summary as $key => $value) {
 131              $out .= sprintf("%-10s: %s\n", $key, $value);
 132          }
 133  
 134          return $array ? $summary : $this->out($out);
 135      }
 136  
 137      public function result()
 138      {
 139          $tracelog = "Trace log:\n  Time(ms) | Duration | Trace\n";
 140          $querylog = "Query log:\nDuration | Query\n";
 141  
 142          foreach ($this->trace as $nr => $trace) {
 143              $tracelog .= (in_array($nr, $this->memWhere)) ? '*' : ' ';
 144              $tracelog .= sprintf(' %8.2f | ', ($trace['begin'] - $this->bigBang) * 1000);
 145              $line = '';
 146  
 147              if (isset($trace['end'])) {
 148                  $line .= sprintf('%8.2f | ', ($trace['end'] - $trace['begin']) * 1000);
 149              } else {
 150                  $line .= str_repeat(' ', 8).' | ';
 151              }
 152  
 153              if ($trace['query']) {
 154                  $querylog .= $line.$trace['msg']."\n";
 155              }
 156  
 157              $line .= str_repeat("\t", $trace['level']).$trace['msg']."\n";
 158              $tracelog .= $line;
 159          }
 160  
 161          return $this->out($tracelog).($this->queries ? $this->out($querylog) : '');
 162      }
 163  }

title

Description

title

Description

title

Description

title

title

Body