Skip to content

Instantly share code, notes, and snippets.

@westonruter
Created November 11, 2009 21:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/232314 to your computer and use it in GitHub Desktop.
Save westonruter/232314 to your computer and use it in GitHub Desktop.
Track runtime performance of WordPress by adding these snippets to header.php and footer.php
<!-- Add to beginning of <head> -->
<?php if(defined('WP_DEBUG') && WP_DEBUG): ?>
<script>var debugClientStartTime = (new Date()).valueOf();</script>
<?php endif; ?>
<!-- Add to end of <body> -->
<?php if(defined('WP_DEBUG') && WP_DEBUG):
ob_start(); ?>
<div id="debugPerformance">
<style scoped="">
#debugPerformance {
position:fixed;
left:0;
bottom:0;
padding:0.5em;
border:solid 1px gray;
border-left:0;
border-bottom:0;
background:white;
font-size:8pt;
}
#debugPerformance th {
padding-right:5px;
text-align:right;
}
</style>
<table>
<tr style="color:blue">
<th>DB query count</th>
<td><?php echo get_num_queries() ?></td>
</tr>
<tr>
<th>Server time</th>
<td id="debugServerExecuteTime"><?php timer_stop(1) ?></td>
</tr>
<tr>
<th>DOM load</th>
<td id="debugDomLoadTime">?</td>
</tr>
<tr>
<th>Δ onload</th>
<td id="debugOnLoadDeltaTime">?</td>
</tr>
<tr style="background-color:yellow">
<th align="right">Total</th>
<td id="debugTotalLoadTime">?</td>
</tr>
</table>
<script>
/*<![CDATA[[*/
var debugDomLoadTime = (new Date()).valueOf();
var debugServerEndTime = <?php echo time()*1000 ?>;
(function(){
function format(num){
return Math.round(num * 1000)/1000;
}
document.getElementById('debugDomLoadTime').innerHTML = format((debugDomLoadTime - debugClientStartTime)/1000);
var onloadhandler = function(){
var now = (new Date()).valueOf();
document.getElementById('debugOnLoadDeltaTime').innerHTML = format((now - debugDomLoadTime)/1000);
document.getElementById('debugTotalLoadTime').innerHTML = format(
parseFloat(document.getElementById('debugServerExecuteTime').innerHTML) +
(now - debugClientStartTime)/1000
);
};
if(window.addEventListener)
window.addEventListener('load', onloadhandler, false);
else if(window.attachEvent)
window.attachEvent('onload', onloadhandler);
else throw Error("No onload event");
function MySQLQuery(query, timer, caller){
this.query = query;
this.timer = timer;
this.caller = caller;
};
MySQLQuery.prototype.toString = function(){
return "MySQL " + Math.round(this.timer*1000) + "ms: " + this.query;
};
if(window.console){
<?php
if(defined('SAVEQUERIES') && SAVEQUERIES){
global $wpdb;
foreach($wpdb->queries as $query){
if($query[1] > 0.001)
print "console.warn(new MySQLQuery(" . trim(json_encode($query), '[]') . "));\n";
else
print "console.info(new MySQLQuery(" . trim(json_encode($query), '[]') . "));\n";
}
}
?>
}
})();
/*]]>*/
</script>
</div>
<?php
//Put the code all on one line so it doesn't visually get in the way of the code we care about
$code = preg_replace('/\s+/', ' ', ob_get_contents());
ob_end_clean();
echo $code;
endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment