Subversion Repositories Applications.framework

Rev

Rev 153 | Rev 217 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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