Last active
April 6, 2022 08:23
-
-
Save nasrulhazim/4c0e18a0fb03046ba64b73bc25771c88 to your computer and use it in GitHub Desktop.
Get Memory Utilisation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Actions; | |
class MemoryUtilisation | |
{ | |
public $units = ['B','KB','MB','GB','TB','PB']; | |
public $start; | |
public $startTime; | |
public $end; | |
public $endTime; | |
public function start() | |
{ | |
$this->start = memory_get_usage(); | |
$this->startTime = date('Y-m-d H:i:s'); | |
return $this; | |
} | |
public function end() | |
{ | |
$this->end = memory_get_usage(); | |
$this->endTime = date('Y-m-d H:i:s'); | |
return $this; | |
} | |
public function toArray() | |
{ | |
return [ | |
'start' => $this->toHuman($this->start), | |
'start_time' => $this->startTime, | |
'end' => $this->toHuman($this->end), | |
'end_time' => $this->endTime, | |
'total' => $this->toHuman($this->end - $this->start) | |
]; | |
} | |
private function toHuman($value) | |
{ | |
return @round( | |
$value / | |
pow( | |
1024, | |
($i = floor(log($value, 1024))) | |
), 2).' '.$this->units[$i]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$mem = new \App\Actions\MemoryUtilisation(); | |
$mem->start(); | |
// do your stuff... | |
echo 'blablablabla'; | |
echo 'do something else'; | |
$mem->end(); | |
// get status | |
$mem->toArray() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: