Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
CRAP | |
100.00% |
21 / 21 |
Benchmark | |
100.00% |
1 / 1 |
|
100.00% |
5 / 5 |
8 | |
100.00% |
21 / 21 |
__construct | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
reset | |
100.00% |
1 / 1 |
1 | |
100.00% |
3 / 3 |
|||
getMicroTime | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
|||
getTime | |
100.00% |
1 / 1 |
4 | |
100.00% |
13 / 13 |
|||
getMemory | |
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
<?php | |
namespace Win\Services; | |
/** | |
* Contador de Desempenho | |
* | |
* Usado para analisar desempenho de códigos | |
* | |
* <code> | |
* $b = new Benchmark(); | |
* [code here] | |
* echo $b->getTime(); | |
* </code> | |
*/ | |
class Benchmark | |
{ | |
private int $startTime; | |
private int $memory; | |
/** | |
* Inicia a contagem de tempo | |
*/ | |
public function __construct() | |
{ | |
$this->startTime = microtime(true); | |
$this->memory = memory_get_usage(); | |
} | |
/** | |
* Reinicia a contagem de tempo | |
*/ | |
public function reset() | |
{ | |
$this->startTime = microtime(true); | |
$this->memory = memory_get_usage(); | |
} | |
/** | |
* Retorna o tempo gasto (em microssegundos) | |
* @return float | |
*/ | |
private function getMicroTime() | |
{ | |
return microtime(true) - $this->startTime; | |
} | |
/** | |
* Retorna o tempo gasto (na unidade correta) | |
* @return string | |
*/ | |
public function getTime() | |
{ | |
$sec = $this->getMicroTime(); | |
$day = floor($sec / 86400); | |
$sec -= $day * 86400; | |
$hour = floor($sec / 3600); | |
$sec -= $hour * 3600; | |
$minute = floor($sec / 60); | |
$sec -= $minute * 60; | |
$microSec = ($sec - floor($sec)) * 1000; | |
return | |
(empty($day) ? '' : $day . 'd ') . | |
(empty($hour) ? '' : $hour . 'h ') . | |
(empty($minute) ? '' : $minute . 'm ') . | |
floor($sec) . 's ' . | |
$microSec . 'ms'; | |
} | |
/** | |
* Retorna a quantidade de memória gasta | |
* @return int | |
*/ | |
public function getMemory() | |
{ | |
return (memory_get_usage() - $this->memory) / (1024 * 1024); | |
} | |
} |