|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Blitz PHP framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) 2022 Dimitri Sitchet Tomkeu <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view |
|
9
|
|
|
* the LICENSE file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace BlitzPHP\Debug; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Iterateur pour le debuggage. |
|
18
|
|
|
*/ |
|
19
|
|
|
class Iterator |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* Stocke les tests que nous devons exécuter. |
|
23
|
|
|
* |
|
24
|
|
|
* @var array<string, Closure> |
|
25
|
|
|
*/ |
|
26
|
|
|
protected array $tests = []; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Stocke les résultats de chacun des tests. |
|
30
|
|
|
* |
|
31
|
|
|
* @var array<string, array> |
|
32
|
|
|
*/ |
|
33
|
|
|
protected array $results = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Ajoute un test à exécuter. |
|
37
|
|
|
* |
|
38
|
|
|
* Les tests sont simplement des fermetures permettant à l'utilisateur de définir n'importe quelle séquence de choses qui se produiront pendant le test. |
|
39
|
|
|
*/ |
|
40
|
|
|
public function add(string $name, Closure $closure): self |
|
41
|
|
|
{ |
|
42
|
|
|
$name = strtolower($name); |
|
43
|
|
|
|
|
44
|
|
|
$this->tests[$name] = $closure; |
|
45
|
|
|
|
|
46
|
|
|
return $this; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Exécute tous les tests qui ont été ajoutés, en enregistrant le temps nécessaire pour exécuter |
|
51
|
|
|
* le nombre d'itérations souhaité et l'utilisation approximative de la mémoire utilisée au cours |
|
52
|
|
|
* de ces itérations. |
|
53
|
|
|
*/ |
|
54
|
|
|
public function run(int $iterations = 1000, bool $output = true): ?string |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($this->tests as $name => $test) { |
|
57
|
|
|
// clear memory before start |
|
58
|
|
|
gc_collect_cycles(); |
|
59
|
|
|
|
|
60
|
|
|
$start = microtime(true); |
|
61
|
|
|
$startMem = $maxMemory = memory_get_usage(true); |
|
62
|
|
|
|
|
63
|
|
|
for ($i = 0; $i < $iterations; $i++) { |
|
64
|
|
|
$result = $test(); |
|
65
|
|
|
$maxMemory = max($maxMemory, memory_get_usage(true)); |
|
66
|
|
|
|
|
67
|
|
|
unset($result); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$this->results[$name] = [ |
|
71
|
|
|
'time' => microtime(true) - $start, |
|
72
|
|
|
'memory' => $maxMemory - $startMem, |
|
73
|
|
|
'n' => $iterations, |
|
74
|
|
|
]; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
if ($output) { |
|
78
|
|
|
return $this->getReport(); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Recupere le resultat du test |
|
86
|
|
|
*/ |
|
87
|
|
|
public function getReport(): string |
|
88
|
|
|
{ |
|
89
|
|
|
if ($this->results === []) { |
|
90
|
|
|
return 'No results to display.'; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
helper('number'); |
|
94
|
|
|
|
|
95
|
|
|
// Template |
|
96
|
|
|
$tpl = '<table> |
|
97
|
|
|
<thead> |
|
98
|
|
|
<tr> |
|
99
|
|
|
<td>Test</td> |
|
100
|
|
|
<td>Time</td> |
|
101
|
|
|
<td>Memory</td> |
|
102
|
|
|
</tr> |
|
103
|
|
|
</thead> |
|
104
|
|
|
<tbody> |
|
105
|
|
|
{rows} |
|
106
|
|
|
</tbody> |
|
107
|
|
|
</table>'; |
|
108
|
|
|
|
|
109
|
|
|
$rows = ''; |
|
110
|
|
|
|
|
111
|
|
|
foreach ($this->results as $name => $result) { |
|
112
|
|
|
$memory = number_to_size($result['memory'], 4); |
|
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
$rows .= "<tr> |
|
115
|
|
|
<td>{$name}</td> |
|
116
|
|
|
<td>" . number_format($result['time'], 4) . "</td> |
|
117
|
|
|
<td>{$memory}</td> |
|
118
|
|
|
</tr>"; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$tpl = str_replace('{rows}', $rows, $tpl); |
|
122
|
|
|
|
|
123
|
|
|
return $tpl . '<br/>'; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|