3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET')) {
25 * Check DB queries for filesorts and such and log em.
27 * @package SQLStatsPlugin
28 * @maintainer Evan Prodromou <evan@status.net>
30 class SQLStatsPlugin extends Plugin
32 const PLUGIN_VERSION = '2.0.0';
34 protected $queryCount = 0;
35 protected $queryStart = 0;
36 protected $queryTimes = array();
37 protected $queries = array();
39 function onPluginVersion(array &$versions)
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',
46 // TRANS: Plugin decription.
47 _m('Debug tool to watch for poorly indexed DB queries.'));
52 function onStartDBQuery($obj, $query, &$result)
54 $this->queryStart = microtime(true);
58 function onEndDBQuery($obj, $query, &$result)
60 $endTime = microtime(true);
61 $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000);
62 $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
63 $this->queryStart = 0;
70 if (count($this->queryTimes) == 0) {
71 $this->log(LOG_INFO, sprintf('0 queries this hit.'));
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)));
81 $verbose = common_config('sqlstats', 'verbose');
84 foreach ($this->queries as $query) {
85 $this->log(LOG_INFO, $query);