-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTimer.php
209 lines (166 loc) · 6 KB
/
Timer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<?php
/*******************************************************************************
* @name: Timer
* @note: Stopwatch in milliseconds script time+ the various steps in the code
* @author: Jgauthi, created at [28mars2007], url: <https://github.com/jgauthi/component_debug>
* @version: 2.1
*******************************************************************************/
namespace Jgauthi\Component\Debug;
class Timer
{
public const EXPORT_FORMAT_HTML = 'html';
public const EXPORT_FORMAT_COMMENT = 'commentaire';
protected float $startTime;
protected array $time = [];
protected array $chapterTimes = [];
protected string $result;
protected string $step;
protected string $chapter;
public function __construct(private bool $sendHeaderHttp = false)
{
$this->startTime = microtime(true);
}
static public function init(bool $header = false, string $format = self::EXPORT_FORMAT_HTML): self
{
$timer = new self($header);
$timer->shutdown($format);
return $timer;
}
//-- ETALONNER LE PARCOURS DU SCRIPT --------------
public function step(string $nom): self
{
$this->time[] = ['nom' => $nom, 'time' => microtime(true)];
return $this;
}
public function chapterStart(string $nom): self
{
$this->chapterTimes[$nom] = ['start' => microtime(true)];
return $this;
}
public function chapterEnd(string $nom): self
{
$this->chapterTimes[$nom]['end'] = microtime(true);
return $this;
}
//-- METTRE FIN AU COMPTEUR
public function stop(): self
{
$endTime = microtime(true);
// Vérifie si le header est correcte
if ($this->sendHeaderHttp && headers_sent()) {
$this->sendHeaderHttp = false;
}
// Analyse des données
$this->result = $this->_conv($endTime - $this->startTime);
// Analyse des étapes
if (count($this->time) > 0) {
$this->step = '';
foreach ($this->time as $id => $tmp) {
$calcul = $this->number_format($this->_conv($tmp['time'] - $this->startTime));
$this->step .= "- {$tmp['nom']}: $calcul ms".PHP_EOL;
if ($this->sendHeaderHttp) {
header("$id-Time: $calcul ms->{$tmp['nom']}");
}
}
}
// Analyse des chapitres
if (count($this->chapterTimes) > 0) {
$this->chapter = '';
foreach ($this->chapterTimes as $id => $temp) {
$calcul = $this->number_format($this->_conv($temp['end'] - $temp['start']));
$this->chapter .= "- {$id}: $calcul ms".PHP_EOL;
if ($this->sendHeaderHttp) {
header("{$id}-Chap: {$calcul} ms");
}
}
}
// Renvoi des données
if ($this->sendHeaderHttp) {
header('X-Time: '.$this->number_format($this->result).' ms');
}
return $this;
}
public function outPut(?string $type = null): string
{
$output = '';
// Organiser les données
if (count($this->time) > 0) {
$output .= 'Step:'.PHP_EOL.$this->step;
}
if (count($this->chapterTimes) > 0) {
$output .= 'Chapter:'.PHP_EOL.$this->chapter;
}
$output .= PHP_EOL.'=>Time: '.$this->number_format($this->result).' ms';
// Formater les données
if ($type === self::EXPORT_FORMAT_HTML) {
$output = '<h2>Script time:</h2>'.PHP_EOL.
str_replace(["\r\n", "\r", "\n"], '<br>', $output);
} elseif ($type === self::EXPORT_FORMAT_COMMENT) {
$output = '<!- TIME OF SCRIPT: '.PHP_EOL.$output.PHP_EOL.'-->';
}
return $output;
}
public function end(string $format = self::EXPORT_FORMAT_HTML): void
{
$this->stop();
echo $this->outPut($format);
}
public function shutdown(string $format = self::EXPORT_FORMAT_HTML): void
{
register_shutdown_function([$this, 'end'], $format);
}
//-- CONVERTISSEUR ---------------------------------
protected function _conv(float $time): string
{
return $time * 1000;
// return mb_substr($time * 1000, 0, 6);
// Récupère les 6 premiers chiffres
// Convertie en millisecondes
// Remplace . par , (écriture FR)
}
protected function number_format(float $chiffre): string
{
return number_format($chiffre, 2, ',', ' ');
}
//-- Outil de test/debug -------------------------------
public function testLoop(callable $function, int $nb_loop = 10, int $nb = 100, array $args = []): self
{
$chapitre = preg_replace('#[^a-z0-9-_]#i', '', mb_substr($function, 0, 50));
for ($loop = 0; $loop < $nb_loop; ++$loop) {
$this->chapterStart($chapitre.'#'.$loop);
for ($i = 0; $i < $nb; ++$i) {
call_user_func_array($function, $args);
}
$this->chapterEnd($chapitre.'#'.$loop);
}
return $this;
}
/**
* Method stand-alone
* Usage: \Jgauthi\Component\Timer::loopFunction(function() use ($var): void { somecode($var); } );
*/
public static function loopFunction(callable $closure, int $nbLoop = 100): float
{
$data = [];
for($i = 0; $i < $nbLoop; $i++) {
$time = microtime(true);
$closure();
$data[] = microtime(true) - $time;
}
return array_sum($data) / $nbLoop;
}
/**
* Exporte un chapitre au format CSV.
*/
public function exportChapter(string $filename, string $delimiter = ';'): void
{
// Analyse des chapitres
if (empty($this->chapterTimes)) {
return;
}
foreach ($this->chapterTimes as $id => $temp) {
$calcul = $this->number_format($this->_conv($temp['end'] - $temp['start']));
error_log("{$id}{$delimiter}{$calcul};ms".PHP_EOL, 3, $filename);
}
}
}