Resource tracing class
Posted: Thu May 14, 2009 10:09 am
I made a resource tracing class that checks for the time and memory usage. It's not really complex and lacks several features for later, but i think it has a solid start. Please review it:
Usage
Code of the class
Example styling
Usage
Code: Select all
include('resourcetracer.php');
$tracer = new system_diagnostics_resourcetracer();
$tracer->output(); //Outputs the initial trace (initial memory and time)
//Generate a trace
$tracer->trace('Trace name');
$tracer->output();
Code: Select all
//Constants for this class library
define('TRACK_TIME', 1);
define('TRACK_MEM', 2);
//This class represents a column definition
class system_diagnostics_resourcetracer {
//Properties of the web control
protected $prvtraceflags = 0;
protected $prvlasttracename = '';
protected $prvinitialmemtrace = 0;
protected $prvinitialtimetrace = 0;
protected $prvprevmemtrace = 0;
protected $prvprevtimetrace = 0;
protected $prvlastmemtrace = 0;
protected $prvlasttimetrace = 0;
//Constructor/destructor
public function __construct($tracetime = true, $tracemem = true){
//Save the properties
$this->prvtraceflags |= ($this->readBool($tracetime, false) == false ? 0 : TRACK_TIME);
$this->prvtraceflags |= ($this->readBool($tracemem, false) == false ? 0 : TRACK_MEM);
$this->prvinitialmemtrace = memory_get_usage();
$this->prvinitialtimetrace = time()+microtime();
$this->prvprevmemtrace = $this->prvinitialmemtrace;
$this->prvprevtimetrace = $this->prvinitialtimetrace;
$this->prvlasttracename = 'Initial trace';
$this->prvlastmemtrace = $this->prvprevmemtrace;
$this->prvlasttimetrace = $this->prvprevtimetrace;
}
public function __destruct(){
unset($this->prvtraceflags, $this->prvlasttracename, $this->prvlastmemtrace, $this->prvlasttimetrace);
}
//The text get/set
public function gettracetime(){ return (($this->prvtraceflags & TRACE_TIME) == TRACE_TIME); }
public function gettracemem(){ return (($this->prvtraceflags & TRACK_MEM) == TRACK_MEM); }
public function getlasttracename(){ return $this->prvlasttracename; }
public function getinitialmemtrace(){ return $this->prvinitialmemtrace; }
public function getinitialtimetrace(){ return $this->prvinitialtimetrace; }
public function getprevmemtrace(){ return $this->prvprevmemtrace; }
public function getprevtimetrace(){ return $this->prvprevtimetrace; }
public function getlastmemtrace(){ return $this->prvlastmemtrace; }
public function getlasttimetrace(){ return $this->prvlasttimetrace; }
//Tracing function updates the trace counters
public function trace($text = ''){
//Save the previous info
$this->prvprevmemtrace = $this->prvlastmemtrace;
$this->prvprevtimetrace = $this->prvlasttimetrace;
//Trace the new info
$this->prvlasttracename = $text;
$this->prvlastmemtrace = memory_get_usage();
$this->prvlasttimetrace = time()+microtime();
}
//Outputs the last trace info in beautiful divs
public function output(){
echo '<div class="diagnostic_trace">';
echo '<div class="title">'.$this->lasttracename.'</div>';
echo '<div class="memory current">Current memory: '.number_format($this->prvlastmemtrace/1024, 1, '.', ' ').' kb</div>';
echo '<div class="memory diff">Initial memory difference: '.number_format(($this->prvlastmemtrace/1024)-($this->prvinitialmemtrace/1024), 1, '.', ' ').' kb</div>';
echo '<div class="memory change">Memory change since last trace: '.number_format(($this->prvlastmemtrace/1024)-($this->prvprevmemtrace/1024), 1, '.', ' ').' kb</div>';
echo '<div class="time diff">Total run time: '.number_format(($this->prvlasttimetrace)-($this->prvinitialtimetrace), 5, '.', ' ').' secs</div>';
echo '<div class="time change">Time change since last trace: '.number_format(($this->prvlasttimetrace)-($this->prvprevtimetrace), 5, '.', ' ').' secs</div>';
echo '</div>';
}
}
Code: Select all
.diagnostic_trace {
margin: 10px;
padding: 5px;
font-size: 11px;
border: 1px dotted #cccccc;
}
.diagnostic_trace .title { background-color: #fafafa; font-weight: bold; padding: 2px; }
.diagnostic_trace .memory { background-color: #F5E7E7; padding: 2px; }
.diagnostic_trace .time { background-color: #ECFCEB; padding: 2px; }