Skip to content

Commit 9d62ba8

Browse files
authored
Merge pull request #1 from Button99/develop
Add Memory Usage class
2 parents d05e711 + a00cedf commit 9d62ba8

File tree

6 files changed

+188
-5
lines changed

6 files changed

+188
-5
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
composer.phar
22
/vendor/
3-
3+
.idea
44
# Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control
55
# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file
66
# composer.lock

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ Console::warning("Warning");
3232
Console::blue("This is a blue message");
3333
Console::magenta("This is a magenta message");
3434
Console::cyan("Cyan message for notification");
35+
Console::debug("Debug testing");
36+
Console::Notice("Notice testing");
3537
```
3638

3739
#### Progress Bar
@@ -65,6 +67,17 @@ Or withSteps method
6567
```php
6668
Spinner::withSteps(0, 10, 1);
6769
```
70+
#### MemoryUsage
71+
The `MemoryUsage` class tracks Memory consumption for executed functions.
72+
73+
Usage:
74+
```php
75+
$start = \Ckoumpis\PhpPrompt\MemoryUsage::getMemoryUsage();
76+
$arr = array_fill(0, 100000, 'test');
77+
$end = \Ckoumpis\PhpPrompt\MemoryUsage::getMemoryUsage();
78+
$memoryUsage = \Ckoumpis\PhpPrompt\MemoryUsage::showMemory($end - $start);
79+
```
80+
6881

6982
### Contributing
70-
We welcome contributions! If you find a bug or have a feature request, please open an issue or submit a pull request on Github.
83+
We welcome contributions! If you find a bug or have a feature request, please open an issue or submit a pull request on Github.

src/Console.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ class Console {
99
'blue' => '0;34',
1010
'magenta' => '0;35',
1111
'cyan' => '0;36',
12-
'white' => '1;37'
12+
'white' => '1;37',
13+
'grey' => '38;5;235'
1314
];
1415

1516
public static function log(string $message, string $color = 'white'): void {
16-
$colorCode = self::COLORS[$color]?? self::COLORS['white'];
17+
$colorCode = self::COLORS[$color] ?? self::COLORS['white'];
1718
echo "\033[" . $colorCode . "m" . $message . "\033[0m" . PHP_EOL;
1819
}
1920

@@ -29,6 +30,16 @@ public static function warning(string $message): void {
2930
self::log("Warning: " . $message, 'yellow');
3031
}
3132

33+
public static function debug(string $message): void {
34+
self::log("Debug: " . $message, 'grey');
35+
}
36+
37+
public static function notice(string $message): void
38+
{
39+
self::log('Notice: '. $message, 'cyan');
40+
}
41+
42+
3243
public static function blue(string $message): void {
3344
self::log("Blue: " . $message, 'blue');
3445
}
@@ -41,4 +52,4 @@ public static function cyan(string $message): void {
4152
self::log("Cyan: " . $message, 'cyan');
4253
}
4354
}
44-
?>
55+
?>

src/MemoryUsage.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Ckoumpis\PhpPrompt;
4+
5+
class MemoryUsage
6+
{
7+
8+
const BYTES_MB = 1024;
9+
const BYTES_KB = 1048576;
10+
11+
public static function getMemoryUsage(): float
12+
{
13+
return memory_get_usage();
14+
}
15+
16+
public static function getPeakMemoryUsage(): float
17+
{
18+
return memory_get_peak_usage();
19+
}
20+
21+
public static function showMemory(int $bytes): string
22+
{
23+
if ($bytes >= self::BYTES_MB) {
24+
return self::formatMemory($bytes, self::BYTES_MB, ' MB');
25+
} elseif ($bytes >= self::BYTES_KB) {
26+
return self::formatMemory($bytes, self::BYTES_KB, ' KB');
27+
} else {
28+
return $bytes . " Bytes";
29+
}
30+
}
31+
32+
protected static function formatMemory(int $bytes, int $unitSize, string $unit): string
33+
{
34+
return round($bytes / $unitSize, 2) . ' ' . $unit;
35+
}
36+
}

tests/ConsoleTest.php

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
<?php
2+
3+
4+
use Ckoumpis\PhpPrompt\Console;
5+
use PHPUnit\Framework\TestCase;
6+
7+
class ConsoleTest extends TestCase
8+
{
9+
10+
public function testWarning()
11+
{
12+
ob_start();
13+
Console::warning('Testing warning!');
14+
$output = ob_get_clean();
15+
echo $output;
16+
$this->assertStringContainsString("\033[1;33", $output);
17+
}
18+
19+
public function testCyan()
20+
{
21+
ob_start();
22+
Console::cyan('Testing Cyan!');
23+
$output = ob_get_clean();
24+
echo $output;
25+
$this->assertStringContainsString("\033[0;36", $output);
26+
27+
}
28+
29+
public function testLog()
30+
{
31+
ob_start();
32+
Console::log('Testing log!');
33+
$output = ob_get_clean();
34+
echo $output;
35+
$this->assertStringContainsString("\033[1;37", $output);
36+
}
37+
38+
public function testError()
39+
{
40+
ob_start();
41+
Console::error('Testing error!');
42+
$output = ob_get_clean();
43+
echo $output;
44+
$this->assertStringContainsString("\033[0;31", $output);
45+
}
46+
47+
public function testSuccess()
48+
{
49+
ob_start();
50+
Console::success('Testing success!');
51+
$output = ob_get_clean();
52+
echo $output;
53+
$this->assertStringContainsString("\033[0;32", $output);
54+
}
55+
56+
public function testMagenta()
57+
{
58+
ob_start();
59+
Console::magenta('Testing magenta!');
60+
$output = ob_get_clean();
61+
echo $output;
62+
$this->assertStringContainsString("\033[0;35", $output);
63+
64+
}
65+
66+
public function testBlue()
67+
{
68+
ob_start();
69+
Console::blue('Testing blue!');
70+
$output = ob_get_clean();
71+
echo $output;
72+
$this->assertStringContainsString("\033[0;34", $output);
73+
}
74+
75+
public function testDebug()
76+
{
77+
ob_start();
78+
Console::debug('Testing debug!');
79+
$output = ob_get_clean();
80+
echo $output;
81+
$this->assertStringContainsString("\033[38;5;235", $output);
82+
}
83+
84+
public function testNotice()
85+
{
86+
ob_start();
87+
Console::notice('Testing notice!');
88+
$output = ob_get_clean();
89+
echo $output;
90+
$this->assertStringContainsString("\033[0;36", $output);
91+
92+
}
93+
}

tests/MemUsageTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
5+
class MemUsageTest extends TestCase
6+
{
7+
public function testMemoryUsage(): void {
8+
ob_start();
9+
$start = \Ckoumpis\PhpPrompt\MemoryUsage::getMemoryUsage();
10+
$arr = array_fill(0, 100000, 'test');
11+
$end = \Ckoumpis\PhpPrompt\MemoryUsage::getMemoryUsage();
12+
$memoryUsage = \Ckoumpis\PhpPrompt\MemoryUsage::showMemory($end - $start);
13+
ob_end_clean();
14+
$this->assertGreaterThan(0, $end - $start, 'Memory usage is =<0');
15+
$this->assertIsString($memoryUsage, 'Memory usage is string!');
16+
}
17+
18+
public function testPeakMemoryUsage(): void {
19+
ob_start();
20+
$start = \Ckoumpis\PhpPrompt\MemoryUsage::getPeakMemoryUsage();
21+
$arr = array_fill(0, 100000, 'test');
22+
$end = \Ckoumpis\PhpPrompt\MemoryUsage::getPeakMemoryUsage();
23+
$memoryUsage = \Ckoumpis\PhpPrompt\MemoryUsage::showMemory($end - $start);
24+
echo $memoryUsage;
25+
$content = ob_get_contents();
26+
echo $content;
27+
ob_end_clean();
28+
$this->assertIsString($memoryUsage, 'Memory Peak usage is float!');
29+
}
30+
}

0 commit comments

Comments
 (0)