| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,7 +82,7 @@ | |||
| 82 | 82 | ||
| 83 | 83 | include txpath.'/lib/constants.php'; | |
| 84 | 84 | include txpath.'/lib/txplib_misc.php'; | |
| 85 | - trace_log(TEXTPATTERN_TRACE_START); | ||
| 85 | + $trace = new Trace(); | ||
| 86 | 86 | ||
| 87 | 87 | $nolog = 1; | |
| 88 | 88 | ||
@@ -97,7 +97,5 @@ | |||
| 97 | 97 | output_css($s, $n); | |
| 98 | 98 | ||
| 99 | 99 | if ($production_status === 'debug') { | |
| 100 | - echo n.'/*'; | ||
| 101 | - trace_log(TEXTPATTERN_TRACE_DISPLAY); | ||
| 102 | - echo n.'*/'.n; | ||
| 100 | + echo n.'/*' . $trace->result() . n.'*/'.n; | ||
| 103 | 101 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -82,9 +82,10 @@ | |||
| 82 | 82 | ob_end_clean(); | |
| 83 | 83 | } | |
| 84 | 84 | ||
| 85 | + include txpath.'/lib/class.trace.php'; | ||
| 86 | + $trace = new Trace(); | ||
| 85 | 87 | include txpath.'/lib/constants.php'; | |
| 86 | 88 | include txpath.'/lib/txplib_misc.php'; | |
| 87 | - trace_log(TEXTPATTERN_TRACE_START); | ||
| 88 | 89 | ||
| 89 | 90 | if (!isset($txpcfg['table_prefix'])) { | |
| 90 | 91 | txp_status_header('503 Service Unavailable'); | |
@@ -99,4 +100,10 @@ | |||
| 99 | 100 | ||
| 100 | 101 | include txpath.'/publish.php'; | |
| 101 | 102 | textpattern(); | |
| 102 | - trace_log(TEXTPATTERN_TRACE_DISPLAY); | ||
| 103 | + | ||
| 104 | + if ($production_status !== 'live') { | ||
| 105 | + echo $trace->summary(); | ||
| 106 | + if ($production_status === 'debug') { | ||
| 107 | + echo $trace->result(); | ||
| 108 | + } | ||
| 109 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -80,11 +80,11 @@ | |||
| 80 | 80 | ||
| 81 | 81 | error_reporting(E_ALL | E_STRICT); | |
| 82 | 82 | @ini_set("display_errors", "1"); | |
| 83 | + include txpath.'/lib/class.trace.php'; | ||
| 84 | + $trace = new Trace(); | ||
| 83 | 85 | include_once txpath.'/lib/constants.php'; | |
| 84 | 86 | include txpath.'/lib/txplib_misc.php'; | |
| 85 | 87 | ||
| 86 | - trace_log(TEXTPATTERN_TRACE_START); | ||
| 87 | - | ||
| 88 | 88 | include txpath.'/vendors/Textpattern/Loader.php'; | |
| 89 | 89 | ||
| 90 | 90 | $loader = new Textpattern_Loader(txpath.'/vendors'); | |
@@ -93,13 +93,14 @@ | |||
| 93 | 93 | $loader = new Textpattern_Loader(txpath.'/lib'); | |
| 94 | 94 | $loader->register(); | |
| 95 | 95 | ||
| 96 | + $trace->start('[Static PHP includes]'); | ||
| 96 | 97 | include txpath.'/lib/txplib_db.php'; | |
| 97 | 98 | include txpath.'/lib/txplib_forms.php'; | |
| 98 | 99 | include txpath.'/lib/txplib_html.php'; | |
| 99 | 100 | include txpath.'/lib/txplib_theme.php'; | |
| 100 | 101 | include txpath.'/lib/txplib_validator.php'; | |
| 101 | 102 | include txpath.'/lib/admin_config.php'; | |
| 102 | - trace_add('[PHP Include end]'); | ||
| 103 | + $trace->stop(); | ||
| 103 | 104 | ||
| 104 | 105 | set_error_handler('adminErrorHandler', error_reporting()); | |
| 105 | 106 | ||
@@ -219,7 +220,8 @@ | |||
| 219 | 220 | end_page(); | |
| 220 | 221 | ||
| 221 | 222 | if ($app_mode != 'async') { | |
| 222 | - trace_log(TEXTPATTERN_TRACE_DISPLAY); | ||
| 223 | + echo $trace->summary(); | ||
| 224 | + echo $trace->result(); | ||
| 223 | 225 | } else { | |
| 224 | 226 | $trace = trace_log(TEXTPATTERN_TRACE_RESULT); | |
| 225 | 227 | header('X-Textpattern-Runtime: ' . @$trace['microdiff']); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,155 @@ | |||
| 1 | + <?php | ||
| 2 | + | ||
| 3 | + class Trace { | ||
| 4 | + private static $quiet = false; | ||
| 5 | + private $bigBang; | ||
| 6 | + private $memFunc; | ||
| 7 | + private $memPeak = 0; | ||
| 8 | + private $memWhere = array(); | ||
| 9 | + private $queries = 0; | ||
| 10 | + private $queryTime = 0; | ||
| 11 | + private $trace = array(); | ||
| 12 | + private $nest = array(); | ||
| 13 | + | ||
| 14 | + public function __construct() | ||
| 15 | + { | ||
| 16 | + $this->bigBang = $this->getmicrotime(); | ||
| 17 | + $this->memFunc = is_callable('memory_get_peak_usage'); | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + public static function setQuiet($quiet) | ||
| 21 | + { | ||
| 22 | + self::$quiet = $quiet; | ||
| 23 | + } | ||
| 24 | + | ||
| 25 | + function getmicrotime() | ||
| 26 | + { | ||
| 27 | + list($usec, $sec) = explode(" ", microtime()); | ||
| 28 | + | ||
| 29 | + return ((float) $usec + (float) $sec); | ||
| 30 | + } | ||
| 31 | + | ||
| 32 | + private function traceAdd($msg, $query = false) | ||
| 33 | + { | ||
| 34 | + $trace['level'] = sizeof($this->nest); | ||
| 35 | + $trace['begin'] = $this->getmicrotime(); | ||
| 36 | + $trace['query'] = $query; | ||
| 37 | + $trace['msg'] = $msg; | ||
| 38 | + array_push($this->trace, $trace); | ||
| 39 | + } | ||
| 40 | + | ||
| 41 | + private function isPeak() | ||
| 42 | + { | ||
| 43 | + if ($this->memFunc) { | ||
| 44 | + $peak = memory_get_peak_usage(); | ||
| 45 | + | ||
| 46 | + if ($peak > $this->memPeak) { | ||
| 47 | + $this->memPeak = $peak; | ||
| 48 | + return true; | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + return false; | ||
| 53 | + } | ||
| 54 | + | ||
| 55 | + public function start($msg, $query = false) | ||
| 56 | + { | ||
| 57 | + if (self::$quiet) return; | ||
| 58 | + | ||
| 59 | + $start = sizeof($this->trace); | ||
| 60 | + | ||
| 61 | + if ($this->isPeak()) { | ||
| 62 | + $this->memWhere = array($start-1, $start); | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + $this->traceAdd($msg, $query); | ||
| 66 | + array_push($this->nest, $start); | ||
| 67 | + } | ||
| 68 | + | ||
| 69 | + public function stop($msg = null) | ||
| 70 | + { | ||
| 71 | + if (self::$quiet) return; | ||
| 72 | + | ||
| 73 | + $start = array_pop($this->nest); | ||
| 74 | + $this->trace[$start]['end'] = $this->getmicrotime(); | ||
| 75 | + | ||
| 76 | + if ($this->trace[$start]['query']) { | ||
| 77 | + $this->queries++; | ||
| 78 | + $this->queryTime += $this->trace[$start]['end'] - $this->trace[$start]['begin']; | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + if ($this->isPeak()) { | ||
| 82 | + $this->memWhere = array($start); | ||
| 83 | + | ||
| 84 | + if (null !== $msg) { | ||
| 85 | + array_push($this->memWhere, sizeof($this->trace)); | ||
| 86 | + } | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + if (null !== $msg) { | ||
| 90 | + $this->traceAdd($msg); | ||
| 91 | + } | ||
| 92 | + } | ||
| 93 | + | ||
| 94 | + public function log($msg) | ||
| 95 | + { | ||
| 96 | + if (self::$quiet) return; | ||
| 97 | + | ||
| 98 | + $start = sizeof($this->trace); | ||
| 99 | + | ||
| 100 | + if ($this->isPeak()) { | ||
| 101 | + $this->memWhere = array($start-1, $start); | ||
| 102 | + } | ||
| 103 | + | ||
| 104 | + $this->traceAdd($msg); | ||
| 105 | + } | ||
| 106 | + | ||
| 107 | + private function out($str) | ||
| 108 | + { | ||
| 109 | + if (self::$quiet) return ''; | ||
| 110 | + | ||
| 111 | + return "\n<!-- " . str_replace('--', '- - ', $str) . "-->\n"; | ||
| 112 | + } | ||
| 113 | + | ||
| 114 | + public function summary() | ||
| 115 | + { | ||
| 116 | + $out = "Trace summary:\n"; | ||
| 117 | + $out .= "Runtime : ". sprintf('%5.3f', ($this->getmicrotime() - $this->bigBang) * 1000) . " ms\n"; | ||
| 118 | + $out .= "Query time: ". sprintf('%5.3f', $this->queryTime * 1000) . " ms\n"; | ||
| 119 | + $out .= "Queries : ". $this->queries . "\n"; | ||
| 120 | + | ||
| 121 | + if ($this->memFunc) { | ||
| 122 | + $out .= "Memory (*): ". ceil(memory_get_peak_usage() / 1024) . " kB\n"; | ||
| 123 | + } | ||
| 124 | + | ||
| 125 | + return $this->out($out); | ||
| 126 | + } | ||
| 127 | + | ||
| 128 | + public function result() | ||
| 129 | + { | ||
| 130 | + $tracelog = "Trace log:\n Time(ms) | Duration | Trace\n"; | ||
| 131 | + $querylog = "Query log:\nDuration | Query\n"; | ||
| 132 | + | ||
| 133 | + foreach($this->trace as $nr => $trace) { | ||
| 134 | + $tracelog .= (in_array($nr, $this->memWhere)) ? '*' : ' '; | ||
| 135 | + $tracelog .= sprintf(' %8.3f | ', ($trace['begin'] - $this->bigBang) * 1000); | ||
| 136 | + $line = ''; | ||
| 137 | + | ||
| 138 | + if (isset($trace['end'])) { | ||
| 139 | + $line .= sprintf('%8.3f | ', ($trace['end'] - $trace['begin']) * 1000); | ||
| 140 | + } | ||
| 141 | + else { | ||
| 142 | + $line .= str_repeat(' ', 8) . ' | '; | ||
| 143 | + } | ||
| 144 | + | ||
| 145 | + if ($trace['query']) { | ||
| 146 | + $querylog .= $line . $trace['msg'] . "\n"; | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + $line .= str_repeat("\t", $trace['level']) . $trace['msg'] . "\n"; | ||
| 150 | + $tracelog .= $line; | ||
| 151 | + } | ||
| 152 | + | ||
| 153 | + return $this->out($tracelog).($this->queries ? $this->out($querylog) : ''); | ||
| 154 | + } | ||
| 155 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -368,7 +368,7 @@ function safe_escape_like($in = '') | |||
| 368 | 368 | ||
| 369 | 369 | function safe_query($q = '', $debug = false, $unbuf = false) | |
| 370 | 370 | { | |
| 371 | - global $DB, $txpcfg, $txptrace_qcount, $txptrace_qtime, $production_status; | ||
| 371 | + global $DB, $txpcfg, $trace, $production_status; | ||
| 372 | 372 | $method = ($unbuf) ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT; | |
| 373 | 373 | ||
| 374 | 374 | if (!$q) { | |
@@ -379,18 +379,14 @@ function safe_query($q = '', $debug = false, $unbuf = false) | |||
| 379 | 379 | dmp($q); | |
| 380 | 380 | } | |
| 381 | 381 | ||
| 382 | - $start = getmicrotime(); | ||
| 382 | + $trace->start("[SQL: $q ]", true); | ||
| 383 | 383 | $result = mysqli_query($DB->link, $q, $method); | |
| 384 | - $time = getmicrotime() - $start; | ||
| 385 | - @$txptrace_qtime += $time; | ||
| 386 | - @$txptrace_qcount++; | ||
| 384 | + $trace->stop(); | ||
| 387 | 385 | ||
| 388 | 386 | if ($result === false) { | |
| 389 | 387 | trigger_error(mysqli_error($DB->link), E_USER_ERROR); | |
| 390 | 388 | } | |
| 391 | 389 | ||
| 392 | - trace_add('[SQL ('.number_format($time, 6, '.', '')."): $q]"); | ||
| 393 | - | ||
| 394 | 390 | if (!$result) { | |
| 395 | 391 | return false; | |
| 396 | 392 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments