Resource tracing class

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Resource tracing class

Post 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; }
 
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Resource tracing class

Post 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.
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Resource tracing class

Post 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?
Post Reply