SQLStatsPlugin.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /*
  17. * Check DB queries for filesorts and such and log em.
  18. *
  19. * @package SQLStatsPlugin
  20. * @author Evan Prodromou <evan@status.net>
  21. * @copyright 2011 StatusNet, Inc.
  22. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  23. */
  24. defined('GNUSOCIAL') || die();
  25. /**
  26. * Check DB queries for filesorts and such and log em.
  27. *
  28. * @package SQLStatsPlugin
  29. * @author Evan Prodromou <evan@status.net>
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class SQLStatsPlugin extends Plugin
  33. {
  34. const PLUGIN_VERSION = '2.0.0';
  35. protected $queryCount = 0;
  36. protected $queryStart = 0;
  37. protected $queryTimes = array();
  38. protected $queries = array();
  39. public function onPluginVersion(array &$versions): bool
  40. {
  41. $versions[] = array('name' => 'SQLStats',
  42. 'version' => self::PLUGIN_VERSION,
  43. 'author' => 'Evan Prodromou',
  44. 'homepage' => GNUSOCIAL_ENGINE_REPO_URL . 'tree/master/plugins/SQLStats',
  45. 'rawdescription' =>
  46. // TRANS: Plugin decription.
  47. _m('Debug tool to watch for poorly indexed DB queries.'));
  48. return true;
  49. }
  50. public function onStartDBQuery($obj, $query, &$result)
  51. {
  52. $this->queryStart = hrtime(true);
  53. return true;
  54. }
  55. public function onEndDBQuery($obj, $query, &$result)
  56. {
  57. $endTime = hrtime(true);
  58. $this->queryTimes[] = round(($endTime - $this->queryStart) / 1000000);
  59. $this->queries[] = trim(preg_replace('/\s/', ' ', $query));
  60. $this->queryStart = 0;
  61. return true;
  62. }
  63. public function cleanup()
  64. {
  65. if (count($this->queryTimes) == 0) {
  66. $this->log(LOG_INFO, sprintf('0 queries this hit.'));
  67. } else {
  68. $this->log(LOG_INFO, sprintf(
  69. '%d queries this hit (total = %d, avg = %d, max = %d, min = %d)',
  70. count($this->queryTimes),
  71. array_sum($this->queryTimes),
  72. array_sum($this->queryTimes) / count($this->queryTimes),
  73. max($this->queryTimes),
  74. min($this->queryTimes)
  75. ));
  76. }
  77. $verbose = common_config('sqlstats', 'verbose');
  78. if ($verbose) {
  79. foreach ($this->queries as $query) {
  80. $this->log(LOG_INFO, $query);
  81. }
  82. }
  83. }
  84. }