]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/SQLStats/SQLStatsPlugin.php
[VersionBump] 1.19.0, fairly late
[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     const PLUGIN_VERSION = '2.0.0';
33
34     protected $queryCount = 0;
35     protected $queryStart = 0;
36     protected $queryTimes = array();
37     protected $queries    = array();
38
39     function onPluginVersion(array &$versions)
40     {
41         $versions[] = array('name' => 'SQLStats',
42                             'version' => self::PLUGIN_VERSION,
43                             'author' => 'Evan Prodromou',
44                             'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/SQLStats',
45                             'rawdescription' =>
46                             // TRANS: Plugin decription.
47                             _m('Debug tool to watch for poorly indexed DB queries.'));
48
49         return true;
50     }
51
52     function onStartDBQuery($obj, $query, &$result)
53     {
54         $this->queryStart = microtime(true);
55         return true;
56     }
57
58     function onEndDBQuery($obj, $query, &$result)
59     {
60         $endTime = microtime(true);
61         $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000);
62         $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
63         $this->queryStart = 0;
64
65         return true;
66     }
67
68     function cleanup()
69     {
70         if (count($this->queryTimes) == 0) {
71             $this->log(LOG_INFO, sprintf('0 queries this hit.'));
72         } else {
73             $this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
74                                          count($this->queryTimes),
75                                          array_sum($this->queryTimes),
76                                          array_sum($this->queryTimes)/count($this->queryTimes),
77                                          max($this->queryTimes),
78                                          min($this->queryTimes)));
79         }
80
81         $verbose = common_config('sqlstats', 'verbose');
82
83         if ($verbose) {
84             foreach ($this->queries as $query) {
85                 $this->log(LOG_INFO, $query);
86             }
87         }
88     }
89 }