Page 1 of 1

Resource tracing class

Posted: Thu May 14, 2009 10:09 am
by crazycoders
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: 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 of the class

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>';
        }
    }
 
Example styling

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; }
 

Re: Resource tracing class

Posted: Sat May 16, 2009 11:53 pm
by John Cartwright
One thing that jumps out at the screen is that your class is directly outputting HTML. This severly limits how the class can be used, with no flexibility in mind (browser vs console, logging, etc). Rather than outputting HTML, simply have it return an array of information that can be displayed as per the application.

Re: Resource tracing class

Posted: Sun May 17, 2009 12:14 pm
by crazycoders
Indeed, my goal was in fact to creating a simple layer that actually doesn't take too much memory and can be used to quickly diagnose where and what was taking a lot of. If i start logging different messages and times, it will take up some memory (although not much except in large loop situations) and thus alter the result.

I think i could simply extend it as a resourceTracerWBuffer class when you want to use it that way... What do you think?