[icinga-checkins] icinga.org: icingaweb2-module-director/master: cli/config: add profiler

git at icinga.org git at icinga.org
Sat Jul 23 10:13:39 CEST 2016


Module: icingaweb2-module-director
Branch: master
Commit: 247ff404593cb41cfae01c70f9c6b75bfe723b74
URL:    https://git.icinga.org/?p=icingaweb2-module-director.git;a=commit;h=247ff404593cb41cfae01c70f9c6b75bfe723b74

Author: Thomas Gelf <thomas at gelf.net>
Date:   Fri Jul 22 17:20:57 2016 +0200

cli/config: add profiler

---

 application/clicommands/ConfigCommand.php |   43 +++++++++++++++++++++++++++++
 1 file changed, 43 insertions(+)

diff --git a/application/clicommands/ConfigCommand.php b/application/clicommands/ConfigCommand.php
index 4cc40d6..092245b 100644
--- a/application/clicommands/ConfigCommand.php
+++ b/application/clicommands/ConfigCommand.php
@@ -17,6 +17,11 @@ class ConfigCommand extends Command
      */
     public function renderAction()
     {
+        $profile = $this->params->shift('profile');
+        if ($profile) {
+            $this->enableDbProfiler();
+        }
+
         $config = new IcingaConfig($this->db());
         Benchmark::measure('Rendering config');
         if ($config->hasBeenModified()) {
@@ -35,6 +40,44 @@ class ConfigCommand extends Command
                 $checksum
             );
         }
+
+        if ($profile) {
+            $this->dumpDbProfile();
+        }
+    }
+
+    protected function dumpDbProfile()
+    {
+        $profiler = $this->getDbProfiler();
+
+        $totalTime    = $profiler->getTotalElapsedSecs();
+        $queryCount   = $profiler->getTotalNumQueries();
+        $longestTime  = 0;
+        $longestQuery = null;
+
+        foreach ($profiler->getQueryProfiles() as $query) {
+            echo $query->getQuery() . "\n";
+            if ($query->getElapsedSecs() > $longestTime) {
+                $longestTime  = $query->getElapsedSecs();
+                $longestQuery = $query->getQuery();
+            }
+        }
+
+        echo 'Executed ' . $queryCount . ' queries in ' . $totalTime . ' seconds' . "\n";
+        echo 'Average query length: ' . $totalTime / $queryCount . ' seconds' . "\n";
+        echo 'Queries per second: ' . $queryCount / $totalTime . "\n";
+        echo 'Longest query length: ' . $longestTime . "\n";
+        echo "Longest query: \n" . $longestQuery . "\n";
+    }
+
+    protected function getDbProfiler()
+    {
+        return $this->db()->getDbAdapter()->getProfiler();
+    }
+
+    protected function enableDbProfiler()
+    {
+        return $this->getDbProfiler()->setEnabled(true);
     }
 
     /**



More information about the icinga-checkins mailing list