1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14:
15: class Chronometre {
16:
17: private static $instance = null;
18: private static $temps = array();
19:
20:
21: private function __construct() {
22: self::setTemps('depart', microtime());
23: }
24:
25: 26: 27: 28: 29: 30:
31: private static function getTemps($cle = null) {
32: $temps = '';
33: if (!is_null($cle)) {
34: $temps = self::$temps[$cle];
35: } else {
36: $temps = self::$temps;
37: }
38: return $temps;
39: }
40:
41: 42: 43: 44: 45: 46:
47: private static function setTemps($cle, $moment) {
48: array_push(self::$temps, array($cle => $moment));
49: }
50:
51:
52:
53: 54: 55: 56: 57: 58:
59: public static function chrono($cle) {
60: $moment = microtime();
61: self::verifierCreationInstance();
62: self::setTemps($cle, $moment);
63: }
64:
65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
83: public static function afficherChrono($indentation_origine = 8, $indentation = 4) {
84: self::verifierCreationInstance();
85:
86: self::setTemps('fin', microtime());
87:
88:
89: $sortie = str_repeat(' ', $indentation_origine) .
90: '<table id="chrono" lang="fr" summary="Résultat du
91: chronométrage du programme affichant la page actuelle.">' . "\n";
92: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
93: '<caption>Chronométrage</caption>' . "\n";
94: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
95: '<thead>' . "\n";
96: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
97: '<tr><th>Action</th><th>Temps écoulé (en s.)</th>
98: <th>Cumul du temps écoulé (en s.)</th></tr>' . "\n";
99: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
100: '</thead>' . "\n";
101:
102: $tbody = str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
103: '<tbody>' . "\n";
104:
105: $total_tps_ecoule = 0;
106:
107:
108: $tab_depart = self::getTemps(0);
109: list ($usec, $sec) = explode(' ', $tab_depart['depart']);
110:
111:
112: $tps_debut = ((float) $usec + (float) $sec);
113:
114: foreach (self::getTemps() as $tab_temps) {
115: foreach ($tab_temps as $cle => $valeur) {
116: list ($usec, $sec) = explode(' ', $valeur);
117: $tps_fin = ((float) $usec + (float) $sec);
118:
119: $tps_ecoule = abs($tps_fin - $tps_debut);
120: $total_tps_ecoule += $tps_ecoule;
121:
122: $tbody .= str_repeat(' ',
123: ($indentation_origine + ($indentation * 2))) .
124: '<tr>' .
125: '<th>' . $cle . '</th>' .
126: '<td>' . number_format($tps_ecoule, 3, ',', ' ') . '</td>' .
127: '<td>' . number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
128: '</tr>' . "\n";
129: $tps_debut = $tps_fin;
130: }
131: }
132: $tbody .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
133: '</tbody>' . "\n";
134:
135: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
136: '<tfoot>' . "\n";
137: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
138: '<tr>' .
139: '<th>' . 'Total du temps écoulé (en s.)' . '</th>' .
140: '<td colspan="2">' .
141: number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
142: '</tr>' . "\n";
143: $sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
144: '</tfoot>' . "\n";
145: $sortie .= $tbody;
146: $sortie .= str_repeat(' ', $indentation_origine) .
147: '</table>' . "\n";
148:
149: return $sortie;
150: }
151:
152: 153: 154:
155: private static function verifierCreationInstance() {
156: if (empty(self::$instance)) {
157: self::$instance = new Chronometre();
158: }
159: }
160: }
161: ?>