Allow measuring the time spent running the script.
authorGunnar Wrobel <p@rdus.de>
Mon, 20 Dec 2010 21:05:54 +0000 (22:05 +0100)
committerGunnar Wrobel <p@rdus.de>
Tue, 4 Jan 2011 07:54:11 +0000 (08:54 +0100)
framework/Kolab_Cli/lib/Horde/Kolab/Cli.php
framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php
framework/Kolab_Cli/package.xml
framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php
framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/CliTest.php

index b8b56cc..c9c6473 100644 (file)
@@ -55,9 +55,23 @@ class Horde_Kolab_Cli
             $parser->printHelp();
         } else {
             try {
+                if (!empty($options['timed'])
+                    && class_exists('Horde_Support_Timer')) {
+                    $timer = new Horde_Support_Timer();
+                    $timer->push();
+                } else {
+                    $timer = false;
+                }
                 $modular->getProvider()
                     ->getModule(ucfirst($arguments[0]))
-                    ->run($options, $arguments);
+                    ->run($cli, $options, $arguments);
+                if (!empty($options['timed'])) {
+                    if ($timer) {
+                        $cli->message(floor($timer->pop() * 1000) . ' ms');
+                    } else {
+                        $cli->message('The class Horde_Support_Timer seems to be missing!');
+                    }
+                }
             } catch (Horde_Cli_Modular_Exception $e) {
                 $parser->printHelp();
             }
index db9b675..8142999 100644 (file)
@@ -92,6 +92,14 @@ Choices are:
                     'help'   => Horde_Kolab_Cli_Translation::t('The host that holds the data.')
                 )
             ),
+            new Horde_Argv_Option(
+                '-t',
+                '--timed',
+                array(
+                    'action' => 'store_true',
+                    'help'   => Horde_Kolab_Cli_Translation::t('Produce time measurements to indicate how long the processing takes.')
+                )
+            ),
         );
     }
 
index 315f09f..5dbd06d 100644 (file)
     <channel>pear.horde.org</channel>
    </package>
   </required>
+  <optional>
+   <package>
+    <name>Support</name>
+    <channel>pear.horde.org</channel>
+   </package>
+  </optional>
  </dependencies>
  <phprelease>
   <filelist>
index 3710f7a..44569c8 100644 (file)
@@ -89,4 +89,15 @@ extends Horde_Kolab_Cli_TestCase
             $this->runCli()
         );
     }
+
+    public function testOptionTimed()
+    {
+        $_SERVER['argv'] = array(
+            'klb'
+        );
+        $this->assertRegExp(
+            '/-t,[ ]*--timed/',
+            $this->runCli()
+        );
+    }
 }
index 87b3f97..cff1aa8 100644 (file)
@@ -64,4 +64,39 @@ extends Horde_Kolab_Cli_TestCase
         );
         $this->assertContains('INBOX', $this->runCli());
     }
+
+    public function testTimeInfo()
+    {
+        $_SERVER['argv'] = array(
+            'klb',
+            '--driver=mock',
+            '--timed',
+            '--user=test',
+            'folder'
+        );
+        $this->assertContains('[  INFO  ]', $this->runCli());
+    }
+
+    public function testTimed()
+    {
+        $_SERVER['argv'] = array(
+            'klb',
+            '--driver=mock',
+            '--timed',
+            '--user=test',
+            'folder'
+        );
+        $this->assertRegExp('/\[  INFO  \] [0-9]+ ms/', $this->runCli());
+    }
+
+    public function testTimeMissing()
+    {
+        $_SERVER['argv'] = array(
+            'klb',
+            '--driver=mock',
+            '--user=test',
+            'folder'
+        );
+        $this->assertNotContains('[  INFO  ]', $this->runCli());
+    }
 }