From 43d919799635cdfaf545ed9919e2937d0965500e Mon Sep 17 00:00:00 2001 From: Gunnar Wrobel Date: Mon, 20 Dec 2010 22:05:54 +0100 Subject: [PATCH] Allow measuring the time spent running the script. --- framework/Kolab_Cli/lib/Horde/Kolab/Cli.php | 16 +++++++++- .../Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php | 8 +++++ framework/Kolab_Cli/package.xml | 6 ++++ .../test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php | 11 +++++++ .../test/Horde/Kolab/Cli/Unit/CliTest.php | 35 ++++++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) diff --git a/framework/Kolab_Cli/lib/Horde/Kolab/Cli.php b/framework/Kolab_Cli/lib/Horde/Kolab/Cli.php index b8b56cc3c..c9c64736b 100644 --- a/framework/Kolab_Cli/lib/Horde/Kolab/Cli.php +++ b/framework/Kolab_Cli/lib/Horde/Kolab/Cli.php @@ -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(); } diff --git a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php index db9b67533..8142999cb 100644 --- a/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php +++ b/framework/Kolab_Cli/lib/Horde/Kolab/Cli/Module/Base.php @@ -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.') + ) + ), ); } diff --git a/framework/Kolab_Cli/package.xml b/framework/Kolab_Cli/package.xml index 315f09f09..5dbd06d6b 100644 --- a/framework/Kolab_Cli/package.xml +++ b/framework/Kolab_Cli/package.xml @@ -94,6 +94,12 @@ pear.horde.org + + + Support + pear.horde.org + + diff --git a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php index 3710f7a6f..44569c858 100644 --- a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php +++ b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/Cli/OptionsTest.php @@ -89,4 +89,15 @@ extends Horde_Kolab_Cli_TestCase $this->runCli() ); } + + public function testOptionTimed() + { + $_SERVER['argv'] = array( + 'klb' + ); + $this->assertRegExp( + '/-t,[ ]*--timed/', + $this->runCli() + ); + } } diff --git a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/CliTest.php b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/CliTest.php index 87b3f97be..cff1aa8e7 100644 --- a/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/CliTest.php +++ b/framework/Kolab_Cli/test/Horde/Kolab/Cli/Unit/CliTest.php @@ -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()); + } } -- 2.11.0