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 <brion@status.net>
31 class SQLStatsPlugin extends Plugin
33 protected $queryCount = 0;
34 protected $queryStart = 0;
35 protected $queryTimes = array();
36 protected $queries = array();
38 function onPluginVersion(&$versions)
40 $versions[] = array('name' => 'SQLStats',
41 'version' => STATUSNET_VERSION,
42 'author' => 'Evan Prodromou',
43 'homepage' => 'http://status.net/wiki/Plugin:SQLStats',
45 _m('Debug tool to watch for poorly indexed DB queries.'));
50 function onStartDBQuery($obj, $query, &$result)
52 $this->queryStart = microtime(true);
56 function onEndDBQuery($obj, $query, &$result)
58 $endTime = microtime(true);
59 $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000);
60 $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
61 $this->queryStart = 0;
68 $this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
69 count($this->queryTimes),
70 array_sum($this->queryTimes),
71 array_sum($this->queryTimes)/count($this->queryTimes),
72 max($this->queryTimes),
73 min($this->queryTimes)));
75 $verbose = common_config('sqlstats', 'verbose');
78 foreach ($this->queries as $query) {
79 $this->log(LOG_INFO, $query);