TeraWurfl
[ class tree: TeraWurfl ] [ index: TeraWurfl ] [ all elements ]

Source for file phpMyProfiler.php

Documentation is available at phpMyProfiler.php

  1. <?php
  2. /**
  3.  * Class to grab query profiling data from MySQL and log it to a file
  4.  * for later examination.  Modified by Steve Kamerman for better
  5.  * operation in evaluating Tera-WURFL performance.
  6.  * 
  7.  * Originally downloaded from http://www.php-trivandrum.org/open-php-myprofiler/
  8.  */
  9.     
  10.     private $link;
  11.     private $error;
  12.     private $log;
  13.     
  14.     function __construct(MySQLi &$link$log false){
  15.         $this->log $log;
  16.         if(!$this->logreturn;
  17.         $this->link =$link;
  18.         $this->startProfiling();
  19.     }
  20.  
  21.     function setLink(&$link){
  22.         $this->link $link;
  23.         $this->stopProfiling();
  24.         $this->startProfiling();
  25.     }
  26.  
  27.     function __destruct(){
  28.         $this->log();
  29.     }
  30.     private function startProfiling(){
  31.         $this->link->query('set profiling_history_size=100'or die($this->link->error);
  32.         $this->link->query('set profiling=1'or die($this->link->error);
  33.         $res $this->link->query("show variables like 'profiling'");
  34.         $row $res->fetch_assoc();
  35.         if($row['Value'== "OFF"){
  36.             throw new Exception("Cannot enable profiling in MySQL!");
  37.         }
  38.     }
  39.     private function stopProfiling(){
  40.         $this->link->query('set profiling=0'or die($this->link->error);
  41.     }
  42.     
  43.     private function collectData(){
  44.         $rv array();
  45.         $rs $this->link->query('show profiles'or die("Error: ".$this->link->error);
  46.            if($rs->num_rows == 0return;
  47.         while($rd $rs->fetch_assoc()){
  48.             if($rd['Query_ID'== 0continue;
  49.             if($detail $this->getDetails($rd['Query_ID']))
  50.                 $rd['detail'$detail;
  51.             $rv[$rd;
  52.         }
  53.         $rs->free_result();
  54.         return $rv;
  55.     }
  56.     
  57.     private function getDetails($qid){
  58.             $rsd $this->link->query('select min(seq) seq,state,count(*) numb_ops, '
  59.                 . 'round(sum(duration),5) sum_dur, round(avg(duration),5) avg_dur, '
  60.                 . 'round(sum(cpu_user),5) sum_cpu, round(avg(cpu_user),5) avg_cpu '
  61.                 . 'from information_schema.profiling '
  62.                 . 'where query_id = ' $qid
  63.                 . ' group by state order by seq'or die($this->link->error);
  64.             if($rsd->num_rows == 0return;
  65.             $rsv array();
  66.             while($rdd $rsd->fetch_assoc()){
  67.                 $rsv[$rdd;
  68.             }
  69.             return $rsv;
  70.     }
  71.     
  72.     public function log(){
  73.         if(!$this->link or !$this->log){
  74.             return;
  75.         }
  76.         $this->stopProfiling();
  77.         $logFile $this->log $_SERVER['HTTP_HOST'.'-' date('Ymd-G''.log';
  78.         if(!file_exists($logFile)){
  79.             file_put_contents($logFile'#PhpMyProfiler' "\n");
  80.         }
  81.         $data['instance'array('timestamp' => time()'request' => $_SERVER['REQUEST_URI' ]);
  82.     //ob_start();
  83.         $data['profiles'$this->collectData();
  84.         //die(var_export($data,true));
  85.     //ob_get_clean();
  86.         if(empty($data['profiles']or count($data['profiles']== 0){
  87.             touch($this->log.'NOPROFILE');
  88.             return;
  89.         }
  90.         $logData base64_encode(gzcompress(serialize($data))) "\n";
  91.         file_put_contents($logFile$logDataFILE_APPEND LOCK_EX);
  92.         $this->log false// dont want to call a second time
  93.     }
  94. }
  95. ?>

Documentation generated on Sun, 19 Sep 2010 00:15:53 +0000 by phpDocumentor 1.4.3