Skip to content

Commit 3247a07

Browse files
authored
Displays memory usage from workers and only on swoole (#304)
* Displays memory usage from workers and only on swoole * Apply fixes from StyleCI (#303)
1 parent 549e122 commit 3247a07

File tree

3 files changed

+14
-10
lines changed

3 files changed

+14
-10
lines changed

src/Commands/Concerns/InteractsWithIO.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,20 +95,23 @@ public function requestInfo($request, $verbosity = null)
9595

9696
$url = parse_url($request['url'], PHP_URL_PATH) ?: '/';
9797
$duration = number_format(round($request['duration'], 2), 2, '.', '');
98-
$memory = number_format(round($request['memory'] ?? memory_get_usage() / 1024 / 1204, 2), 2, '.', '');
98+
99+
$memory = isset($request['memory'])
100+
? (number_format($request['memory'] / 1024 / 1204, 2, '.', '').' mb ')
101+
: '';
99102

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

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

104107
if (empty($dots) && ! $this->output->isVerbose()) {
105-
$url = substr($url, 0, $terminalWidth - strlen($method.$duration) - 15 - 3).'...';
108+
$url = substr($url, 0, $terminalWidth - strlen($method.$duration.$memory) - 15 - 3).'...';
106109
} else {
107110
$dots .= ' ';
108111
}
109112

110113
$this->output->writeln(sprintf(
111-
' <fg=%s;options=bold>%s </> <fg=cyan;options=bold>%s</> <options=bold>%s</><fg=#6C7280> %s%s mb %s ms</>',
114+
' <fg=%s;options=bold>%s </> <fg=cyan;options=bold>%s</> <options=bold>%s</><fg=#6C7280> %s%s%s ms</>',
112115
match (true) {
113116
$statusCode >= 500 => 'red',
114117
$statusCode >= 400 => 'yellow',

src/Stream.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public static function request(string $method, string $url, int $statusCode, flo
2121
'type' => 'request',
2222
'method' => $method,
2323
'url' => $url,
24+
'memory' => memory_get_usage(),
2425
'statusCode' => $statusCode,
2526
'duration' => $duration,
2627
])."\n");

tests/CommandTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,30 @@ public function test_request()
5454
'method' => 'GET',
5555
'url' => 'http://127.0.0.1/welcome',
5656
'statusCode' => '200',
57-
'memory' => 23.43,
57+
'memory' => 17393560,
5858
'duration' => 10,
5959
]);
6060

6161
$command->requestInfo([
6262
'method' => 'POST',
6363
'url' => 'http://127.0.0.1:8080',
6464
'statusCode' => '404',
65-
'memory' => 26.43,
65+
'memory' => 20393560,
6666
'duration' => 1234,
6767
]);
6868

6969
$command->requestInfo([
7070
'method' => 'POST',
7171
'url' => 'http://127.0.0.1:8080/'.str_repeat('foo', 100),
7272
'statusCode' => 500,
73-
'memory' => 28.43,
73+
'memory' => 30393560,
7474
'duration' => 4567854,
7575
]);
7676

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

8282
EOF, $output->fetch());
8383
}

0 commit comments

Comments
 (0)