diff --git a/src/Console/RunServerCommand.php b/src/Console/RunServerCommand.php index 388f86b..b218beb 100644 --- a/src/Console/RunServerCommand.php +++ b/src/Console/RunServerCommand.php @@ -18,7 +18,7 @@ use Drift\Console\OutputPrinter; use Drift\Server\Adapter\KernelAdapter; use Drift\Server\Application; -use Drift\Server\ConsoleServerMessage; +use Drift\Server\ConsoleMasterServerMessage; use Drift\Server\Context\ServerContext; use Drift\Server\Mime\MimeTypeChecker; use React\EventLoop\LoopInterface; @@ -68,14 +68,15 @@ protected function executeServerCommand( $filesystem ) ->then(function (KernelAdapter $kernelAdapter) use ($application, $outputPrinter, $serverContext, $filesystem, &$forceShutdownReference) { - (new ConsoleServerMessage('Kernel built.', '~', true))->print($outputPrinter); - (new ConsoleServerMessage('Kernel preloaded.', '~', true))->print($outputPrinter); - (new ConsoleServerMessage('Kernel ready to accept requests.', '~', true))->print($outputPrinter); + (new ConsoleMasterServerMessage('Kernel built.', true))->print($outputPrinter); + (new ConsoleMasterServerMessage('Kernel preloaded.', true))->print($outputPrinter); + (new ConsoleMasterServerMessage('Kernel ready to accept requests.', true))->print($outputPrinter); if ($serverContext->hasExchanges()) { - (new ConsoleServerMessage('Kernel connected to exchanges.', '~', true))->print($outputPrinter); + (new ConsoleMasterServerMessage('Kernel connected to exchanges.', true))->print($outputPrinter); } + (new ConsoleMasterServerMessage('...', true))->print($outputPrinter); $applicationCallback = function () use ($application, $kernelAdapter, $filesystem, &$forceShutdownReference) { $application->runServer( $kernelAdapter, @@ -90,9 +91,10 @@ protected function executeServerCommand( $this->forkApplication($serverContext->getWorkers(), $applicationCallback, $outputPrinter); } }, function (Throwable $e) use ($outputPrinter) { - (new ConsoleServerMessage($e->getMessage(), '~', false))->print($outputPrinter); - (new ConsoleServerMessage('The server will shut down.', '~', false))->print($outputPrinter); - (new ConsoleServerMessage('Bye!', '~', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('...', false))->print($outputPrinter); + (new ConsoleMasterServerMessage($e->getMessage(), false))->print($outputPrinter); + (new ConsoleMasterServerMessage('The server will shut down.', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('Bye!', false))->print($outputPrinter); }); } @@ -117,7 +119,7 @@ private function forkApplication( case 0: $GLOBALS['number_of_process'] = str_pad(\strval($numberOfFork), 2, '0', STR_PAD_LEFT); - (new ConsoleServerMessage("Worker #$numberOfFork starting", '~', true))->print($outputPrinter); + (new ConsoleMasterServerMessage("Worker #$numberOfFork starting", true))->print($outputPrinter); $callable(); break; diff --git a/src/Console/ServerCommand.php b/src/Console/ServerCommand.php index f5669af..442498b 100644 --- a/src/Console/ServerCommand.php +++ b/src/Console/ServerCommand.php @@ -20,13 +20,14 @@ use Drift\EventLoop\EventLoopUtils; use Drift\Server\Console\Style\Muted; use Drift\Server\Console\Style\Purple; -use Drift\Server\ConsoleServerMessage; +use Drift\Server\ConsoleMasterServerMessage; use Drift\Server\Context\ServerContext; use Drift\Server\ServerHeaderPrinter; use Exception; use React\EventLoop\Loop; use React\EventLoop\LoopInterface; use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Formatter\OutputFormatterStyle; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -134,21 +135,22 @@ protected function execute(InputInterface $input, OutputInterface $output) $forceShutdownReference ); - (new ConsoleServerMessage('EventLoop is running.', '~', true))->print($outputPrinter); + (new ConsoleMasterServerMessage('EventLoop is running.', true))->print($outputPrinter); EventLoopUtils::runLoop($loop, (\intval($input->getOption('allowed-loop-stops')) + 1), function (int $timesMissing) use ($outputPrinter, &$forceShutdownReference) { if ($forceShutdownReference) { - (new ConsoleServerMessage( - sprintf('Loop forced to stop.'), '~', false) + (new ConsoleMasterServerMessage( + sprintf('Loop forced to stop.'), false) )->print($outputPrinter); } else { - (new ConsoleServerMessage( - sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), '~', false) + (new ConsoleMasterServerMessage( + sprintf('Rerunning EventLoop. %d retries missing', $timesMissing), false) )->print($outputPrinter); } }, $forceShutdownReference); - (new ConsoleServerMessage('EventLoop stopped.', '~', false))->print($outputPrinter); - (new ConsoleServerMessage('Closing the server.', '~', false))->print($outputPrinter); - (new ConsoleServerMessage('Bye bye!.', '~', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('...', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('EventLoop stopped.', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('Closing the server.', false))->print($outputPrinter); + (new ConsoleMasterServerMessage('Bye bye!.', false))->print($outputPrinter); return 0; } @@ -186,6 +188,7 @@ protected function createOutputPrinter( $outputFormatter = $output->getFormatter(); $outputFormatter->setStyle('muted', new Muted()); $outputFormatter->setStyle('purple', new Purple()); + $outputFormatter->setStyle('performance', new OutputFormatterStyle('gray')); return new OutputPrinter($output, $isQuiet, $isAlmostQuiet); } diff --git a/src/Console/Style/Performance.php b/src/Console/Style/Performance.php new file mode 100644 index 0000000..e8c76c7 --- /dev/null +++ b/src/Console/Style/Performance.php @@ -0,0 +1,27 @@ + + */ + +declare(strict_types=1); + +namespace Drift\Server\Console\Style; + +/** + * Class Purple. + */ +final class Performance extends CustomStyle +{ + public function apply(string $text) + { + return sprintf("\033[01;95m%s\e[0m", $text); + } +} diff --git a/src/ConsoleMasterServerMessage.php b/src/ConsoleMasterServerMessage.php new file mode 100644 index 0000000..893a30e --- /dev/null +++ b/src/ConsoleMasterServerMessage.php @@ -0,0 +1,64 @@ + + */ + +declare(strict_types=1); + +namespace Drift\Server; + +use Drift\Console\OutputPrinter; + +/** + * Class ConsoleMasterServerMessage. + */ +final class ConsoleMasterServerMessage implements Printable +{ + private string $message; + private bool $ok; + + /** + * @param string $message + * @param bool $ok + */ + public function __construct( + string $message, + bool $ok + ) { + $this->message = $message; + $this->ok = $ok; + } + + /** + * Print. + * + * @param OutputPrinter $outputPrinter + */ + public function print(OutputPrinter $outputPrinter) + { + if (!$outputPrinter->shouldPrintImportantOutput()) { + return; + } + + $color = 'red'; + if ($this->ok) { + $color = 'green'; + } + + $forkNumber = isset($GLOBALS['number_of_process']) + ? "[{$GLOBALS['number_of_process']}] " + : ''; + $outputPrinter->print("$forkNumberSRV"); + $outputPrinter->print(' '); + $outputPrinter->print(''.$this->message.''); + $outputPrinter->printLine(); + } +} diff --git a/src/ConsoleMessage.php b/src/ConsoleMessage.php new file mode 100644 index 0000000..43dc80d --- /dev/null +++ b/src/ConsoleMessage.php @@ -0,0 +1,46 @@ + + */ + +declare(strict_types=1); + +namespace Drift\Server; + +abstract class ConsoleMessage implements Printable +{ + /** + * @param string $elapsedType + * + * @return string + */ + protected function styledPerformance(string $elapsedType): string + { + $info = [ + $this->toLength($elapsedType, str_contains($elapsedType, 'μ') ? 8 : 7), + $this->toLength((string) (int) (memory_get_usage() / 1048576), 4), + $this->toLength((string) (int) (memory_get_usage(true) / 1048576), 4), + ]; + + return '['.implode('|', $info).']'; + } + + /** + * @param string $string + * @param int $length + * + * @return string + */ + protected function toLength(string $string, int $length): string + { + return str_pad($string, $length, ' ', STR_PAD_LEFT); + } +} diff --git a/src/ConsoleRequestMessage.php b/src/ConsoleRequestMessage.php index 7725cec..7518e61 100644 --- a/src/ConsoleRequestMessage.php +++ b/src/ConsoleRequestMessage.php @@ -20,7 +20,7 @@ /** * Class ConsoleRequestMessage. */ -final class ConsoleRequestMessage implements Printable +final class ConsoleRequestMessage extends ConsoleMessage { private string $url; private string $method; @@ -58,7 +58,7 @@ public function __construct( */ public function print(OutputPrinter $outputPrinter) { - $hasError = \intval($this->code) >= 400; + $hasError = $this->code >= 400; if ( !$outputPrinter->shouldPrintRegularOutput() && (!$outputPrinter->shouldPrintImportantOutput() || !$hasError) @@ -74,12 +74,12 @@ public function print(OutputPrinter $outputPrinter) $color = 'red'; } + $performance = $this->styledPerformance($this->elapsedTime); $forkNumber = isset($GLOBALS['number_of_process']) ? "[{$GLOBALS['number_of_process']}] " : ''; $outputPrinter->print("$forkNumber{$this->code}"); - $outputPrinter->print(" $method $this->url "); - $outputPrinter->print('('.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB)'); + $outputPrinter->print(" $performance $method $this->url "); if ($this->code >= 400) { $outputPrinter->print(' - '.$this->messageInMessage($this->message).''); } diff --git a/src/ConsoleServerMessage.php b/src/ConsoleServerMessage.php index ece14e9..6306f07 100644 --- a/src/ConsoleServerMessage.php +++ b/src/ConsoleServerMessage.php @@ -20,7 +20,7 @@ /** * Class ConsoleServerMessage. */ -final class ConsoleServerMessage implements Printable +final class ConsoleServerMessage extends ConsoleMessage { private string $message; private string $elapsedTime; @@ -59,13 +59,13 @@ public function print(OutputPrinter $outputPrinter) $color = 'green'; } + $performance = $this->styledPerformance($this->elapsedTime); $forkNumber = isset($GLOBALS['number_of_process']) ? "[{$GLOBALS['number_of_process']}] " : ''; $outputPrinter->print("$forkNumberSRV"); $outputPrinter->print(' '); - $outputPrinter->print('('.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB)'); - $outputPrinter->print(' - '.$this->message.''); + $outputPrinter->print(" $performance {$this->message}"); $outputPrinter->printLine(); } } diff --git a/src/ConsoleStaticMessage.php b/src/ConsoleStaticMessage.php index db1d356..086391b 100644 --- a/src/ConsoleStaticMessage.php +++ b/src/ConsoleStaticMessage.php @@ -20,7 +20,7 @@ /** * Class ConsoleStaticMessage. */ -final class ConsoleStaticMessage implements Printable +final class ConsoleStaticMessage extends ConsoleMessage { private string $url; private string $elapsedTime; @@ -52,12 +52,12 @@ public function print(OutputPrinter $outputPrinter) $method = str_pad('GET', 6, ' '); + $performance = $this->styledPerformance($this->elapsedTime); $forkNumber = isset($GLOBALS['number_of_process']) ? "[{$GLOBALS['number_of_process']}] " : ''; $outputPrinter->print("$forkNumber200"); - $outputPrinter->print(" $method $this->url "); - $outputPrinter->print('('.$this->elapsedTime.' | '.((int) (memory_get_usage() / 1000000)).' MB'); + $outputPrinter->print(" $performance $method $this->url "); $outputPrinter->printLine(); } } diff --git a/tests/ApplicationStaticFolderTest.php b/tests/ApplicationStaticFolderTest.php index 74889ba..6597bf6 100644 --- a/tests/ApplicationStaticFolderTest.php +++ b/tests/ApplicationStaticFolderTest.php @@ -40,7 +40,12 @@ public function testRegular() $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + '200', + $process->getOutput() + ); + + $this->assertStringContainsString( + ' GET ', $process->getOutput() ); diff --git a/tests/ApplicationTest.php b/tests/ApplicationTest.php index 1453ca2..a29e536 100644 --- a/tests/ApplicationTest.php +++ b/tests/ApplicationTest.php @@ -29,7 +29,12 @@ public function testRegular() Utils::curl("http://127.0.0.1:$port/query?code=200"); $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + '200', + $process->getOutput() + ); + + $this->assertStringContainsString( + ' GET ', $process->getOutput() ); @@ -50,7 +55,12 @@ public function testDefaultHost() Utils::curl("http://127.0.0.1:$port/query?code=200"); $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + '200', + $process->getOutput() + ); + + $this->assertStringContainsString( + ' GET ', $process->getOutput() ); @@ -71,7 +81,12 @@ public function testEmptyHost() Utils::curl("http://127.0.0.1:$port/query?code=200"); $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + '200', + $process->getOutput() + ); + + $this->assertStringContainsString( + ' GET ', $process->getOutput() ); @@ -118,17 +133,22 @@ public function testAlmostQuietServer() ); $this->assertStringNotContainsString( - '200 GET', + '200', $process->getOutput() ); $this->assertStringNotContainsString( - '300 GET', + '300', $process->getOutput() ); $this->assertStringContainsString( - '400 GET', + ' GET ', + $process->getOutput() + ); + + $this->assertStringContainsString( + '400', $process->getOutput() ); @@ -136,7 +156,7 @@ public function testAlmostQuietServer() } /** - * Test route not found. + * Test route not found */ public function testRouteNotFound() { @@ -145,7 +165,12 @@ public function testRouteNotFound() $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '404 GET', + ' GET ', + $process->getOutput() + ); + + $this->assertStringContainsString( + '404', $process->getOutput() ); @@ -162,8 +187,8 @@ public function testAnsi() Utils::curl("http://127.0.0.1:$port/query?code=404"); $this->waitForChange($process, $initialOutput); - $this->assertStringContainsString('200 GET', $process->getOutput()); - $this->assertStringContainsString('404 GET', $process->getOutput()); + $this->assertStringContainsString('200', $process->getOutput()); + $this->assertStringContainsString('404', $process->getOutput()); $process->stop(); } @@ -180,7 +205,12 @@ public function testAnotherPSR7Implementation() $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + ' GET ', + $process->getOutput() + ); + + $this->assertStringContainsString( + '200', $process->getOutput() ); @@ -229,8 +259,14 @@ public function testSymfonyAdapter() list($process, $port, $initialOutput) = $this->buildServer(['--no-ansi'], FakeSymfonyAdapter::class); Utils::curl("http://127.0.0.1:$port/query?code=200"); $this->waitForChange($process, $initialOutput); + + $this->assertStringContainsString( + ' GET ', + $process->getOutput() + ); + $this->assertStringContainsString( - '200 GET', + '200', $process->getOutput() ); diff --git a/tests/ServerOptionsTest.php b/tests/ServerOptionsTest.php index 202c90d..55fa86a 100644 --- a/tests/ServerOptionsTest.php +++ b/tests/ServerOptionsTest.php @@ -34,7 +34,12 @@ public function testMultipleOptionsAreWorking() $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '200 GET', + '200', + $process->getOutput() + ); + + $this->assertStringContainsString( + ' GET ', $process->getOutput() ); @@ -54,7 +59,12 @@ public function test0Concurrency() $this->waitForChange($process, $initialOutput); $this->assertStringContainsString( - '500 EXC', + '500', + $process->getOutput() + ); + + $this->assertStringContainsString( + 'EXC', $process->getOutput() ); diff --git a/tests/WatcherTest.php b/tests/WatcherTest.php index 7cea881..17f5748 100644 --- a/tests/WatcherTest.php +++ b/tests/WatcherTest.php @@ -33,7 +33,8 @@ public function testRegular() usleep(300000); $output = $process->getOutput(); $this->assertStringContainsString('Workers: 1', $output); - $this->assertStringContainsString('200 GET', $output); + $this->assertStringContainsString('200', $output); + $this->assertStringContainsString('GET', $output); $process->stop(); $processKill = new Process(['pkill', '-f', "0.0.0.0:$port", '-c']); diff --git a/tests/WorkersTest.php b/tests/WorkersTest.php index 5220502..f5fb71c 100644 --- a/tests/WorkersTest.php +++ b/tests/WorkersTest.php @@ -27,7 +27,8 @@ public function testOneWorker() $output = $this->waitForChange($process, $initialOutput); $this->assertStringContainsString('Workers: 1', $output); - $this->assertStringContainsString('200 GET', $output); + $this->assertStringContainsString('200', $output); + $this->assertStringContainsString(' GET ', $output); $this->assertStringNotContainsString('[00] ', $output); $this->assertStringNotContainsString('[01] ', $output); $this->assertStringNotContainsString('[02] ', $output); @@ -42,7 +43,8 @@ public function testNoWorkerDefault() $output = $this->waitForChange($process, $initialOutput); $this->assertStringContainsString('Workers: 1', $output); - $this->assertStringContainsString('200 GET', $output); + $this->assertStringContainsString('200', $output); + $this->assertStringContainsString(' GET ', $output); $this->assertStringNotContainsString('[00] ', $output); $this->assertStringNotContainsString('[01] ', $output); $this->assertStringNotContainsString('[02] ', $output);