forked from BoldGrid/w3-total-cache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsageStatistics_Source_DbQueriesLog.php
153 lines (119 loc) · 3.42 KB
/
UsageStatistics_Source_DbQueriesLog.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php
namespace W3TC;
/**
* database queries debug log reader - provides data from this logfile
*/
class UsageStatistics_Source_DbQueriesLog {
// running values
private $timestamp_start;
private $sort_column;
private $by_query = array();
/* if need to read more access log chunks */
private $more_log_needed = true;
function __construct( $timestamp_start, $sort_column ) {
$this->timestamp_start = $timestamp_start;
$this->sort_column = $sort_column;
}
/**
* Lists entries from log
**/
public function list_entries() {
$log_filename = Util_Debug::log_filename( 'dbcache-queries' );
$h = @fopen( $log_filename, 'rb' );
if ( !$h ) {
throw new \Exception( 'Failed to open log file' . $log_filename );
}
fseek( $h, 0, SEEK_END );
$pos = ftell( $h );
$unparsed_head = '';
while ( $pos >= 0 && $this->more_log_needed ) {
$pos -= 8192;
if ( $pos <= 0 ) {
$pos = 0;
}
fseek( $h, $pos );
$s = fread( $h, 8192 );
$unparsed_head = $this->parse_string( $s . $unparsed_head, $pos > 0 );
if ( $pos <= 0 ) {
$this->more_log_needed = false;
}
}
$output = array();
foreach ( $this->by_query as $query => $data ) {
$output[] = array(
'query' => $query,
'count_total' => $data['count_total'],
'count_hit' => $data['count_hit'],
'avg_size' => (int)( $data['sum_size'] / $data['count_total'] ),
'avg_time_ms' => (int)( $data['sum_time_ms'] / $data['count_total'] ),
'sum_time_ms' => (int)$data['sum_time_ms'],
'reasons' => $data['reasons']
);
}
usort( $output, function($a, $b) {
return (int)($b[$this->sort_column]) - (int)($a[$this->sort_column]);
});
$output = array_slice( $output, 0, 200 );
return $output;
}
private function parse_string( $s, $skip_first_line ) {
$s_length = strlen( $s );
$unparsed_head = '';
$n = 0;
if ( $skip_first_line ) {
for ( ; $n < $s_length; $n++ ) {
$c = substr( $s, $n, 1 );
if ( $c == "\r" || $c == "\n" ) {
$unparsed_head = substr( $s, 0, $n + 1 );
break;
}
}
}
$line_start = $n;
for ( ; $n < $s_length; $n++ ) {
$c = substr( $s, $n, 1 );
if ( $c == "\r" || $c == "\n" ) {
if ( $n > $line_start ) {
$this->push_line( substr( $s, $line_start, $n - $line_start ) );
}
$line_start = $n + 1;
}
}
return $unparsed_head;
}
private function push_line( $line ) {
$matches = str_getcsv( $line, "\t" );
if ( !$matches ) {
return;
}
$date_string = $matches[0];
$query = $matches[2];
$time_taken_ms = isset( $matches[3] ) ? (float)$matches[3] / 1000 : 0;
$reason = isset( $matches[4] ) ? $matches[4] : '';
$hit = isset( $matches[5] ) ? $matches[5] : false;
$size = isset( $matches[6] ) ? $matches[6] : 0;
$time = strtotime($date_string);
// dont read more if we touched entries before timeperiod of collection
if ( $time < $this->timestamp_start ) {
$this->more_log_needed = false;
}
if ( !isset( $this->by_query[$query] ) ) {
$this->by_query[$query] = array(
'count_total' => 0,
'count_hit' => 0,
'sum_size' => 0,
'sum_time_ms' => 0,
'reasons' => array()
);
}
$this->by_query[$query]['count_total']++;
if ($hit) {
$this->by_query[$query]['count_hit']++;
}
$this->by_query[$query]['sum_size'] += $size;
$this->by_query[$query]['sum_time_ms'] += $time_taken_ms;
if ( !in_array( $reason, $this->by_query[$query]['reasons']) ) {
$this->by_query[$query]['reasons'][] = $reason;
}
}
}