"Fossies" - the Fresh Open Source Software Archive

Member "libs/PHPCrawler/PHPCrawlerBenchmark.class.php" (8 Jan 2013, 4533 Bytes) of package /linux/www/SitemapCreator.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 /**
    3  * A static benchmark-class for doing benchmarks within phpcrawl.
    4  *
    5  * Example:
    6  * <code>
    7  * PHPCrawlerBenchmark::start("benchmark1");
    8  * sleep(2);
    9  * PHPCrawlerBenchmark::stop("benchmark1");
   10  * echo PHPCrawlerBenchmark::getElapsedTime("benchmark1");
   11  * </code>
   12  *
   13  * @package phpcrawl
   14  * @internal
   15  */
   16 class PHPCrawlerBenchmark
   17 {
   18   protected static $benchmark_results = array();
   19   protected static $benchmark_starttimes = array();
   20   protected static $benchmark_startcount = array();
   21   protected static $temporary_benchmarks = array();
   22   
   23   /**
   24    * Starts the clock for the given benchmark.
   25    *
   26    * @param string $identifie           The benchmark name/identifier.
   27    * @param bool   $temporary_benchmark If set to TRUE, the benchmark will not be returned by methods
   28    *                                    like getAllBenchmarks() and printAllBenchmarks()
   29    */
   30   public static function start($identifier, $temporary_benchmark = false)
   31   {
   32     self::$benchmark_starttimes[$identifier] = self::getmicrotime();
   33     
   34     if (isset(self::$benchmark_startcount[$identifier]))
   35       self::$benchmark_startcount[$identifier] = self::$benchmark_startcount[$identifier] + 1;
   36     else self::$benchmark_startcount[$identifier] = 1;
   37     
   38     if ($temporary_benchmark == true)
   39     {
   40       self::$temporary_benchmarks[$identifier] = true;
   41     }
   42   }
   43   
   44   public static function getCallCount($identifier)
   45   {
   46     return self::$benchmark_startcount[$identifier];
   47   }
   48   
   49   /**
   50    * Stops the benchmark-clock for the given benchmark.
   51    *
   52    * @param string  The benchmark name/identifier.
   53    */
   54   public static function stop($identifier)
   55   {
   56     if (isset(self::$benchmark_starttimes[$identifier]))
   57     {
   58       $elapsed_time = self::getmicrotime() - self::$benchmark_starttimes[$identifier];
   59 
   60       if (isset(self::$benchmark_results[$identifier])) self::$benchmark_results[$identifier] += $elapsed_time;
   61       else self::$benchmark_results[$identifier] = $elapsed_time;
   62     }
   63   }
   64   
   65   /**
   66    * Gets the elapsed time for the given benchmark.
   67    *
   68    * @param string  The benchmark name/identifier.
   69    * @return float The elapsed time in seconds and miliseconds (e.g. 1.74343)
   70    */
   71   public static function getElapsedTime($identifier)
   72   {
   73     if (isset(self::$benchmark_results[$identifier]))
   74     {
   75       return self::$benchmark_results[$identifier];
   76     }
   77   }
   78   
   79   /**
   80    * Resets the clock for the given benchmark.
   81    */
   82   public static function reset($identifier)
   83   {
   84     if (isset(self::$benchmark_results[$identifier]))
   85     {
   86       self::$benchmark_results[$identifier] = 0;
   87     }
   88   }
   89   
   90   /**
   91    * Resets all clocks for all benchmarks.
   92    *
   93    * @param array $retain_benachmarks Optional. Numeric array containing benchmark-identifiers that should NOT get resetted.
   94    */
   95   public static function resetAll($retain_benchmarks = array())
   96   {
   97     // If no benchmarks should be retained
   98     if (count($retain_benchmarks) == 0)
   99     {
  100       self::$benchmark_results = array();
  101       return;
  102     }
  103     
  104     // Else reset all benchmarks BUT the retain_benachmarks
  105     @reset(self::$benchmark_results);
  106     while (list($identifier) = @each(self::$benchmark_results))
  107     {
  108       if (!in_array($identifier, $retain_benchmarks))
  109       {
  110         self::$benchmark_results[$identifier] = 0;
  111       }
  112     }
  113   }
  114   
  115   public static function printAllBenchmarks($linebreak = "<br />")
  116   {
  117     @reset(self::$benchmark_results);
  118     while (list($identifier, $elapsed_time) = @each(self::$benchmark_results))
  119     {
  120       if (!isset(self::$temporary_benchmarks[$identifier])) echo $identifier.": ".$elapsed_time." sec" . $linebreak;
  121     }
  122   }
  123   
  124   /**
  125    * Returns all registered benchmark-results.
  126    *
  127    * @return array associative Array. The keys are the benchmark-identifiers, the values the benchmark-times.
  128    */
  129   public static function getAllBenchmarks()
  130   {
  131     $benchmarks = array();
  132     
  133     @reset(self::$benchmark_results);
  134     while (list($identifier, $elapsed_time) = @each(self::$benchmark_results))
  135     {
  136       if (!isset(self::$temporary_benchmarks[$identifier])) $benchmarks[$identifier] = $elapsed_time;
  137     }
  138     
  139     return $benchmarks;
  140   }
  141   
  142   /**
  143    * Returns the current time in seconds and milliseconds.
  144    *
  145    * @return float
  146    */
  147   public static function getmicrotime()
  148   { 
  149     list($usec, $sec) = explode(" ",microtime());
  150     return ((float)$usec + (float)$sec); 
  151   }
  152 }