SQLStatsPlugin.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. if (!defined('STATUSNET')) {
  20. exit(1);
  21. }
  22. /**
  23. * Check DB queries for filesorts and such and log em.
  24. *
  25. * @package SQLStatsPlugin
  26. * @maintainer Evan Prodromou <evan@status.net>
  27. */
  28. class SQLStatsPlugin extends Plugin
  29. {
  30. const PLUGIN_VERSION = '2.0.0';
  31. protected $queryCount = 0;
  32. protected $queryStart = 0;
  33. protected $queryTimes = array();
  34. protected $queries = array();
  35. public function onPluginVersion(array &$versions): bool
  36. {
  37. $versions[] = array('name' => 'SQLStats',
  38. 'version' => self::PLUGIN_VERSION,
  39. 'author' => 'Evan Prodromou',
  40. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SQLStats',
  41. 'rawdescription' =>
  42. // TRANS: Plugin decription.
  43. _m('Debug tool to watch for poorly indexed DB queries.'));
  44. return true;
  45. }
  46. function onStartDBQuery($obj, $query, &$result)
  47. {
  48. $this->queryStart = microtime(true);
  49. return true;
  50. }
  51. function onEndDBQuery($obj, $query, &$result)
  52. {
  53. $endTime = microtime(true);
  54. $this->queryTimes[] = round(($endTime - $this->queryStart) * 1000);
  55. $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
  56. $this->queryStart = 0;
  57. return true;
  58. }
  59. function cleanup()
  60. {
  61. if (count($this->queryTimes) == 0) {
  62. $this->log(LOG_INFO, sprintf('0 queries this hit.'));
  63. } else {
  64. $this->log(LOG_INFO, sprintf('%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
  65. count($this->queryTimes),
  66. array_sum($this->queryTimes),
  67. array_sum($this->queryTimes)/count($this->queryTimes),
  68. max($this->queryTimes),
  69. min($this->queryTimes)));
  70. }
  71. $verbose = common_config('sqlstats', 'verbose');
  72. if ($verbose) {
  73. foreach ($this->queries as $query) {
  74. $this->log(LOG_INFO, $query);
  75. }
  76. }
  77. }
  78. }