Passed
Push — master ( b9a31e...494505 )
by Wanderson
02:11
created

Benchmark   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 62
rs 10
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A reset() 0 4 1
A getTime() 0 17 4
A getMemory() 0 3 1
A getMicroTime() 0 3 1
1
<?php
2
3
namespace Win\Common;
4
5
/**
6
 * Contador de Desempenho
7
 *
8
 * Usado para analisar desempenho de códigos códigos
9
 *
10
 * <code>
11
 * $b = new Benchmark();
12
 * [code here]
13
 * echo $b->getTime();
14
 * </code>
15
 */
16
class Benchmark
17
{
18
	private $startTime;
19
	private $memory;
20
21
	/**
22
	 * Inicia a contagem de tempo
23
	 */
24
	public function __construct()
25
	{
26
		$this->startTime = microtime(true);
27
		$this->memory = memory_get_usage();
28
	}
29
30
	/**
31
	 * Reinicia a contagem de tempo
32
	 */
33
	public function reset()
34
	{
35
		$this->startTime = microtime(true);
36
		$this->memory = memory_get_usage();
37
	}
38
39
	/**
40
	 * Retorna o tempo gasto (em microssegundos)
41
	 * @return float
42
	 */
43
	private function getMicroTime()
44
	{
45
		return microtime(true) - $this->startTime;
46
	}
47
48
	/**
49
	 * Retorna o tempo gasto (na unidade correta)
50
	 * @return string
51
	 */
52
	public function getTime()
53
	{
54
		$sec = $this->getMicroTime();
55
		$day = floor($sec / 86400);
56
		$sec -= $day * 86400;
57
		$hour = floor($sec / 3600);
58
		$sec -= $hour * 3600;
59
		$minute = floor($sec / 60);
60
		$sec -= $minute * 60;
61
		$microSec = ($sec - floor($sec)) * 1000;
62
63
		return
64
				(empty($day) ? '' : $day . 'd ') .
65
				(empty($hour) ? '' : $hour . 'h ') .
66
				(empty($minute) ? '' : $minute . 'm ') .
67
				floor($sec) . 's ' .
68
				$microSec . 'ms';
69
	}
70
71
	/**
72
	 * Retorna a quantidade de memória gasta
73
	 * @return int
74
	 */
75
	public function getMemory()
76
	{
77
		return (memory_get_usage() - $this->memory) / (1024 * 1024);
78
	}
79
}
80