Development

/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/database/sfDoctrineConnectionProfiler.class.php

Revision 33570, 4.8 kB (checked in by fabien, 2 years ago)

[1.4] fixed Doctrine queries execution time in the profiler (closes #10079, patch from jakub.turek)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /**
4  * Connection profiler.
5  *
6  * @package    sfDoctrinePlugin
7  * @subpackage database
8  * @author     Kris Wallsmith <kris.wallsmith@symfony-project.com>
9  * @version    SVN: $Id$
10  */
11 class sfDoctrineConnectionProfiler extends Doctrine_Connection_Profiler
12 {
13   protected
14     $dispatcher = null,
15     $options    = array();
16
17   /**
18    * Constructor.
19    *
20    * Available options:
21    *
22    *  * logging:              Whether to notify query logging events (defaults to false)
23    *  * slow_query_threshold: How many seconds a query must take to be considered slow (defaults to 1)
24    *
25    * @param sfEventDispatcher $dispatcher
26    * @param array             $options
27    */
28   public function __construct(sfEventDispatcher $dispatcher, $options = array())
29   {
30     $this->dispatcher = $dispatcher;
31     $this->options = array_merge(array(
32       'logging'              => false,
33       'slow_query_threshold' => 1,
34     ), $options);
35   }
36
37   /**
38    * Returns an option value.
39    *
40    * @param  string $name
41    *
42    * @return mixed
43    */
44   public function getOption($name)
45   {
46     return isset($this->options[$name]) ? $this->options[$name] : null;
47   }
48
49   /**
50    * Sets an option value.
51    *
52    * @param string $name
53    * @param mixed  $value
54    */
55   public function setOption($name, $value)
56   {
57     $this->options[$name] = $value;
58   }
59
60   /**
61    * Logs time and a connection query on behalf of the connection.
62    *
63    * @param Doctrine_Event $event
64    */
65   public function preQuery(Doctrine_Event $event)
66   {
67     if ($this->options['logging'])
68     {
69       $this->dispatcher->notify(new sfEvent($event->getInvoker(), 'application.log', array(sprintf('query : %s - (%s)', $event->getQuery(), join(', ', self::fixParams($event->getParams()))))));
70     }
71
72     sfTimerManager::getTimer('Database (Doctrine)');
73
74     $args = func_get_args();
75     $this->__call(__FUNCTION__, $args);
76   }
77
78   /**
79    * Logs to the timer.
80    *
81    * @param Doctrine_Event $event
82    */
83   public function postQuery(Doctrine_Event $event)
84   {
85     sfTimerManager::getTimer('Database (Doctrine)',false)->addTime();
86
87     $args = func_get_args();
88     $this->__call(__FUNCTION__, $args);
89
90     if ($event->getElapsedSecs() > $this->options['slow_query_threshold'])
91     {
92       $event->slowQuery = true;
93     }
94   }
95
96   /**
97    * Logs a connection exec on behalf of the connection.
98    *
99    * @param Doctrine_Event $event
100    */
101   public function preExec(Doctrine_Event $event)
102   {
103     if ($this->options['logging'])
104     {
105       $this->dispatcher->notify(new sfEvent($event->getInvoker(), 'application.log', array(sprintf('exec : %s - (%s)', $event->getQuery(), join(', ', self::fixParams($event->getParams()))))));
106     }
107
108     sfTimerManager::getTimer('Database (Doctrine)');
109
110     $args = func_get_args();
111     $this->__call(__FUNCTION__, $args);
112   }
113
114   /**
115    * Logs to the timer.
116    *
117    * @param Doctrine_Event $event
118    */
119   public function postExec(Doctrine_Event $event)
120   {
121     sfTimerManager::getTimer('Database (Doctrine)',false)->addTime();
122
123     $args = func_get_args();
124     $this->__call(__FUNCTION__, $args);
125
126     if ($event->getElapsedSecs() > $this->options['slow_query_threshold'])
127     {
128       $event->slowQuery = true;
129     }
130   }
131
132   /**
133    * Logs a statement execute on behalf of the statement.
134    *
135    * @param Doctrine_Event $event
136    */
137   public function preStmtExecute(Doctrine_Event $event)
138   {
139     if ($this->options['logging'])
140     {
141       $this->dispatcher->notify(new sfEvent($event->getInvoker(), 'application.log', array(sprintf('execute : %s - (%s)', $event->getQuery(), join(', ', self::fixParams($event->getParams()))))));
142     }
143
144     sfTimerManager::getTimer('Database (Doctrine)');
145
146     $args = func_get_args();
147     $this->__call(__FUNCTION__, $args);
148   }
149
150   /**
151    * Logs to the timer.
152    *
153    * @param Doctrine_Event $event
154    */
155   public function postStmtExecute(Doctrine_Event $event)
156   {
157     sfTimerManager::getTimer('Database (Doctrine)',false)->addTime();
158
159     $args = func_get_args();
160     $this->__call(__FUNCTION__, $args);
161
162     if ($event->getElapsedSecs() > $this->options['slow_query_threshold'])
163     {
164       $event->slowQuery = true;
165     }
166   }
167
168   /**
169    * Returns events having to do with query execution.
170    *
171    * @return array
172    */
173   public function getQueryExecutionEvents()
174   {
175     $events = array();
176     foreach ($this as $event)
177     {
178       if (in_array($event->getCode(), array(Doctrine_Event::CONN_QUERY, Doctrine_Event::CONN_EXEC, Doctrine_Event::STMT_EXECUTE)))
179       {
180         $events[] = $event;
181       }
182     }
183
184     return $events;
185   }
186
187   /**
188    * Fixes query parameters for logging.
189    *
190    * @param  array $params
191    *
192    * @return array
193    */
194   static public function fixParams($params)
195   {
196     foreach ($params as $key => $param)
197     {
198       if (strlen($param) >= 255)
199       {
200         $params[$key] = '['.number_format(strlen($param) / 1024, 2).'Kb]';
201       }
202     }
203
204     return $params;
205   }
206 }
207
Note: See TracBrowser for help on using the browser.