]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SQLStats/SQLStatsPlugin.php
Merge remote-tracking branch 'upstream/master' into social-master
[quix0rs-gnu-social.git] / plugins / SQLStats / SQLStatsPlugin.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2011, StatusNet, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('STATUSNET')) {
21     exit(1);
22 }
23
24 /**
25  * Check DB queries for filesorts and such and log em.
26  *
27  * @package SQLStatsPlugin
28  * @maintainer Evan Prodromou <evan@status.net>
29  */
30 class SQLStatsPlugin extends Plugin
31 {
32     protected $queryCount = 0;
33     protected $queryStart = 0;
34     protected $queryTimes = array();
35     protected $queries    = array();
36
37     function onPluginVersion(array &$versions)
38     {
39         $versions[] = array('name' => 'SQLStats',
40                             'version' => GNUSOCIAL_VERSION,
41                             'author' => 'Evan Prodromou',
42                             'homepage' => 'http://status.net/wiki/Plugin:SQLStats',
43                             'rawdescription' =>
44                             // TRANS: Plugin decription.
45                             _m('Debug tool to watch for poorly indexed DB queries.'));
46
47         return true;
48     }
49
50     function onStartDBQuery($obj, $query, &$result)
51     {
52         $this->queryStart = microtime(true);
53         return true;
54     }
55
56     function onEndDBQuery($obj, $query, &$result)
57     {
58         $endTime = microtime(true);
59         $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000);
60         $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
61         $this->queryStart = 0;
62
63         return true;
64     }
65
66     function cleanup()
67     {
68         if (count($this->queryTimes) == 0) {
69             $this->log(LOG_INFO, sprintf('0 queries this hit.'));
70         } else {
71             $this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
72                                          count($this->queryTimes),
73                                          array_sum($this->queryTimes),
74                                          array_sum($this->queryTimes)/count($this->queryTimes),
75                                          max($this->queryTimes),
76                                          min($this->queryTimes)));
77         }
78
79         $verbose = common_config('sqlstats', 'verbose');
80
81         if ($verbose) {
82             foreach ($this->queries as $query) {
83                 $this->log(LOG_INFO, $query);
84             }
85         }
86     }
87 }