Skip to content

Commit

Permalink
Test: measure run time duration (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
milo authored and dg committed Mar 1, 2021
1 parent 6591365 commit f8f9cd2
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 6 deletions.
16 changes: 16 additions & 0 deletions src/Runner/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ class Job
/** @var string[] output headers */
private $headers = [];

/** @var float|null */
private $duration;


public function __construct(Test $test, PhpInterpreter $interpreter, array $envVars = null)
{
Expand Down Expand Up @@ -100,6 +103,7 @@ public function run(int $flags = 0): void
: Helpers::escapeArg($value);
}

$this->duration = -microtime(true);
$this->proc = proc_open(
$this->interpreter->getCommandLine()
. ' -d register_argc_argv=on ' . Helpers::escapeArg($this->test->getFile()) . ' ' . implode(' ', $args),
Expand Down Expand Up @@ -156,6 +160,7 @@ public function isRunning(): bool
if ($status['running']) {
return true;
}
$this->duration += microtime(true);

fclose($this->stdout);
if ($this->stderr) {
Expand Down Expand Up @@ -202,4 +207,15 @@ public function getHeaders(): array
{
return $this->headers;
}


/**
* Returns process duration in seconds.
*/
public function getDuration(): ?float
{
return $this->duration > 0
? $this->duration
: null;
}
}
15 changes: 14 additions & 1 deletion src/Runner/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class Test
/** @var int */
private $result = self::PREPARED;

/** @var float|null */
private $duration;

/** @var string[]|string[][] */
private $args = [];

Expand Down Expand Up @@ -87,6 +90,15 @@ public function hasResult(): bool
}


/**
* Duration in seconds.
*/
public function getDuration(): ?float
{
return $this->duration;
}


/**
* @return static
*/
Expand All @@ -111,7 +123,7 @@ public function withArguments(array $args): self
/**
* @return static
*/
public function withResult(int $result, ?string $message): self
public function withResult(int $result, ?string $message, float $duration = null): self
{
if ($this->hasResult()) {
throw new \LogicException("Result of test is already set to $this->result with message '$this->message'.");
Expand All @@ -120,6 +132,7 @@ public function withResult(int $result, ?string $message): self
$me = clone $this;
$me->result = $result;
$me->message = $message;
$me->duration = $duration;
return $me;
}
}
2 changes: 1 addition & 1 deletion src/Runner/TestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function assess(Job $job): void
}
}
}
$this->runner->finishTest($test->withResult(Test::PASSED, $test->message));
$this->runner->finishTest($test->withResult(Test::PASSED, $test->message, $job->getDuration()));
}


Expand Down
1 change: 1 addition & 0 deletions tests/Runner/Job.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test(function () {

Assert::same('Args: one, --two=1, three, --two=2+stdout', $job->getTest()->stdout);
Assert::same('+stderr1+stderr2', $job->getTest()->stderr);
Assert::type('float', $job->getDuration());

if (PHP_SAPI !== 'cli') {
Assert::contains('Nette Tester', $job->getHeaders());
Expand Down
9 changes: 5 additions & 4 deletions tests/Runner/Runner.misc.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
declare(strict_types=1);

use Tester\Assert;
use Tester\Expect;
use Tester\Runner\Test;

require __DIR__ . '/../bootstrap.php';
Expand All @@ -24,7 +25,7 @@ class Logger implements Tester\Runner\OutputHandler

public function finish(Test $test): void
{
$this->results[basename($test->getFile())] = $test->getResult();
$this->results[basename($test->getFile())] = [$test->getResult(), $test->getDuration()];
}


Expand All @@ -50,7 +51,7 @@ $runner->run();
Assert::false(getenv('TesterEnvVar'));

ksort($logger->results);
Assert::same([
'addPhpIniOption.phptx' => Test::PASSED,
'env-vars.phptx' => Test::PASSED,
Assert::equal([
'addPhpIniOption.phptx' => [Test::PASSED, Expect::type('float')],
'env-vars.phptx' => [Test::PASSED, Expect::type('float')],
], $logger->results);
1 change: 1 addition & 0 deletions tests/Runner/Test.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test(function () {
Assert::same('some/Test.phpt', $test->getSignature());
Assert::false($test->hasResult());
Assert::same(Test::PREPARED, $test->getResult());
Assert::null($test->getDuration());
});


Expand Down

0 comments on commit f8f9cd2

Please sign in to comment.