Subversion Repositories Applications.gtt

Rev

Rev 119 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
118 jpm 1
<?php
2
// +------------------------------------------------------------------------------------------------------+
3
// | PHP version 5.0.4                                                                                    |
4
// +------------------------------------------------------------------------------------------------------+
5
// | Copyright (C) 2005 Tela Botanica (accueil@tela-botanica.org)                                         |
6
// +------------------------------------------------------------------------------------------------------+
7
// | This file is part of eFlore-Debogage.                                                                |
8
// |                                                                                                      |
9
// | Foobar is free software; you can redistribute it and/or modify                                       |
10
// | it under the terms of the GNU General Public License as published by                                 |
11
// | the Free Software Foundation; either version 2 of the License, or                                    |
12
// | (at your option) any later version.                                                                  |
13
// |                                                                                                      |
14
// | Foobar is distributed in the hope that it will be useful,                                            |
15
// | but WITHOUT ANY WARRANTY; without even the implied warranty of                                       |
16
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                        |
17
// | GNU General Public License for more details.                                                         |
18
// |                                                                                                      |
19
// | You should have received a copy of the GNU General Public License                                    |
20
// | along with Foobar; if not, write to the Free Software                                                |
21
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                            |
22
// +------------------------------------------------------------------------------------------------------+
23
// CVS : $Id: Chronometre.class.php,v 1.1 2007-01-12 13:16:09 jp_milcent Exp $
24
/**
25
* Classe permettant de mesurer le temps d'execution d'un script.
26
*
27
* Contient des méthodes permettant d'évaluer la vitesse d'exécution d'un script.
28
*
29
*@package eFlore
30
*@subpackage Debogage
31
//Auteur original :
32
*@author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
33
//Autres auteurs :
34
*@author        aucun
35
*@copyright     Tela-Botanica 2000-2005
36
*@version       $Revision: 1.1 $ $Date: 2007-01-12 13:16:09 $
37
// +------------------------------------------------------------------------------------------------------+
38
*/
39
 
40
// +------------------------------------------------------------------------------------------------------+
41
// |                                            ENTETE du PROGRAMME                                       |
42
// +------------------------------------------------------------------------------------------------------+
43
 
44
 
45
// +------------------------------------------------------------------------------------------------------+
46
// |                                            CORPS du PROGRAMME                                        |
47
// +------------------------------------------------------------------------------------------------------+
48
/**Classe Chronometre() - Permet de stocker et d'afficher les temps d'éxécution de script.
49
*
50
* Cette classe permet de réaliser un ensemble de mesure de temps prises à
51
* différents endroits d'un script. Ces mesures peuvent ensuite être affichées au
52
* sein d'un tableau XHTML.
53
*
54
* @author	Jean-Pascal MILCENT <jpm@tela-botanica.org>
55
*/
56
class Chronometre
57
{
58
	/*** Attributs : ***/
59
	private $temps = array();
60
	private $temps_sql = 0;
61
 
62
	/*** Constructeur : ***/
63
	public function __construct() {
64
		$this->setTemps(array('depart' => microtime()));
65
	}
66
 
67
	/*** Accesseurs : ***/
68
 
69
	// Temps
70
	public function getTemps($cle = NULL) {
71
		if (!is_null($cle)) {
72
			return $this->temps[$cle];
73
		} else {
74
			return $this->temps;
75
		}
76
	}
77
	public function setTemps($moment = array()) {
78
		array_push($this->temps, $moment);
79
	}
80
 
81
	// Temps Sql
119 jpm 82
	// Notes : utiliser microtime(true) pour renvoyer un float
118 jpm 83
	public function getTempsSql() {
84
		return $this->temps_sql;
85
	}
86
	public function setTempsSql($tps_deb, $tps_fin = null) {
87
		if (is_null($tps_fin)) {
88
			if (is_float($tps_deb)) {
89
				$this->temps_sql += $tps_deb;
90
			}
91
		} else {
92
			if (is_float($tps_deb) && is_float($tps_fin)) {
93
				$this->temps_sql += $tps_fin - $tps_deb;
94
			}
95
		}
96
	}
97
 
98
	/*** Méthodes : ***/
99
 
100
	/**Méthode afficherChrono() - Permet d'afficher les temps d'éxécution de différentes parties d'un script.
101
	*
102
	* Cette fonction permet d'afficher un ensemble de mesure de temps prises à différents endroits d'un script.
103
	* Ces mesures sont affichées au sein d'un tableau XHTML dont on peut controler l'indentation des balises.
104
	* Pour un site en production, il suffit d'ajouter un style #chrono {display:none;} dans la css. De cette façon,
105
	* le tableau ne s'affichera pas. Le webmaster lui pourra rajouter sa propre feuille de style affichant le tableau.
106
	* Le développeur initial de cette fonction est Loic d'Anterroches. Elle a été modifiée par Jean-Pascal Milcent.
107
	* Elle utilise une variable gobale : $_CHRONO_
108
	*
109
	* @author   Loic d'Anterroches
110
	* @author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
111
	* @param    int     l'indentation de base pour le code html du tableau.
112
	* @param    int     le pas d'indentation pour le code html du tableau.
113
	* @return   string  la chaine XHTML de mesure des temps.
114
	*/
115
	function afficherChrono($indentation_origine = 8, $indentation = 4)	{
116
		// Création du chrono de fin
117
		$GLOBALS['_EFLORE_']['chrono']->setTemps(array('fin' => microtime()));
118
 
119
		// Début création de l'affichage
120
		$sortie = str_repeat(' ', $indentation_origine).
121
		    		'<table id="chrono" lang="fr" summary="Résultat du chronométrage du programme affichant la page actuelle.">'."\n";
122
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
123
		        '<caption>Chronométrage</caption>'."\n";
124
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
125
		        '<thead>'."\n";
126
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))).
127
		            '<tr><th>Action</th><th>Temps écoulé (en s.)</th><th>Cumul du temps écoulé (en s.)</th></tr>'."\n";
128
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
129
		        '</thead>'."\n";
130
 
131
		$tbody = str_repeat(' ', ($indentation_origine + ($indentation * 1))).
132
		        '<tbody>'."\n";
133
 
134
		$total_tps_ecoule = 0;
135
 
136
		// Récupération de la première mesure
137
		$tab_depart =& $this->getTemps(0);
138
		list($usec, $sec) = explode(' ', $tab_depart['depart']);
139
 
140
		// Ce temps correspond à tps_fin
141
		$tps_debut = ((float)$usec + (float)$sec);
142
 
143
		foreach ($this->getTemps() as $tab_temps) {
144
			foreach ($tab_temps as $cle => $valeur) {
145
				list($usec, $sec) = explode(' ', $valeur);
146
				$tps_fin = ((float)$usec + (float)$sec);
147
 
148
				$tps_ecoule = abs($tps_fin - $tps_debut);
149
				$total_tps_ecoule += $tps_ecoule;
150
 
151
				$tbody .=   str_repeat(' ', ($indentation_origine + ($indentation * 2))).
152
				        '<tr>'.
153
				            '<th>'.$cle.'</th>'.
154
				            '<td>'.number_format($tps_ecoule, 3, ',', ' ').'</td>'.
155
				            '<td>'.number_format($total_tps_ecoule, 3, ',', ' ').'</td>'.
156
				        '</tr>'."\n";
157
				$tps_debut = $tps_fin;
158
			}
159
		}
160
		$tbody .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
161
		        '</tbody>'."\n";
162
 
163
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
164
		        '<tfoot>'."\n";
165
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))).
166
		            '<tr>'.
167
		                '<th>'.'Total du temps écoulé (en s.)'.'</th>'.
168
		                '<td colspan="2">'.number_format($total_tps_ecoule,3, ',', ' ').'</td>'.
169
		            '</tr>'."\n";
170
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
171
		        '</tfoot>'."\n";
172
		$sortie .= $tbody;
173
		$sortie .= str_repeat(' ', $indentation_origine).
174
		    '</table>'."\n";
175
 
176
		return $sortie;
177
	}
178
 
179
}
180
 
181
/* +--Fin du code ----------------------------------------------------------------------------------------+
182
*
183
* $Log$
184
*
185
* +-- Fin du code ----------------------------------------------------------------------------------------+
186
*/
187
?>