Skip to content
This repository was archived by the owner on Jan 25, 2022. It is now read-only.

Commit 1aec86c

Browse files
committed
Remove problematic .gitignore and add in missing classes.
1 parent 688324e commit 1aec86c

File tree

4 files changed

+337
-4
lines changed

4 files changed

+337
-4
lines changed

.gitignore

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
/IP
66
/.vagrant
77
/.vscode
8-
/.wplib-box.git
9-
/.save.git
108
/logs
119
/scripts
12-
/www/*
13-
!/www/content/
10+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
/**
3+
* Timer that collects timing and memory usage.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Timer {
9+
10+
protected $start = null;
11+
protected $end = null;
12+
protected $trace = null;
13+
protected $laps = array();
14+
15+
public function start( array $data = null ) {
16+
$this->trace = new QM_Backtrace;
17+
$this->start = array(
18+
'time' => microtime( true ),
19+
'memory' => memory_get_usage(),
20+
'data' => $data,
21+
);
22+
return $this;
23+
}
24+
25+
public function stop( array $data = null ) {
26+
27+
$this->end = array(
28+
'time' => microtime( true ),
29+
'memory' => memory_get_usage(),
30+
'data' => $data,
31+
);
32+
33+
return $this;
34+
35+
}
36+
37+
public function lap( array $data = null, $name = null ) {
38+
39+
$lap = array(
40+
'time' => microtime( true ),
41+
'memory' => memory_get_usage(),
42+
'data' => $data,
43+
);
44+
45+
if ( ! isset( $name ) ) {
46+
/* translators: %d: Timing lap number */
47+
$i = sprintf( __( 'Lap %d', 'query-monitor' ), count( $this->laps ) + 1 );
48+
} else {
49+
$i = $name;
50+
}
51+
52+
$this->laps[ $i ] = $lap;
53+
54+
return $this;
55+
56+
}
57+
58+
public function get_laps() {
59+
60+
$laps = array();
61+
$prev = $this->start;
62+
63+
foreach ( $this->laps as $lap_id => $lap ) {
64+
65+
$lap['time_used'] = $lap['time'] - $prev['time'];
66+
$lap['memory_used'] = $lap['memory'] - $prev['memory'];
67+
68+
$laps[ $lap_id ] = $prev = $lap;
69+
70+
}
71+
72+
return $laps;
73+
74+
}
75+
76+
public function get_time() {
77+
return $this->end['time'] - $this->start['time'];
78+
}
79+
80+
public function get_memory() {
81+
return $this->end['memory'] - $this->start['memory'];
82+
}
83+
84+
public function get_trace() {
85+
return $this->trace;
86+
}
87+
88+
public function end( array $data = null ) {
89+
return $this->stop( $data );
90+
}
91+
92+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Timing and profiling collector.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Collector_Timing extends QM_Collector {
9+
10+
public $id = 'timing';
11+
private $track_timer = array();
12+
private $start = array();
13+
private $stop = array();
14+
private $laps = array();
15+
16+
public function name() {
17+
return __( 'Timing', 'query-monitor' );
18+
}
19+
20+
public function __construct() {
21+
parent::__construct();
22+
add_action( 'qm/start', array( $this, 'action_function_time_start' ), 10, 1 );
23+
add_action( 'qm/stop', array( $this, 'action_function_time_stop' ), 10, 1 );
24+
add_action( 'qm/lap', array( $this, 'action_function_time_lap' ), 10, 2 );
25+
}
26+
27+
public function action_function_time_start( $function ) {
28+
$this->track_timer[ $function ] = new QM_Timer;
29+
$this->start[ $function ] = $this->track_timer[ $function ]->start();
30+
}
31+
32+
public function action_function_time_stop( $function ) {
33+
if ( ! isset( $this->track_timer[ $function ] ) ) {
34+
$trace = new QM_Backtrace;
35+
$this->data['warning'][] = array(
36+
'function' => $function,
37+
'message' => __( 'Timer not started', 'query-monitor' ),
38+
'trace' => $trace,
39+
);
40+
return;
41+
}
42+
$this->stop[ $function ] = $this->track_timer[ $function ]->stop();
43+
$this->calculate_time( $function );
44+
}
45+
46+
public function action_function_time_lap( $function, $name = null ) {
47+
if ( ! isset( $this->track_timer[ $function ] ) ) {
48+
$trace = new QM_Backtrace;
49+
$this->data['warning'][] = array(
50+
'function' => $function,
51+
'message' => __( 'Timer not started', 'query-monitor' ),
52+
'trace' => $trace,
53+
);
54+
return;
55+
}
56+
$this->track_timer[ $function ]->lap( $name );
57+
}
58+
59+
public function calculate_time( $function ) {
60+
$trace = $this->track_timer[ $function ]->get_trace();
61+
$function_time = $this->track_timer[ $function ]->get_time();
62+
$function_memory = $this->track_timer[ $function ]->get_memory();
63+
$function_laps = $this->track_timer[ $function ]->get_laps();
64+
65+
$this->data['timing'][] = array(
66+
'function' => $function,
67+
'function_time' => $function_time,
68+
'function_memory' => $function_memory,
69+
'laps' => $function_laps,
70+
'trace' => $trace,
71+
);
72+
}
73+
74+
public function process() {
75+
foreach ( $this->start as $function => $value ) {
76+
if ( ! isset( $this->stop[ $function ] ) ) {
77+
$trace = $this->track_timer[ $function ]->get_trace();
78+
$this->data['warning'][] = array(
79+
'function' => $function,
80+
'message' => __( 'Timer not stopped', 'query-monitor' ),
81+
'trace' => $trace,
82+
);
83+
}
84+
}
85+
}
86+
87+
}
88+
89+
# Load early in case a plugin is setting the function to be checked when it initialises instead of after the `plugins_loaded` hook
90+
QM_Collectors::add( new QM_Collector_Timing );
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
<?php
2+
/**
3+
* Timing and profiling output for HTML pages.
4+
*
5+
* @package query-monitor
6+
*/
7+
8+
class QM_Output_Html_Timing extends QM_Output_Html {
9+
10+
public function __construct( QM_Collector $collector ) {
11+
parent::__construct( $collector );
12+
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 15 );
13+
}
14+
15+
public function output() {
16+
17+
$data = $this->collector->get_data();
18+
19+
if ( empty( $data['timing'] ) && empty( $data['warning'] ) ) {
20+
return;
21+
}
22+
23+
echo '<div class="qm" id="' . esc_attr( $this->collector->id() ) . '">';
24+
echo '<table>';
25+
26+
echo '<caption class="screen-reader-text">' . esc_html__( 'Function Timing', 'query-monitor' ) . '</caption>';
27+
28+
echo '<thead>';
29+
echo '<tr>';
30+
echo '<th scope="col">' . esc_html__( 'Tracked Function', 'query-monitor' ) . '</th>';
31+
echo '<th scope="col" class="qm-num">' . esc_html__( 'Time', 'query-monitor' ) . '</th>';
32+
echo '<th scope="col" class="qm-num">' . esc_html__( 'Memory', 'query-monitor' ) . '</th>';
33+
echo '<th scope="col">' . esc_html__( 'Component', 'query-monitor' ) . '</th>';
34+
echo '</tr>';
35+
echo '</thead>';
36+
37+
echo '<tbody>';
38+
if ( ! empty( $data['timing'] ) ) {
39+
foreach ( $data['timing'] as $row ) {
40+
41+
$component = $row['trace']->get_component();
42+
$trace = $row['trace']->get_filtered_trace();
43+
$file = self::output_filename( $row['function'], $trace[0]['file'], $trace[0]['line'] );
44+
45+
echo '<tr>';
46+
47+
if ( self::has_clickable_links() ) {
48+
echo '<td class="qm-ltr">';
49+
echo $file; // WPCS: XSS ok.
50+
echo '</td>';
51+
} else {
52+
echo '<td class="qm-ltr qm-has-toggle"><ol class="qm-toggler">';
53+
echo self::build_toggler(); // WPCS: XSS ok;
54+
echo '<li>';
55+
echo $file; // WPCS: XSS ok.
56+
echo '</li>';
57+
echo '</ol></td>';
58+
}
59+
60+
printf(
61+
'<td class="qm-num">%s</td>',
62+
esc_html( number_format_i18n( $row['function_time'] * 1000, 4 ) )
63+
);
64+
65+
$mem = sprintf(
66+
/* translators: %s: Approximate memory used in kilobytes */
67+
__( '~%s kB', 'query-monitor' ),
68+
number_format_i18n( $row['function_memory'] / 1024 )
69+
);
70+
printf(
71+
'<td class="qm-num">%s</td>',
72+
esc_html( $mem )
73+
);
74+
printf(
75+
'<td class="qm-nowrap">%s</td>',
76+
esc_html( $component->name )
77+
);
78+
79+
echo '</tr>';
80+
81+
}
82+
}
83+
if ( ! empty( $data['warning'] ) ) {
84+
foreach ( $data['warning'] as $row ) {
85+
$component = $row['trace']->get_component();
86+
$trace = $row['trace']->get_filtered_trace();
87+
$file = self::output_filename( $row['function'], $trace[0]['file'], $trace[0]['line'] );
88+
89+
echo '<tr class="qm-warn">';
90+
if ( self::has_clickable_links() ) {
91+
echo '<td class="qm-ltr">';
92+
echo $file; // WPCS: XSS ok.
93+
echo '</td>';
94+
} else {
95+
echo '<td class="qm-ltr qm-has-toggle"><ol class="qm-toggler">';
96+
echo self::build_toggler(); // WPCS: XSS ok;
97+
echo '<li>';
98+
echo $file; // WPCS: XSS ok.
99+
echo '</li>';
100+
echo '</ol></td>';
101+
}
102+
103+
printf(
104+
'<td colspan="2">%s</td>',
105+
esc_html( $row['message'] )
106+
);
107+
108+
printf(
109+
'<td class="qm-nowrap">%s</td>',
110+
esc_html( $component->name )
111+
);
112+
}
113+
}
114+
115+
echo '</tbody>';
116+
echo '</table>';
117+
echo '</div>';
118+
119+
}
120+
121+
public function admin_menu( array $menu ) {
122+
$data = $this->collector->get_data();
123+
124+
if ( ! empty( $data['timing'] ) || ! empty( $data['warning'] ) ) {
125+
$count = 0;
126+
if ( ! empty( $data['timing'] ) ) {
127+
$count += count( $data['timing'] );
128+
}
129+
if ( ! empty( $data['warning'] ) ) {
130+
$count += count( $data['warning'] );
131+
}
132+
/* translators: %s: Number of function timing results that are available */
133+
$label = _n( 'Timings (%s)', 'Timings (%s)', $count, 'query-monitor' );
134+
$menu[] = $this->menu( array(
135+
'title' => esc_html( sprintf(
136+
$label,
137+
number_format_i18n( $count )
138+
) ),
139+
) );
140+
}
141+
142+
return $menu;
143+
}
144+
145+
}
146+
147+
function register_qm_output_html_timing( array $output, QM_Collectors $collectors ) {
148+
if ( $collector = QM_Collectors::get( 'timing' ) ) {
149+
$output['timing'] = new QM_Output_Html_Timing( $collector );
150+
}
151+
return $output;
152+
}
153+
154+
add_filter( 'qm/outputter/html', 'register_qm_output_html_timing', 15, 2 );

0 commit comments

Comments
 (0)