[Piwik-svn] r475 - in trunk: config modules modules/Log plugins/Installation tests/modules

svnmaster at piwik.org svnmaster at piwik.org
Sun May 11 18:58:04 CEST 2008


Author: matt
Date: 2008-05-11 18:58:02 +0200 (Sun, 11 May 2008)
New Revision: 475

Modified:
   trunk/config/global.ini.php
   trunk/modules/Log.php
   trunk/modules/Log/Error.php
   trunk/modules/Log/Exception.php
   trunk/modules/Piwik.php
   trunk/plugins/Installation/Controller.php
   trunk/tests/modules/LogStats_Db.test.php
Log:
- now logging error & exceptions to the error output
- improving SQL profiling output, adding new option to enable sql profiling in configuration file

Modified: trunk/config/global.ini.php
===================================================================
--- trunk/config/global.ini.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/config/global.ini.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -62,10 +62,14 @@
 ; this is useful when making changes to the archiving code so we can 
 always_archive_data = false
 
+; if set to true, all the SQL queries will be recorded by the profiler 
+; and a profiling summary will be printed at the end of the request
+enable_sql_profiler = false
+
 [General]
 ; Time in seconds after which an archive will be computed again. 
 ; This setting is used only for today's statistics.
-time_before_archive_considered_outdated = 600
+time_before_archive_considered_outdated = 20
 
 ; When loading piwik interface, we redirect the user to 'yesterday' statistics by default
 ; Possible values: yesterday, today, or any YYYY-MM-DD
@@ -185,5 +189,6 @@
 
 ; error reporting inside Smarty
 error_reporting = E_ALL|E_NOTICE
+
 ; should be set to false in a piwik release
 debugging		= true

Modified: trunk/modules/Log/Error.php
===================================================================
--- trunk/modules/Log/Error.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/modules/Log/Error.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -34,6 +34,14 @@
 							$logToDatabaseColumnMapping );
 	}
 	
+	function addWriteToScreen()
+	{
+		parent::addWriteToScreen();
+		$writerScreen = new Zend_Log_Writer_Stream('php://stderr');
+		$writerScreen->setFormatter( $this->screenFormatter );
+		$this->addWriter($writerScreen);
+	}
+	
 	public function log($errno, $errstr, $errfile, $errline, $backtrace)
 	{
 		$event = array();

Modified: trunk/modules/Log/Exception.php
===================================================================
--- trunk/modules/Log/Exception.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/modules/Log/Exception.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -35,6 +35,14 @@
 							$logToDatabaseTableName, 
 							$logToDatabaseColumnMapping );
 	}
+
+	function addWriteToScreen()
+	{
+		parent::addWriteToScreen();
+		$writerScreen = new Zend_Log_Writer_Stream('php://stderr');
+		$writerScreen->setFormatter( $this->screenFormatter );
+		$this->addWriter($writerScreen);
+	}
 	
 	public function log($exception)
 	{

Modified: trunk/modules/Log.php
===================================================================
--- trunk/modules/Log.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/modules/Log.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -23,11 +23,11 @@
  */
 abstract class Piwik_Log extends Zend_Log
 {
-	private $logToDatabaseTableName = null;
-	private $logToDatabaseColumnMapping = null;
-	private $logToFileFilename = null;
-	private $fileFormatter = null;
-	private $screenFormatter = null;
+	protected $logToDatabaseTableName = null;
+	protected $logToDatabaseColumnMapping = null;
+	protected $logToFileFilename = null;
+	protected $fileFormatter = null;
+	protected $screenFormatter = null;
 	
 	function __construct( 	$logToFileFilename, 
 							$fileFormatter,
@@ -88,7 +88,7 @@
 
 	/**
 	 * Log an event
-	 * Overload Zend_log::log cos its too weak for our requirements
+	 * Overload Zend_log::log
 	 */
 	public function log($event)
 	{

Modified: trunk/modules/Piwik.php
===================================================================
--- trunk/modules/Piwik.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/modules/Piwik.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -264,28 +264,10 @@
 		
 		if(!$profiler->getEnabled())
 		{
-			throw new Exception("To display the profiler you should turn on profiling on your config/config.ini.php file");
+			throw new Exception("To display the profiler you should enable enable_sql_profiler on your config/config.ini.php file");
 		}
-		$totalTime    = $profiler->getTotalElapsedSecs();
-		$queryCount   = $profiler->getTotalNumQueries();
-		$longestTime  = 0;
-		$longestQuery = null;
 		
-		foreach ($profiler->getQueryProfiles() as $query) {
-		    if ($query->getElapsedSecs() > $longestTime) {
-		        $longestTime  = $query->getElapsedSecs();
-		        $longestQuery = $query->getQuery();
-		    }
-		}
-		$str = '';
-		$str .= '<br>Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "\n";
-		$str .= '<br>Average query length: ' . $totalTime / $queryCount . ' seconds' . "\n";
-		$str .= '<br>Queries per second: ' . $queryCount / $totalTime . "\n";
-		$str .= '<br>Longest query length: ' . $longestTime . "\n";
-		$str .= '<br>Longest query: <br>' . $longestQuery . "\n";
-		
-		Piwik::log($str);
-		
+		$indexByQuery = array();
 		foreach($profiler->getQueryProfiles() as $query)
 		{
 			if(isset($indexByQuery[$query->getQuery()]))
@@ -306,8 +288,32 @@
 			return $a['sumTimeSeconds'] < $b['sumTimeSeconds'];
 		}
 		uasort( $indexByQuery, 'sortTimeDesc');
-		var_dump($indexByQuery);
 		
+		Piwik::log('<hr><b>SQL Profiler</b>');
+		Piwik::log('<hr><b>Summary</b>');
+		$totalTime    = $profiler->getTotalElapsedSecs();
+		$queryCount   = $profiler->getTotalNumQueries();
+		$longestTime  = 0;
+		$longestQuery = null;
+		foreach ($profiler->getQueryProfiles() as $query) {
+		    if ($query->getElapsedSecs() > $longestTime) {
+		        $longestTime  = $query->getElapsedSecs();
+		        $longestQuery = $query->getQuery();
+		    }
+		}
+		$str = 'Executed ' . $queryCount . ' queries in ' . round($totalTime,3) . ' seconds' . "\n";
+		$str .= '(Average query length: ' . round($totalTime / $queryCount,3) . ' seconds)' . "\n";
+		$str .= '<br>Queries per second: ' . round($queryCount / $totalTime,1) . "\n";
+		$str .= '<br>Longest query length: ' . round($longestTime,3) . " seconds (<code>$longestQuery</code>) \n";
+		Piwik::log($str);
+		
+		Piwik::log('<hr><b>Breakdown by query</b>');
+		foreach($indexByQuery as $query => $queryInfo) 
+		{
+			$timeMs = round($queryInfo['sumTimeSeconds'] * 1000,1);
+			$count = $queryInfo['count'];
+			Piwik::log("Executed <b>$count</b> time". ($count==1?'':'s') ." in <b>".$timeMs."ms</b> total = <code>$query</code>");
+		}
 	}
 	
 	static public function printTimer()
@@ -805,6 +811,8 @@
 		}
 		$dbInfos['password'] = htmlspecialchars_decode($dbInfos['password']);
 		
+		$dbInfos['profiler'] = $config->Debug->enable_sql_profiler;
+		 
 		$db = Zend_Db::factory($config->database->adapter, $dbInfos);
 		$db->getConnection();
 		// see http://framework.zend.com/issues/browse/ZF-1398

Modified: trunk/plugins/Installation/Controller.php
===================================================================
--- trunk/plugins/Installation/Controller.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/plugins/Installation/Controller.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -121,7 +121,6 @@
 				'dbname' 		=> $form->getSubmitValue('dbname'),
 				'tables_prefix' => $form->getSubmitValue('tables_prefix'),
 				'adapter' 		=> Zend_Registry::get('config')->database->adapter,
-				'profiler' 		=> 'false',
 			);
 			
 			// we test the DB connection with these settings

Modified: trunk/tests/modules/LogStats_Db.test.php
===================================================================
--- trunk/tests/modules/LogStats_Db.test.php	2008-05-09 00:49:53 UTC (rev 474)
+++ trunk/tests/modules/LogStats_Db.test.php	2008-05-11 16:58:02 UTC (rev 475)
@@ -29,9 +29,9 @@
     /**
      * test that the profiler is disabled (mandatory on a production server)
      */
-    public function test_profilingDisabledProduction()
+    public function test_profilingDisabledInProduction()
     {
-    	$this->assertTrue(Piwik_LogStats_Db::isProfilingEnabled() === false, 'PROFILER SHOULD BE DISABLED IN PRODUCTION!! See Piwik_LogStats_Db::$profiling');
+    	$this->assertTrue(Piwik_LogStats_Db::isProfilingEnabled() === false, 'SQL profiler should be disabled in production! See Piwik_LogStats_Db::$profiling');
     }
 }
 



More information about the Piwik-svn mailing list