Subversion Repositories Applications.framework

Rev

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

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