Subversion Repositories Applications.framework

Rev

Rev 467 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 467 Rev 476
1
<?php
1
<?php
2
// declare(encoding='UTF-8');
2
// declare(encoding='UTF-8');
3
/** Classe Chronometre() - Permet de stocker et d'afficher
3
/** Classe Chronometre() - Permet de stocker et d'afficher les temps d'éxécution de script.
4
 * les temps d'éxécution de script.
-
 
5
 *
4
 *
6
 * Cette classe permet de réaliser un ensemble
5
 * Cette classe permet de réaliser un ensemble de mesure de temps prises à différents endroits d'un script.
7
 * de mesure de temps prises à
-
 
8
 * différents endroits d'un script.
-
 
9
 * Ces mesures peuvent ensuite être affichées au
6
 * Ces mesures peuvent ensuite être affichées au sein d'un tableau XHTML.
10
 * sein d'un tableau XHTML.
-
 
11
 *
7
 *
12
 *
-
 
13
 * PHP Version 5
-
 
14
 *
-
 
15
 * @category  PHP
8
 * @category	PHP 5.2
16
 * @package   Framework
9
 * @package		Framework
17
 * @author	Jean-Pascal MILCENT <jpm@tela-botanica.org>
10
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
18
 * @copyright 2009 Tela-Botanica
11
 * @copyright	Copyright (c) 2009, Tela Botanica (accueil@tela-botanica.org)
19
 * @license   http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
12
 * @license		GNU-GPL-v3 <http://www.gnu.org/licenses/gpl.html>
20
 * @version   Release: <package_version>
13
 * @license		CECILL-v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt>
21
 * @link	  /doc/framework/
-
 
22
 */
14
 */
23
class Chronometre {
15
class Chronometre {
24
	/*** Attributs : ***/
16
	/*** Attributs : ***/
-
 
17
	private static $instance = null;
25
	private $_temps = array();
18
	private static $temps = array();
26
 
19
 
27
	/** Constructeur : **/
20
	/** Constructeur : **/
28
	public function __construct() {
21
	private function __construct() {
29
		$this->setTemps(array('depart' => microtime()));
22
		self::setTemps('depart', microtime());
30
	}
23
	}
31
 
24
 
32
	/** Accesseurs :
25
	/** Accesseurs :
33
	 *
26
	 *
34
	 * @param string $cle la cle associée à un chronomètre particulier
27
	 * @param string $cle la cle associée à un chronomètre particulier
35
	 *
28
	 *
36
	 * @return int le temps écoulé
29
	 * @return int le temps écoulé
37
	 */
30
	 */
38
	public function getTemps($cle = null) {
31
	private static function getTemps($cle = null) {
39
		$temps = '';
32
		$temps = '';
40
		if (!is_null($cle)) {
33
		if (!is_null($cle)) {
41
			$temps = $this->_temps[$cle];
34
			$temps = self::$temps[$cle];
42
		} else {
35
		} else {
43
			$temps = $this->_temps;
36
			$temps = self::$temps;
44
		}
37
		}
45
		return $temps;
38
		return $temps;
46
	}
39
	}
47
 
40
 
48
	/** Setteur pour la variable temps
41
	/** Setteur pour la variable temps
49
	 *
42
	 *
50
	 * @param array() $moment ajoute des points de chronométrage au tableau _temps
43
	 * @param array() $moment ajoute des points de chronométrage au tableau _temps
51
	 *
44
	 *
52
	 * @return null
45
	 * @return null
53
	 */
46
	 */
54
	public function setTemps($moment = array ()) {
47
	private static function setTemps($cle, $moment) {
55
		array_push($this->_temps, $moment);
48
		array_push(self::$temps, array($cle => $moment));
56
	}
49
	}
57
 
50
 
58
	/*** Méthodes : ***/
51
	/*** Méthodes : ***/
-
 
52
 
-
 
53
	/**
-
 
54
	 *	Effectue un chronometrage.
-
 
55
	 *
-
 
56
	 * @param string le nom du point de chronométrage
-
 
57
	 * @return null
59
 
58
	 */
-
 
59
	public static function chrono($cle) {
-
 
60
		$moment = microtime();
-
 
61
		self::verifierCreationInstance();
-
 
62
		self::setTemps($cle, $moment);
-
 
63
	}
-
 
64
 
60
	/** Méthode afficherChrono() -
65
	/**
61
	* Permet d'afficher les temps d'éxécution de différentes parties d'un script.
66
	* Permet d'afficher les temps d'éxécution de différentes parties d'un script.
62
	*
-
 
63
	* Cette fonction permet d'afficher un ensemble de
67
	*
64
	* mesure de temps prises à différents endroits d'un script.
68
	* Cette fonction permet d'afficher un ensemble de  mesure de temps prises à différents endroits d'un script.
65
	* Ces mesures sont affichées au sein d'un tableau XHTML
-
 
66
	* dont on peut controler l'indentation des balises.
69
	* Ces mesures sont affichées au sein d'un tableau XHTML dont on peut controler l'indentation des balises.
67
	* Pour un site en production, il suffit d'ajouter un style
-
 
68
	* #chrono {display:none;} dans la css. De cette façon,
70
	* Pour un site en production, il suffit d'ajouter un style #chrono {display:none;} dans la css.
69
	* le tableau ne s'affichera pas. Le webmaster lui pourra
71
	* De cette façon, le tableau ne s'affichera pas. Le webmaster lui pourra rajouter sa propre feuille de style
70
	* rajouter sa propre feuille de style affichant le tableau.
72
	* affichant le tableau.
71
	* Le développeur initial de cette fonction est Loic d'Anterroches.
73
	* Le développeur initial de cette fonction est Loic d'Anterroches.
72
	* Elle a été modifiée par Jean-Pascal Milcent.
-
 
73
	* Elle utilise une variable gobale : $_CHRONO_
74
	* Elle a été modifiée par Jean-Pascal Milcent.
74
	*
75
	*
75
	* @author   Loic d'Anterroches
76
	* @author   Loic d'Anterroches
76
	* @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
77
	* @author	Jean-Pascal MILCENT <jpm@tela-botanica.org>
77
	*
78
	*
78
	* @param	int	 $indentation_origine l'indentation de base.
79
	* @param	int	 $indentation_origine l'indentation de base.
79
	* @param	int	 $indentation le pas d'indentation.
80
	* @param	int	 $indentation le pas d'indentation.
80
	* @return   string  la chaine XHTML de mesure des temps.
81
	* @return   string  la chaine XHTML de mesure des temps.
81
	*/
82
	*/
82
	public function afficherChrono($indentation_origine = 8, $indentation = 4) {
83
	public static function afficherChrono($indentation_origine = 8, $indentation = 4) {
-
 
84
		self::verifierCreationInstance();
83
		// Création du chrono de fin
85
		// Création du chrono de fin
84
		$GLOBALS['_SCRIPT_']['chrono']->setTemps(array (
-
 
85
			'fin' => microtime()
86
		self::setTemps('fin', microtime());
86
		));
-
 
87
 
87
 
88
		// Début création de l'affichage
88
		// Début création de l'affichage
89
		$sortie = str_repeat(' ', $indentation_origine) .
89
		$sortie = str_repeat(' ', $indentation_origine) .
90
		'<table id="chrono" lang="fr" summary="Résultat du
90
		'<table id="chrono" lang="fr" summary="Résultat du
91
			chronométrage du programme affichant la page actuelle.">' . "\n";
91
			chronométrage du programme affichant la page actuelle.">' . "\n";
92
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
92
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
93
		'<caption>Chronométrage</caption>' . "\n";
93
		'<caption>Chronométrage</caption>' . "\n";
94
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
94
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
95
		'<thead>' . "\n";
95
		'<thead>' . "\n";
96
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
96
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
97
		'<tr><th>Action</th><th>Temps écoulé (en s.)</th>
97
		'<tr><th>Action</th><th>Temps écoulé (en s.)</th>
98
		<th>Cumul du temps écoulé (en s.)</th></tr>' . "\n";
98
		<th>Cumul du temps écoulé (en s.)</th></tr>' . "\n";
99
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
99
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
100
		'</thead>' . "\n";
100
		'</thead>' . "\n";
101
 
101
 
102
		$tbody = str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
102
		$tbody = str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
103
		'<tbody>' . "\n";
103
		'<tbody>' . "\n";
104
 
104
 
105
		$total_tps_ecoule = 0;
105
		$total_tps_ecoule = 0;
106
 
106
 
107
		// Récupération de la premiére mesure
107
		// Récupération de la premiére mesure
108
		$tab_depart = & $this->getTemps(0);
108
		$tab_depart = self::getTemps(0);
109
		list ($usec, $sec) = explode(' ', $tab_depart['depart']);
109
		list ($usec, $sec) = explode(' ', $tab_depart['depart']);
110
 
110
 
111
		// Ce temps correspond à tps_fin
111
		// Ce temps correspond à tps_fin
112
		$tps_debut = ((float) $usec + (float) $sec);
112
		$tps_debut = ((float) $usec + (float) $sec);
113
 
113
 
114
		foreach ($this->getTemps() as $tab_temps) {
114
		foreach (self::getTemps() as $tab_temps) {
115
			foreach ($tab_temps as $cle => $valeur) {
115
			foreach ($tab_temps as $cle => $valeur) {
116
				list ($usec, $sec) = explode(' ', $valeur);
116
				list ($usec, $sec) = explode(' ', $valeur);
117
				$tps_fin = ((float) $usec + (float) $sec);
117
				$tps_fin = ((float) $usec + (float) $sec);
118
 
118
 
119
				$tps_ecoule = abs($tps_fin - $tps_debut);
119
				$tps_ecoule = abs($tps_fin - $tps_debut);
120
				$total_tps_ecoule += $tps_ecoule;
120
				$total_tps_ecoule += $tps_ecoule;
121
 
121
 
122
				$tbody .= str_repeat(' ',
122
				$tbody .= str_repeat(' ',
123
								($indentation_origine + ($indentation * 2))) .
123
								($indentation_origine + ($indentation * 2))) .
124
				'<tr>' .
124
				'<tr>' .
125
				'<th>' . $cle . '</th>' .
125
				'<th>' . $cle . '</th>' .
126
				'<td>' . number_format($tps_ecoule, 3, ',', ' ') . '</td>' .
126
				'<td>' . number_format($tps_ecoule, 3, ',', ' ') . '</td>' .
127
				'<td>' . number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
127
				'<td>' . number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
128
				'</tr>' . "\n";
128
				'</tr>' . "\n";
129
				$tps_debut = $tps_fin;
129
				$tps_debut = $tps_fin;
130
			}
130
			}
131
		}
131
		}
132
		$tbody .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
132
		$tbody .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
133
		'</tbody>' . "\n";
133
		'</tbody>' . "\n";
134
 
134
 
135
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
135
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
136
		'<tfoot>' . "\n";
136
		'<tfoot>' . "\n";
137
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
137
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))) .
138
		'<tr>' .
138
		'<tr>' .
139
		'<th>' . 'Total du temps écoulé (en s.)' . '</th>' .
139
		'<th>' . 'Total du temps écoulé (en s.)' . '</th>' .
140
		'<td colspan="2">' .
140
		'<td colspan="2">' .
141
			number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
141
			number_format($total_tps_ecoule, 3, ',', ' ') . '</td>' .
142
		'</tr>' . "\n";
142
		'</tr>' . "\n";
143
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
143
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))) .
144
		'</tfoot>' . "\n";
144
		'</tfoot>' . "\n";
145
		$sortie .= $tbody;
145
		$sortie .= $tbody;
146
		$sortie .= str_repeat(' ', $indentation_origine) .
146
		$sortie .= str_repeat(' ', $indentation_origine) .
147
		'</table>' . "\n";
147
		'</table>' . "\n";
148
 
148
 
149
		return $sortie;
149
		return $sortie;
150
	}
150
	}
-
 
151
 
-
 
152
	/**
-
 
153
	 * Vérifie si l'instance de classe à été crée, si non la crée
-
 
154
	 */
-
 
155
	private static function verifierCreationInstance() {
-
 
156
		if (empty(self::$instance)) {
-
 
157
			self::$instance = new Chronometre();
-
 
158
		}
-
 
159
	}
151
}
160
}
152
?>
161
?>