Skip to content

Commit

Permalink
Displays memory usage from workers and only on swoole (#304)
Browse files Browse the repository at this point in the history
* Displays memory usage from workers and only on swoole

* Apply fixes from StyleCI (#303)
  • Loading branch information
nunomaduro authored Jun 1, 2021
1 parent 549e122 commit 3247a07
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/Commands/Concerns/InteractsWithIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,20 +95,23 @@ public function requestInfo($request, $verbosity = null)

$url = parse_url($request['url'], PHP_URL_PATH) ?: '/';
$duration = number_format(round($request['duration'], 2), 2, '.', '');
$memory = number_format(round($request['memory'] ?? memory_get_usage() / 1024 / 1204, 2), 2, '.', '');

$memory = isset($request['memory'])
? (number_format($request['memory'] / 1024 / 1204, 2, '.', '').' mb ')
: '';

['method' => $method, 'statusCode' => $statusCode] = $request;

$dots = str_repeat('.', max($terminalWidth - strlen($method.$url.$duration.$memory) - 19, 0));
$dots = str_repeat('.', max($terminalWidth - strlen($method.$url.$duration.$memory) - 16, 0));

if (empty($dots) && ! $this->output->isVerbose()) {
$url = substr($url, 0, $terminalWidth - strlen($method.$duration) - 15 - 3).'...';
$url = substr($url, 0, $terminalWidth - strlen($method.$duration.$memory) - 15 - 3).'...';
} else {
$dots .= ' ';
}

$this->output->writeln(sprintf(
' <fg=%s;options=bold>%s </> <fg=cyan;options=bold>%s</> <options=bold>%s</><fg=#6C7280> %s%s mb %s ms</>',
' <fg=%s;options=bold>%s </> <fg=cyan;options=bold>%s</> <options=bold>%s</><fg=#6C7280> %s%s%s ms</>',
match (true) {
$statusCode >= 500 => 'red',
$statusCode >= 400 => 'yellow',
Expand Down
1 change: 1 addition & 0 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public static function request(string $method, string $url, int $statusCode, flo
'type' => 'request',
'method' => $method,
'url' => $url,
'memory' => memory_get_usage(),
'statusCode' => $statusCode,
'duration' => $duration,
])."\n");
Expand Down
12 changes: 6 additions & 6 deletions tests/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ public function test_request()
'method' => 'GET',
'url' => 'http://127.0.0.1/welcome',
'statusCode' => '200',
'memory' => 23.43,
'memory' => 17393560,
'duration' => 10,
]);

$command->requestInfo([
'method' => 'POST',
'url' => 'http://127.0.0.1:8080',
'statusCode' => '404',
'memory' => 26.43,
'memory' => 20393560,
'duration' => 1234,
]);

$command->requestInfo([
'method' => 'POST',
'url' => 'http://127.0.0.1:8080/'.str_repeat('foo', 100),
'statusCode' => 500,
'memory' => 28.43,
'memory' => 30393560,
'duration' => 4567854,
]);

$this->assertEquals(<<<'EOF'
200 GET /welcome .......... 23.43 mb 10.00 ms
404 POST / .............. 26.43 mb 1234.00 ms
500 POST /foofoofoofoofoofo... 28.43 mb 4567854.00 ms
200 GET /welcome ......... 14.11 mb 10.00 ms
404 POST / ............. 16.54 mb 1234.00 ms
500 POST /foofoofo... 24.65 mb 4567854.00 ms

EOF, $output->fetch());
}
Expand Down

0 comments on commit 3247a07

Please sign in to comment.