FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Convert trace functions to class · textpattern/textpattern@4dd9b23 · GitHub

Commit 4dd9b23

Browse files
committed
Convert trace functions to class
1 parent b9babfd commit 4dd9b23

10 files changed

Lines changed: 233 additions & 60 deletions

File tree

‎css.php‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282

8383
include txpath.'/lib/constants.php';
8484
include txpath.'/lib/txplib_misc.php';
85-
trace_log(TEXTPATTERN_TRACE_START);
85+
$trace = new Trace();
8686

8787
$nolog = 1;
8888

@@ -97,7 +97,5 @@
9797
output_css($s, $n);
9898

9999
if ($production_status === 'debug') {
100-
echo n.'/*';
101-
trace_log(TEXTPATTERN_TRACE_DISPLAY);
102-
echo n.'*/'.n;
100+
echo n.'/*' . $trace->result() . n.'*/'.n;
103101
}

‎index.php‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@
8282
ob_end_clean();
8383
}
8484

85+
include txpath.'/lib/class.trace.php';
86+
$trace = new Trace();
8587
include txpath.'/lib/constants.php';
8688
include txpath.'/lib/txplib_misc.php';
87-
trace_log(TEXTPATTERN_TRACE_START);
8889

8990
if (!isset($txpcfg['table_prefix'])) {
9091
txp_status_header('503 Service Unavailable');
@@ -99,4 +100,10 @@
99100

100101
include txpath.'/publish.php';
101102
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+
}

‎textpattern/index.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,11 @@
8080

8181
error_reporting(E_ALL | E_STRICT);
8282
@ini_set("display_errors", "1");
83+
include txpath.'/lib/class.trace.php';
84+
$trace = new Trace();
8385
include_once txpath.'/lib/constants.php';
8486
include txpath.'/lib/txplib_misc.php';
8587

86-
trace_log(TEXTPATTERN_TRACE_START);
87-
8888
include txpath.'/vendors/Textpattern/Loader.php';
8989

9090
$loader = new Textpattern_Loader(txpath.'/vendors');
@@ -93,13 +93,14 @@
9393
$loader = new Textpattern_Loader(txpath.'/lib');
9494
$loader->register();
9595

96+
$trace->start('[Static PHP includes]');
9697
include txpath.'/lib/txplib_db.php';
9798
include txpath.'/lib/txplib_forms.php';
9899
include txpath.'/lib/txplib_html.php';
99100
include txpath.'/lib/txplib_theme.php';
100101
include txpath.'/lib/txplib_validator.php';
101102
include txpath.'/lib/admin_config.php';
102-
trace_add('[PHP Include end]');
103+
$trace->stop();
103104

104105
set_error_handler('adminErrorHandler', error_reporting());
105106

@@ -219,7 +220,8 @@
219220
end_page();
220221

221222
if ($app_mode != 'async') {
222-
trace_log(TEXTPATTERN_TRACE_DISPLAY);
223+
echo $trace->summary();
224+
echo $trace->result();
223225
} else {
224226
$trace = trace_log(TEXTPATTERN_TRACE_RESULT);
225227
header('X-Textpattern-Runtime: ' . @$trace['microdiff']);

‎textpattern/lib/class.trace.php‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
}

‎textpattern/lib/txplib_db.php‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ function safe_escape_like($in = '')
368368

369369
function safe_query($q = '', $debug = false, $unbuf = false)
370370
{
371-
global $DB, $txpcfg, $txptrace_qcount, $txptrace_qtime, $production_status;
371+
global $DB, $txpcfg, $trace, $production_status;
372372
$method = ($unbuf) ? MYSQLI_USE_RESULT : MYSQLI_STORE_RESULT;
373373

374374
if (!$q) {
@@ -379,18 +379,14 @@ function safe_query($q = '', $debug = false, $unbuf = false)
379379
dmp($q);
380380
}
381381

382-
$start = getmicrotime();
382+
$trace->start("[SQL: $q ]", true);
383383
$result = mysqli_query($DB->link, $q, $method);
384-
$time = getmicrotime() - $start;
385-
@$txptrace_qtime += $time;
386-
@$txptrace_qcount++;
384+
$trace->stop();
387385

388386
if ($result === false) {
389387
trigger_error(mysqli_error($DB->link), E_USER_ERROR);
390388
}
391389

392-
trace_add('[SQL ('.number_format($time, 6, '.', '')."): $q]");
393-
394390
if (!$result) {
395391
return false;
396392
}

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL