<?php
/*
Plugin Name: Performance Probe
Description: <strong>IMPORTANT:</strong> read source for instructions. <em>Requires</em> a small WP core source change.
Plugin URI: http://libertini.net/libertus/wordpress-plugins/performance-probe/
Author: Paul Mitchell aka Libertus
Author URI: http://libertini.net/libertus/about-the-host/?from=performance-probe-plugin
Version: &beta;1 (2006-08-12)
*/

/*
IMPORTANT OPERATIONAL NOTE!
---------------------------

This plugin generates compressed files of the performance readings it gathers.
The files are named using the following prefix, followed by the date and time.
*/
define(
    
'PERFPROBE_FILENAME_PREFIX',
    
'perfgraph-'
);

/*
In order for this plugin to generate readings;

a) the following directory is where the result files are saved,
   so it must be writable by the web server, but think what you
   could do with PHP stream wrappers
*/
define(
    
'PERFPROBE_DATA_DIR',
    
ABSPATH '/wp-content/'
);

/*
b) function do_action() in wp-includes/functions.php must be modified
   to call a new action, namely 'do_action', as follows:

    function do_action($tag, $arg = '') {
        if( $tag != 'do_action' ) do_action( 'do_action', $tag );
*/

add_action'do_action''performance_probe' );

function 
performance_probe$tag )
{
    global 
$wpdb;
    global 
$performance_graph;

    
$performance_graph[] =
        array(
            
$tag,
            
memory_get_usage(),
            
timer_stop(),
            
$wpdb->num_queries
        
)
    ;

    
// allows installation as filter
    
return $tag;
}

register_shutdown_function'performance_probe_dump_graph' );

function 
performance_probe_dump_graph() {
    
performance_probe$_SERVER['REQUEST_URI'] );    // take one last reading

    
$filename $basename PERFPROBE_DATA_DIR PERFPROBE_FILENAME_PREFIX date('Ymd-His');
    while( 
file_exists$filename ) and ++$seq 100 )
        
$filename $basename '-' $seq;

    global 
$performance_graph;
    
file_put_contents($filenamegzcompress(serialize($performance_graph)) );
}

/* The following standalone script dumps the contents of result files as CSV

$graph = unserialize(gzuncompress(file_get_contents($_SERVER['argv'][1])));
foreach( $graph as $index => $reading ) {
  $tag=$reading[0];
  $memory=$reading[1];
  $timestamp=number_format($reading[2],3);
  $queries = $reading[3];

  $delta = $memory - $delta;
  echo "$index,$tag,$memory,$delta,$timestamp,$queries\n";
  $delta = $memory;
}

*/

?>