From d9ca956ecf9fcae9fe6ddc7e392f4ea901e09ca2 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Apr 2011 22:46:28 -0400 Subject: [PATCH 1/5] SQLStatsPlugin to get some profiling data on SQL queries --- plugins/SQLStats/SQLStatsPlugin.php | 83 +++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 plugins/SQLStats/SQLStatsPlugin.php diff --git a/plugins/SQLStats/SQLStatsPlugin.php b/plugins/SQLStats/SQLStatsPlugin.php new file mode 100644 index 0000000000..9e810a3ffc --- /dev/null +++ b/plugins/SQLStats/SQLStatsPlugin.php @@ -0,0 +1,83 @@ +. + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * Check DB queries for filesorts and such and log em. + * + * @package SQLStatsPlugin + * @maintainer Evan Prodromou + */ + +class SQLStatsPlugin extends Plugin +{ + protected $queryCount = 0; + protected $queryStart = 0; + protected $queryTimes = array(); + protected $queries = array(); + + function onPluginVersion(&$versions) + { + $versions[] = array('name' => 'SQLStats', + 'version' => STATUSNET_VERSION, + 'author' => 'Evan Prodromou', + 'homepage' => 'http://status.net/wiki/Plugin:SQLStats', + 'rawdescription' => + _m('Debug tool to watch for poorly indexed DB queries.')); + + return true; + } + + function onStartDBQuery($obj, $query, &$result) + { + $this->queryStart = microtime(true); + return true; + } + + function onEndDBQuery($obj, $query, &$result) + { + $endTime = microtime(true); + $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000); + $this->queries[] = trim(preg_replace('/\s/', ' ', $query)); + $this->queryStart = 0; + + return true; + } + + function cleanup() + { + $this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)', + count($this->queryTimes), + array_sum($this->queryTimes), + array_sum($this->queryTimes)/count($this->queryTimes), + max($this->queryTimes), + min($this->queryTimes))); + + $verbose = common_config('sqlstats', 'verbose'); + + if ($verbose) { + foreach ($this->queries as $query) { + $this->log(LOG_INFO, $query); + } + } + } +} From 44c64816a5e90cc85769693e5f6bcb96934f57ef Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Apr 2011 22:47:17 -0400 Subject: [PATCH 2/5] cache groups per notice --- classes/Notice.php | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index a4f530c44f..8098a8ace8 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1250,28 +1250,40 @@ class Notice extends Memcached_DataObject return array(); } - // XXX: cache me + $ids = array(); + + $keypart = sprintf('notice:groups:%d', $this->id); + + $idstr = self::cacheGet($keypart); + + if ($idstr !== false) { + $ids = explode(',', $idstr); + } else { + $gi = new Group_inbox(); + + $gi->selectAdd(); + $gi->selectAdd('group_id'); + + $gi->notice_id = $this->id; + + if ($gi->find()) { + while ($gi->fetch()) { + $ids[] = $gi->group_id; + } + } + + self::cacheSet($keypart, implode(',', $ids)); + } $groups = array(); - $gi = new Group_inbox(); - - $gi->selectAdd(); - $gi->selectAdd('group_id'); - - $gi->notice_id = $this->id; - - if ($gi->find()) { - while ($gi->fetch()) { - $group = User_group::staticGet('id', $gi->group_id); - if ($group) { - $groups[] = $group; - } + foreach ($ids as $id) { + $group = User_group::staticGet('id', $id); + if ($group) { + $groups[] = $group; } } - $gi->free(); - return $groups; } From 2a124c1397d92ab4b50627144a54d33c1695d691 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Apr 2011 22:48:33 -0400 Subject: [PATCH 3/5] make User_group use caching staticGet() --- classes/User_group.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/classes/User_group.php b/classes/User_group.php index f72cc57533..e993961118 100644 --- a/classes/User_group.php +++ b/classes/User_group.php @@ -31,7 +31,9 @@ class User_group extends Memcached_DataObject public $force_scope; // tinyint /* Static get */ - function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('User_group',$k,$v); } + function staticGet($k,$v=NULL) { + return Memcached_DataObject::staticGet('User_group',$k,$v); + } /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE From b8fdf6636fb37013886788422629e3c7dcee22dd Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Apr 2011 22:49:09 -0400 Subject: [PATCH 4/5] make Group_block use caching staticGet() --- classes/Group_block.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Group_block.php b/classes/Group_block.php index ffc57a496e..68feaef4de 100644 --- a/classes/Group_block.php +++ b/classes/Group_block.php @@ -35,7 +35,7 @@ class Group_block extends Memcached_DataObject public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP /* Static get */ - function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Group_block',$k,$v); } + function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Group_block',$k,$v); } /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE From fa8592f50be74c84f8db5750513ee3914bf105e0 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 6 Apr 2011 22:51:46 -0400 Subject: [PATCH 5/5] Make Login_token use caching staticGet() --- classes/Login_token.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/classes/Login_token.php b/classes/Login_token.php index 20d5d9dbce..7a9388c947 100644 --- a/classes/Login_token.php +++ b/classes/Login_token.php @@ -35,7 +35,7 @@ class Login_token extends Memcached_DataObject public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP /* Static get */ - function staticGet($k,$v=NULL) { return DB_DataObject::staticGet('Login_token',$k,$v); } + function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('Login_token',$k,$v); } /* the code above is auto generated do not remove the tag below */ ###END_AUTOCODE