Skip to content

Commit eac531a

Browse files
committed
Added command timing
1 parent b9455d0 commit eac531a

File tree

3 files changed

+35
-10
lines changed

3 files changed

+35
-10
lines changed

examples/complex_statement.php

+5-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,11 @@
99

1010
// This example probably only works in real BASH on Linux/UNIX
1111
// It will output a sorted list of directories in the $PATH
12-
//$cmd = Command::factory('IFS=":"; (for DIR in $PATH; do echo $DIR; done) | sort', $escape)->run();
13-
$cmd = Command::factory('IFS=":"; (for DIR in $PATH; do echo $DIR; done) | sort', $no_escape)->run();
12+
$cmd = Command::factory('IFS=":"; (for DIR in $PATH; do echo $DIR; done) | sort', $no_escape)
13+
// Throw an exception if it fails
14+
->useExceptions()
15+
->run();
1416

15-
if ($cmd->getExitCode() === 0) {
16-
echo "STDOUT:\n";
17-
echo $cmd->getStdOut();
18-
} else {
19-
echo "STDERR:\n";
20-
echo $cmd->getStdErr();
21-
}
17+
echo $cmd->getStdOut();
2218

2319
echo "Done.\n";

examples/timing.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
use kamermans\Command\Command;
4+
5+
require_once __DIR__.'/../vendor/autoload.php';
6+
7+
$cmd = Command::factory('sleep 2 && echo "OK"', true)
8+
// Throw an exception if it fails
9+
->useExceptions()
10+
->run();
11+
12+
echo $cmd->getStdOut();
13+
echo "Finished in ".$cmd->getDuration()." second(s)\n";
14+
echo " (with microseconds: ".$cmd->getDuration(true).")\n";

src/Command.php

+16-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ class Command
3333
protected $_stdout = null;
3434
protected $_stderr = null;
3535
protected $_callback = null;
36+
protected $_timestart = null;
37+
protected $_timeend = null;
3638
protected $_cwd = null;
3739
protected $_env = null;
3840
protected $_conf = array();
@@ -189,6 +191,7 @@ public function run($stdin = null)
189191
$this->_exitcode = null;
190192
$this->_stdout = null;
191193
$this->_stderr = null;
194+
$this->_timestart = microtime(true);
192195

193196
// Prepare the buffers structure
194197
$buffers = array(
@@ -197,9 +200,10 @@ public function run($stdin = null)
197200
2 => &$this->_stderr,
198201
);
199202
$this->_exitcode = self::exec($this->getFullCommand(), $buffers, $this->_callback, $this->_cwd, $this->_env, $this->_conf);
203+
$this->_timeend = microtime(true);
200204

201205
if ($this->_throw && $this->_exitcode !== 0) {
202-
throw new CommandException($this, "Command failed '".$this->getFullCommand()."':\n".trim($this->getStdErr()));
206+
throw new CommandException($this, "Command failed '$this':\n".trim($this->getStdErr()));
203207
}
204208

205209
return $this;
@@ -250,6 +254,17 @@ public function getStdErr()
250254
return $this->_stderr;
251255
}
252256

257+
/**
258+
* Gets the duration of the command execution
259+
*
260+
* @param bool $microseconds If true, return microseconds (float), otherwise seconds (int)
261+
* @return int|float
262+
*/
263+
public function getDuration($microseconds=false)
264+
{
265+
$duration = $this->_timeend - $this->_timestart;
266+
return $microseconds? $duration: round($duration);
267+
}
253268

254269
/**
255270
* Executes a command returning the exitcode and capturing the stdout and stderr

0 commit comments

Comments
 (0)