Subversion Repositories Applications.framework

Rev

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

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