Created
March 6, 2020 09:55
-
-
Save Nex-Otaku/b7d013cdf6506b65cef9de4af2d61fe8 to your computer and use it in GitHub Desktop.
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\components\development\profiler; | |
class Profiler | |
{ | |
/** | |
* @var float[] | |
*/ | |
private $timers = []; | |
/** | |
* @param string $category | |
*/ | |
public function startTimer(string $category = 'default'): void | |
{ | |
$this->timers[$category] = $this->getMicrotime(); | |
} | |
/** | |
* @param string $category | |
*/ | |
public function finishTimer(string $category = 'default'): void | |
{ | |
$getTimestampsTime = $this->getMicrotime() - $this->timers[$category]; | |
$this->reportTime($category, $getTimestampsTime); | |
} | |
/** | |
* @param string $category | |
* @return float | |
*/ | |
public function getTimer(string $category = 'default'): float | |
{ | |
return $this->getMicrotime() - $this->timers[$category]; | |
} | |
/** | |
* @return float | |
*/ | |
private function getMicrotime(): float | |
{ | |
return microtime(true); | |
} | |
/** | |
* @param string $category | |
* @param float $time | |
*/ | |
private function reportTime(string $category, float $time): void | |
{ | |
$message = sprintf('%s: %.2f сек', $category, $time); | |
echo "{$message}\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment