From f1a8ca6e4e5fab113bef837b8b27998d2b6af7a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20H=C5=AFla?= Date: Tue, 8 Dec 2020 12:34:20 +0100 Subject: [PATCH] Test: measure run time duration (#426) --- src/Runner/Job.php | 16 ++++++++++++++++ src/Runner/Test.php | 15 ++++++++++++++- src/Runner/TestHandler.php | 2 +- tests/Runner/Job.phpt | 1 + tests/Runner/Runner.misc.phpt | 9 +++++---- tests/Runner/Test.phpt | 1 + 6 files changed, 38 insertions(+), 6 deletions(-) diff --git a/src/Runner/Job.php b/src/Runner/Job.php index 56efbe48..d8b8c1ae 100644 --- a/src/Runner/Job.php +++ b/src/Runner/Job.php @@ -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) { @@ -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), @@ -156,6 +160,7 @@ public function isRunning(): bool if ($status['running']) { return true; } + $this->duration += microtime(true); fclose($this->stdout); if ($this->stderr) { @@ -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; + } } diff --git a/src/Runner/Test.php b/src/Runner/Test.php index 90e96204..530a0ed0 100644 --- a/src/Runner/Test.php +++ b/src/Runner/Test.php @@ -39,6 +39,9 @@ class Test /** @var int */ private $result = self::PREPARED; + /** @var float|null */ + private $duration; + /** @var string[]|string[][] */ private $args = []; @@ -87,6 +90,15 @@ public function hasResult(): bool } + /** + * Duration in seconds. + */ + public function getDuration(): ?float + { + return $this->duration; + } + + /** * @return static */ @@ -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'."); @@ -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; } } diff --git a/src/Runner/TestHandler.php b/src/Runner/TestHandler.php index fec9a523..d11d3b32 100644 --- a/src/Runner/TestHandler.php +++ b/src/Runner/TestHandler.php @@ -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())); } diff --git a/tests/Runner/Job.phpt b/tests/Runner/Job.phpt index 0fe5950a..2e5ffe22 100644 --- a/tests/Runner/Job.phpt +++ b/tests/Runner/Job.phpt @@ -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()); diff --git a/tests/Runner/Runner.misc.phpt b/tests/Runner/Runner.misc.phpt index 2e50fcc3..f0c5b5c2 100644 --- a/tests/Runner/Runner.misc.phpt +++ b/tests/Runner/Runner.misc.phpt @@ -3,6 +3,7 @@ declare(strict_types=1); use Tester\Assert; +use Tester\Expect; use Tester\Runner\Test; require __DIR__ . '/../bootstrap.php'; @@ -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()]; } @@ -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); diff --git a/tests/Runner/Test.phpt b/tests/Runner/Test.phpt index 61241fb2..52ab34f8 100644 --- a/tests/Runner/Test.phpt +++ b/tests/Runner/Test.phpt @@ -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()); });