Passed
Push — main ( 93c2a6...737a66 )
by Dimitri
03:08
created

Iterator::getReport()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 37
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 13
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 37
ccs 0
cts 8
cp 0
crap 12
rs 9.8333
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);
0 ignored issues
show
Bug introduced by
The function number_to_size was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

112
            $memory = /** @scrutinizer ignore-call */ number_to_size($result['memory'], 4);
Loading history...
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