Skip to content

Commit

Permalink
significant enhancements to benchmarks
Browse files Browse the repository at this point in the history
- support for adding early benchmark data
- show difference between previous and current row (and warn)
  • Loading branch information
crstauf committed Aug 14, 2017
1 parent 8bed3ef commit 27d8de3
Show file tree
Hide file tree
Showing 7 changed files with 205 additions and 48 deletions.
50 changes: 41 additions & 9 deletions collectors/benchmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,25 @@ public function __construct() {

}

public function add_data($label = false) {
public function add_data($label = false,$file_line = false,$timestamp = null) {
global $wpdb;

if ( is_array( $file_line ) )
$file_line = str_replace(
ABSPATH,
'./',
$file_line[0] . ':' . $file_line[1]
);

if ( empty( $timestamp ) )
$timestamp = time();

$now = array();

$now['label'] = $label;
$now['file_line'] = $file_line;
$now['i'] = count( $this->data['benchmarks'] );
$now['timestamp'] = time();
$now['timestamp'] = $timestamp;

$now['time'] = self::timer_stop_float();

Expand Down Expand Up @@ -72,21 +83,42 @@ public function add_data($label = false) {

}

function add_early_data( $now ) {
$this->data['benchmarks'][] = $now;
}

}

QM_Collectors::add( new QMX_Collector_Benchmarks );

function QMX_Benchmark($label = false) {
function QMX_Benchmark($label = false,$file_line = false,$timestamp = null) {
if ( $collector = QM_Collectors::get( 'qmx-benchmarks' ) ) {

if ( empty( $timestamp ) )
$timestamp = time();

$data = $collector->get_data();
if ( function_exists( 'wp_get_current_user' ) ) {
if ( current_user_can( 'administrator' ) )
echo '<!-- QMX Benchmark ' . ( count( $data['benchmarks'] ) + 1 ) . ': ' . ( false === $label ? time() : esc_html( $label ) ) . ' -->';
} else
echo '<!-- QMX Benchmark ' . ( count( $data['benchmarks'] ) + 1 ) . ': ' . ( false === $label ? time() : esc_html( $label ) ) . ' -->';

$collector->add_data($label);
if (
did_action( 'send_headers' )
|| did_action( 'admin_init' )
) {

if ( function_exists( 'wp_get_current_user' ) ) {
if ( current_user_can( 'administrator' ) )
echo '<!-- QMX Benchmark ' . ( count( $data['benchmarks'] ) + 1 ) . ': ' . ( false === $label ? $timestamp : esc_html( $label ) ) . ( !empty( $file_line ) ? ', ' . str_replace( ABSPATH, './', $file_line[0] ) . ':' . $file_line[1] : '' ) . ' -->';
} else
echo '<!-- QMX Benchmark ' . ( count( $data['benchmarks'] ) + 1 ) . ': ' . ( false === $label ? $timestamp : esc_html( $label ) ) . ( !empty( $file_line ) ? ', ' . str_replace( ABSPATH, './', $file_line[0] ) . ':' . $file_line[1] : '' ) . ' -->';

}

$collector->add_data($label,$file_line,$timestamp);
}
}

function QMX_Early_Benchmark( $now ) {
if ( $collector = QM_Collectors::get( 'qmx-benchmarks' ) )
$collector->add_early_data( $now );
}

?>
155 changes: 121 additions & 34 deletions output/benchmarks.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );

add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 20 );
add_filter( 'qm/output/title', array( $this, 'admin_title' ), 102 );

$this->db_query_time = 0;
$this->db_query_types = array();
Expand All @@ -31,7 +32,17 @@ public function output() {
echo '<table cellspacing="0" class="qm-sortable">';
echo '<thead>';
echo '<tr>';
echo '<th colspan="' . ( $show_db_cols ? 7 : 5 ) . '">Benchmarks</th>';
echo '<th colspan="' . ( $show_db_cols ? 7 : 5 ) . '">' .
'Benchmarks' .
'<span class="qmx-switches">' .
'<label class="qmx-switch">' .
'<input type="checkbox" ' .
'value="more-info"' .
checked( false, true, false ) .
' /><span class="slider"></span><span>Expand</span>' .
'</label>' .
'</span>' .
'</th>';
echo '</tr>';
echo '<tr>';
echo '<th class="qm-num qm-sorted-asc">&nbsp;' . $this->build_sorter() . '</th>';
Expand All @@ -43,6 +54,14 @@ public function output() {
echo '</thead>';
echo '<tbody>';

$previous = array(
'page_time' => 0,
'memory' => 0,
'db_time' => 0,
'db_types' => array(),
'files' => 0,
);

foreach ( $data['benchmarks'] as $row ) {

if ( array_key_exists( 'db_query_types', $row ) )
Expand All @@ -61,58 +80,110 @@ public function output() {
echo '<tr>';
echo '<td class="qm-num">' . esc_html( ( intval( $row['i'] ) + 1 ) ) . '</td>';

echo '<td data-qm-sort-weight="' . esc_attr( $row['label'] ) . '">' . ( $row['label'] ? esc_html( $row['label'] ) : '<span style="color: #999;">' . $row['timestamp'] . '</span>' ) . '</td>';

echo '<td>';
echo esc_html( number_format_i18n( $row['time'], 4 ) );
echo '<br><span class="qm-info">';
echo esc_html( sprintf(
__( '%1$s%% of %2$ss total', 'query-monitor' ),
number_format_i18n( ( 100 / ( array_key_exists( 'time_taken', $overview_data ) ? $overview_data['time_taken'] : $overview_data['time'] ) ) * $row['time'], 1 ),
number_format_i18n( ( array_key_exists( 'time_taken', $overview_data ) ? $overview_data['time_taken'] : $overview_data['time'] ), 4 )
) );
echo '</span>';
echo '</td>';
echo '<td data-qm-sort-weight="' . esc_attr( strtolower( $row['label'] ) ) . '">' .
esc_html(
!empty( $row['label'] )
? $row['label']
: $row['timestamp']
) .
(
!empty( $row['file_line'] )
&& $row['label'] !== $row['file_line']
? '<span class="qm-info"><br />&nbsp;' . $row['file_line'] . '</span>'
: ''
) .
'</td>';

$class = '';
if ( 0 !== $row['i'] )
if ( number_format_i18n( $previous['page_time'], 4 ) === number_format_i18n( $row['time'], 4 ) )
$class = 'qmx-benchmark-page_time-same';

echo '<td class="' . $class . '">' .
esc_html( number_format_i18n( $row['time'], 4 ) ) . 's';

if ( 0 !== $row['i'] )
echo ' <small class="qm-' . ( ( $row['time'] - $previous['page_time'] ) > .01 ? 'warn' : 'info' ) . '" style="font-size: 0.8em !important;">+' . number_format_i18n( $row['time'] - $previous['page_time'], 4 ) . '</small>';

echo '<br><span class="qm-info">' .
esc_html( sprintf(
__( '%1$s%% of %2$ss total', 'query-monitor' ),
number_format_i18n( ( 100 / ( array_key_exists( 'time_taken', $overview_data ) ? $overview_data['time_taken'] : $overview_data['time'] ) ) * $row['time'], 1 ),
number_format_i18n( ( array_key_exists( 'time_taken', $overview_data ) ? $overview_data['time_taken'] : $overview_data['time'] ), 4 )
) ) .
'</span>' .
'</td>';

if ( 0 !== $row['i'] ) {
$class = '';
if ( number_format_i18n( $previous['memory'] / KB_IN_BYTES ) === number_format_i18n( $row['memory'] / KB_IN_BYTES ) )
$class = 'qmx-benchmark-memory-same';
}

echo '<td class="' . $class . '">';

echo '<td>';
echo esc_html( sprintf(
__( '%s kB', 'query-monitor' ),
number_format_i18n( $row['memory'] / 1024 )
number_format_i18n( $row['memory'] / KB_IN_BYTES )
) );
echo '<br><span class="qm-info">';
echo esc_html( sprintf(
__( '%1$s%% of %2$ss kB total', 'query-monitor' ),
number_format_i18n( ( 100 / $overview_data['memory'] ) * $row['memory'], 1 ),
number_format_i18n( ( $overview_data['memory'] / 1024 ) )
) );
echo '</span>';

if ( 0 !== $row['i'] )
echo ' <small class="qm-' . ( $row['memory'] - $previous['memory'] > ( 1000 * KB_IN_BYTES ) ? 'warn' : 'info' ) . '" style="font-size: 0.8em !important;">+' . number_format_i18n( ( $row['memory'] - $previous['memory'] ) / KB_IN_BYTES ) . '</small>';

echo '<br><span class="qm-info">' .
esc_html( sprintf(
__( '%1$s%% of %2$ss kB total', 'query-monitor' ),
number_format_i18n( ( 100 / $overview_data['memory'] ) * $row['memory'], 1 ),
number_format_i18n( ( $overview_data['memory'] / KB_IN_BYTES ) )
) ) .
'</span>';

echo '</td>';

if ( $show_db_cols && array_key_exists( 'db_query_time', $row ) ) {

echo '<td>';
echo esc_html( number_format_i18n( ( $row['db_query_time'] + $this->db_query_time ), 4 ) );
echo '<br><span class="qm-info">';
echo esc_html( sprintf(
__( '%1$s%% of %2$ss total', 'query-monitor' ),
number_format_i18n( ( 100 / $db_queries_data['total_time'] ) * ( $row['db_query_time'] + $this->db_query_time ), 1 ),
number_format_i18n( $db_queries_data['total_time'], 4 )
) );
echo '</td>';
if ( 0 !== $row['i'] ) {
$class = '';
if ( number_format_i18n( $previous['db_time'], 4 ) === number_format_i18n( $row['db_query_time'] + $this->db_query_time, 4 ) )
$class = 'qmx-benchmark-db_time-same';
}

echo '<td class="' . $class . '">' .
number_format_i18n( $row['db_query_time'] + $this->db_query_time, 4 ) . 's';

if ( 0 !== $row['i'] )
echo ' <small class="qm-' . ( ( $row['db_query_time'] + $this->db_query_time ) - $previous['db_time'] > .001 ? 'warn' : 'info' ) . '" style="font-size: 0.8em !important;">+' . number_format_i18n( ( $row['db_query_time'] + $this->db_query_time ) - $previous['db_time'], 4 ) . '</small>';

echo '<br><span class="qm-info">' .
esc_html( sprintf(
__( '%1$s%% of %2$ss total', 'query-monitor' ),
number_format_i18n( ( 100 / $db_queries_data['total_time'] ) * ( $row['db_query_time'] + $this->db_query_time ), 1 ),
number_format_i18n( $db_queries_data['total_time'], 4 )
) ) .
'</span>' .
'</td>';

echo '<td>' . implode( '<br>', array_map( 'esc_html', $db_query_types ) ) . '</td>';
echo '<td' . ( $previous['db_types'] === $db_query_types ? ' class="qmx-benchmark-db_types-same"' : '' ) . '>' . implode( '<br>', array_map( 'esc_html', $db_query_types ) ) . '</td>';

} else
echo '<td>&nbsp;</td><td>&nbsp;</td>';

echo '<td>';
echo esc_html( $row['included_files'] ) . '<span class="qm-info">/' . $included_files . '</span>';
echo '<td' . ( $previous['files'] === $row['included_files'] ? ' class="qmx-benchmark-files-same"' : '' ) . '>';
echo '<a href="#qmx-file-' . esc_attr( $row['included_files'] ) . '">' . esc_html( $row['included_files'] ) . '</a><span class="qm-info">/' . $included_files . '</span>';
echo '</td>';

echo '</tr>';

if ( array_key_exists( 'db_query_time', $row ) )
$this->db_query_time += $row['db_query_time'];

$previous = array(
'page_time' => $row['time'],
'memory' => $row['memory'],
'db_time' => $this->db_query_time,
'db_types' => $db_query_types,
'files' => $row['included_files'],
);
}

echo '</tbody>';
Expand All @@ -121,6 +192,22 @@ public function output() {
echo '</div>';
}

public function admin_title( array $title ) {

$data = $this->collector->get_data();

$title[] = sprintf(
_x( '%s<small>B</small>', 'number of benchmarks', 'query-monitor' ),
(
array_key_exists( 'benchmarks', $data ) && is_array( $data['benchmarks'] )
? count( $data['benchmarks'] )
: 0
)
);

return $title;
}

public function admin_menu( array $menu ) {

$data = $this->collector->get_data();
Expand Down
2 changes: 1 addition & 1 deletion output/classes.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class QMX_Output_Html_Classes extends QM_Output_Html {

const cols = 6;
const cols = 4;

private static $classes = array();

Expand Down
2 changes: 1 addition & 1 deletion output/included_files.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public function output() {
echo '<tr ' .
'data-qm-includedfilespath="' . esc_attr( implode( ' ', array_keys( $details['selectors'] ) ) ) . '" ' .
'data-qm-includedfilescomponent="' . esc_attr( $details['component'] ) . '"' .
( 'errors' === $status ? ' class="qm-warn"' : '' ) .
( 'errors' === $status ? ' class="qm-warn"' : ' id="qmx-file-' . esc_attr( $details['i'] + 1 ) . '"' ) .
'>' .
'<td class="qm-num" data-qm-sort-weight="' . ( 'errors' === $status ? 0 : ( $details['i'] + 1 ) ) . '">' .
( 'errors' === $status ? ' ' : ( $details['i'] + 1 ) ) .
Expand Down
6 changes: 5 additions & 1 deletion query-monitor-extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Query Monitor Extend
Plugin URI: https://github.com/crstauf/query-monitor-extend
Description: Enhancements and extensions for the awesome Query Monitor plugin by John Blackbourn
Version: 0.0.3 | QM 2.12.0
Version: 0.0.5 | QM 2.13.4
Author: Caleb Stauffer
Author URI: http://develop.calebstauffer.com
*/
Expand All @@ -21,6 +21,8 @@
)
return;

do_action( 'load_QueryMonitorExtend' );

new query_monitor_extend;
class query_monitor_extend {

Expand Down Expand Up @@ -262,4 +264,6 @@ function is_custom_post_type() {
}
}

do_action( 'QueryMonitorExtend_loaded' );

?>
13 changes: 12 additions & 1 deletion scripts.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 27d8de3

Please sign in to comment.