PHP Classes

File: max_LoadChecker

Recommend this page to a friend!
  Classes of Maxim Maletsky   max_LoadChecker   max_LoadChecker   Download  
File: max_LoadChecker
Role: ???
Content type: text/plain
Description: The Class
Class: max_LoadChecker
Calculating the time a functions takes to run
Author: By
Last change:
Date: 24 years ago
Size: 11,422 bytes
 

Contents

Class file image Download
<? /* * max-> LoadChecker($) : v1.0 * The class to check the execution time of your file. * * COPYRIGHT * Copyright (C) 2000, * Maxim Maletsky, [maxim.maletsky@lincmedia.co.jp], * LINC Media Inc., J-Door.com, J@pan Inc. * * LICENCE * This software is FREEWARE. Do with it what you want to, as long as * the copyright notice remains intact. * * DISCLAIMER * Do not blaim me for anything that my software could do wrong. * It's a FREE software - respect it! * * If you redistribute this code please mention in your script my info (Name & Email) as it's creator. * If you are using this code please send me your feedback and your improvements to it. */ class max_LoadChecker { /* Define Variables: **************************************************************************************/ var $msDigits ="5"; // Set The default of decimals to show. var $newWin ="/inc/LoadChecker/LoadChecker.html"; // Specify the file where the table will be created. It can be a non-existing file BUT MUST BE within a writable directory. /* DO NOT CHANGE ANYTHING BELOW! \* Unless You Know What You Are Doing! \* (Please be an interactive programmer - mail me your modifications) **************************************************************************************/ var $Interval = array(); var $Name = array(); var $Line = array(); var $s; var $DigiZeros; function GetTime() { $Clock = explode(" ", microtime()); $Clock = $Clock[1] + $Clock[0]; return $Clock; } function Set( $GivenName="", $GivenLine="0") { $this->s = (sizeof($this->Interval)+1); $this->Interval[$this->s] = ($this->GetTime()); if (!$GivenName || $GivenName=="Default") $this->Name[$this->s] = "Interval(".$this->s.")"; else $this->Name[$this->s] = $GivenName; if ($GivenLine>1) $this->Line[$this->s] = $GivenLine; else $this->Line[$this->s] = "Unknown Line(".$this->s.")"; } function TotalTime() { $Digit =round((($this->Interval[$this->s]-$this->Interval[1]) + 0.00000001), (3+$this->msDigits))*1000; $DigiCount = explode(".", $Digit); $TotalTime=substr($Digit.$this->DigiZeros, 0, (strlen($DigiCount[0])+(1+$this->msDigits))); return $TotalTime; } function Intervals($msDigits=0) { if($msDigits!=0) $this->msDigits=$msDigits; for($Z=1; $Z<=$this->msDigits; $Z++){$this->DigiZeros.=0;} for($i=1; $i<$this->s; $i++) { $Digit =round((($this->Interval[$i+1]-$this->Interval[$i]) + 0.00000001), (3+$this->msDigits))*1000; $DigiCount = explode(".", $Digit); $OutLoop .= '<tr><td valign="middle" nowrap width="200" height="20"><font face="Verdana" size="1"><b>&nbsp;'. $this->Name[$i+1] .'</b></font></td><td valign="middle" nowrap width="300"><font face="Verdana" size="1" color="#666666">Between line <font color="#000000"><i>'. $this->Line[$i] .'</i><font color="#666666"> and <font color="#000000"><i>'. $this->Line[$i+1] .'</i></font></font></td><td width="100" valign="middle" align="center" nowrap><font face="Verdana" size="1" color="#FF0000">'. substr($Digit.$this->DigiZeros, 0, (strlen($DigiCount[0])+(1+$this->msDigits))) .'</font></td></tr><tr><td valign="middle" colspan="3" nowrap width="600" height="9"><hr size="1" color="#3399FF"></td></tr> '; } $OutStart ='<center><table border="0" bgcolor="#3399FF" cellpadding="0" cellspacing="1"><tr><td width="100%"><table border="0" cellspacing="3" cellpadding="0" bgcolor="#FFFFFF" width="100%"><tr><td valign="middle" align="center"><font face="Verdana" size="1"><b>Interval Name<br></b>( <i>Lines</i> )</font></td><td valign="middle" align="center" nowrap width="100"><font face="Verdana" size="1"><b>Process Time<br></b>( Milliseconds )</font></td></tr></table></td></tr><tr><td width="100%"><table border="0" width="600" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF"><tr><td valign="middle" colspan="3" nowrap width="600"><hr size="1" color="#3399FF"></td></tr>'; $OutEnd ='</table></td></tr></table> <table width="600" border="0" bgcolor="#FFFFFF"><tr><td align="right" valign="top"> <font face="Verdana" size="1">Total Execution Time : <b>'.$this->TotalTime().'</b></font> </td></tr></table></center> '; return $OutStart.$OutLoop.$OutEnd; } function newWinWrite($msDigits) { $myFile = fopen(getenv("DOCUMENT_ROOT").$this->newWin,"w"); fputs($myFile,'<html><body>'.$this->Intervals($msDigits).'</body></html>'); fclose($myFile); echo ' <script language="JavaScript"> <!-- var gt = unescape(\'%3e\'); var popup = null; var over = "Launch Pop-up Navigator"; popup = window.open(\'\', \'popupnav\', \'width=620,height='.(65+(36*$this->s)).',resizable=1,scrollbars=auto\'); if (popup != null) { if (popup.opener == null) { popup.opener = self; } popup.location.href = \''.$this->newWin.'\'; } // --> </script>'; exit; } function MaxLoad ($msDigits=0, $Win="") { echo '<!-- by Maxim Maletsky -->'; if ($Win=="_new" || $Win=="new" || $Win=="New") $this->newWinWrite($msDigits); elseif (!$Win) exit; else { echo '<p>&nbsp;'.$this->Intervals($msDigits); exit; } } }/* ===== DESCRIPTION: ===== max-> LoadChecker($) is a class created to help PHP developers to write their software. max-> LoadChecker($) is detecting the exact amount of microseconds when it is called from the main script. Calculating then the difference between two values it will print out the exact time in millisecond taken to execute the selected part of the code. It will give you a very good idea on what part of your script is heavier and what is to be improved. You can use it in different ways: to check and/or compare different functions, to check the speed of your server, your entire software, or a current page. Because it's a class you can make many different calls to it to examine different parts of your file. ===== HOW TO USE: ===== Create a new directory in the root called "max"; Chmod it as a writable directory. Upload this file into the directory you just created. ------ It is not necessary to do if you don't want to, read later below: ------ *** include() this class on the top of the file you want to check for speed and define the class: include "$DOCUMENT_ROOT/max/LoadChecker.inc"; $Clock = new max_LoadChecker; "$Clock" can be anything you want (This two lines should be above any reference to the class ) *** To start an interval use: $Clock->Set( 'Title' , __LINE__ ); where "Title" is the title of interval between this call and the previous one (Change "Title" into whatever you want). Means that it is useless for the first call, but the second "Title" will be outputted to you later for your reference. __LINE__ will print the actual lines of an interval. DO NOT remove the "Title" string - leave it empty if you want to, but don't leave the __LINE__ there alone. you might corrupt the whole class by doing that. You can repeat this line as many times as you want. If it is inside a statement, which is being skipped, your interval will be counter starting from the one call before this one. That's why it is better to call them properly so you can understand what they're staying for. NOTE: If you Set() inside a loop, it will be repeating itself with the same 'Title' as you once specified. Instead, you can do this: add to the 'Title' some of the variables that are unique for each of the loops, so on the output their value will help you to understand which loop was what. i.e: $Clock->Set( 'mySQL Database Connection ( Item '.$id.' )' , __LINE__ ); Idea: because it will count you not only LoopStart_1 - LoopEnd_1, but also LoopEnd_1 - LoopStart_2 difference, it's a good idea to name LoopStart as '_' (underline) or simply a space (not NULL) and assign a unique $id or $name to LoopEnd's only. (making it easier to read) The underslashed or empty untitled fields will tell you how long it took for that loop to check for availability of more items and jump to the top of the loop again. if you do not have any unique id then use your $i or create one like $ClockCount =0; $ClockCount++; and then add $ClockCount to the call. In future I will make this something easier to work with loops, but for now, please use this. *** Now about those __LINEs__: do not remove them because often they are the only thing that actually explains you what is going wrong and where to fix it (your load time ). If it outputs to you as "Unknown Line(x)" it means that the line number is lower or equal to 1 and this means that pick the right number was impossible. Just like in those strange errors saying error..line1 .. ? .. ? . . It usually happens when you escape from PHP to HTML by ? > < ? ************* OK, NOW... ************* Let's finally Get the output! The OutPut is being printed out by this function: $Clock->MaxLoad("3", "new"); the first number are decibels or better say the amount of digits between the dot and the end, modify it as much as you want, if you need milliseconds only set it as 1, if you need something like 0.123456 set it as 6 etc... the default is 3. "new" means that the result page will open in a new window, anything else then 'new' will give the result on the same page you're checking. If you don't want to have ANY output, you can simply remove the last string or make it ''. Then no output will be given to you. Place $Clock->MaxLoad("3", "new"); AFTER THE LAST FUNCTION OF YOUR SCRIPT. right a line before " ? >" and you don't have to worry about your HTML anymore. If you use "new" you then will need to have a writable directory called max in your root (by default) or anywhere else, if you already have one then just specify it in the class: var $newWin ="/max/LoadChecker.html"; do not worry about the file itself, it will be created if not found, just check the name not to overwrite anything if directory existed before. I recommend you to use "new" mode - it is easier, and less risk to corrupt your pages, "0" mode will send the output inside your script and "" or empty mode will do nothing. That's it! P.S: Modify this file as much as you want, send me your comments if you like it or changing it. Also delete all this commented staff if you don't like to read my English ;-)) Cheers! Maxim Maletsky Just to note, the delay of this script is not enormous, if you even notice it, then your script must be pretty fast. max_LoadChecker will delay your script only mostly entirely on output ( MaxLoad() )and here it doesn't matter, because it will be called from the last line and all the records were already registered. Set() actually takes something like 0.02 on my Linux with 64MB RAM, so on your machine I believe each 100 calls would ad a millisecond to your script load time ... */?>