Subversion Repositories Applications.gtt

Rev

Rev 119 | Go to most recent revision | Details | 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
82
	public function getTempsSql() {
83
		return $this->temps_sql;
84
	}
85
	public function setTempsSql($tps_deb, $tps_fin = null) {
86
		if (is_null($tps_fin)) {
87
			if (is_float($tps_deb)) {
88
				$this->temps_sql += $tps_deb;
89
			}
90
		} else {
91
			if (is_float($tps_deb) && is_float($tps_fin)) {
92
				$this->temps_sql += $tps_fin - $tps_deb;
93
			}
94
		}
95
	}
96
 
97
	/*** Méthodes : ***/
98
 
99
	/**Méthode afficherChrono() - Permet d'afficher les temps d'éxécution de différentes parties d'un script.
100
	*
101
	* Cette fonction permet d'afficher un ensemble de mesure de temps prises à différents endroits d'un script.
102
	* Ces mesures sont affichées au sein d'un tableau XHTML dont on peut controler l'indentation des balises.
103
	* Pour un site en production, il suffit d'ajouter un style #chrono {display:none;} dans la css. De cette façon,
104
	* le tableau ne s'affichera pas. Le webmaster lui pourra rajouter sa propre feuille de style affichant le tableau.
105
	* Le développeur initial de cette fonction est Loic d'Anterroches. Elle a été modifiée par Jean-Pascal Milcent.
106
	* Elle utilise une variable gobale : $_CHRONO_
107
	*
108
	* @author   Loic d'Anterroches
109
	* @author        Jean-Pascal MILCENT <jpm@tela-botanica.org>
110
	* @param    int     l'indentation de base pour le code html du tableau.
111
	* @param    int     le pas d'indentation pour le code html du tableau.
112
	* @return   string  la chaine XHTML de mesure des temps.
113
	*/
114
	function afficherChrono($indentation_origine = 8, $indentation = 4)	{
115
		// Création du chrono de fin
116
		$GLOBALS['_EFLORE_']['chrono']->setTemps(array('fin' => microtime()));
117
 
118
		// Début création de l'affichage
119
		$sortie = str_repeat(' ', $indentation_origine).
120
		    		'<table id="chrono" lang="fr" summary="Résultat du chronométrage du programme affichant la page actuelle.">'."\n";
121
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
122
		        '<caption>Chronométrage</caption>'."\n";
123
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
124
		        '<thead>'."\n";
125
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))).
126
		            '<tr><th>Action</th><th>Temps écoulé (en s.)</th><th>Cumul du temps écoulé (en s.)</th></tr>'."\n";
127
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
128
		        '</thead>'."\n";
129
 
130
		$tbody = str_repeat(' ', ($indentation_origine + ($indentation * 1))).
131
		        '<tbody>'."\n";
132
 
133
		$total_tps_ecoule = 0;
134
 
135
		// Récupération de la première mesure
136
		$tab_depart =& $this->getTemps(0);
137
		list($usec, $sec) = explode(' ', $tab_depart['depart']);
138
 
139
		// Ce temps correspond à tps_fin
140
		$tps_debut = ((float)$usec + (float)$sec);
141
 
142
		foreach ($this->getTemps() as $tab_temps) {
143
			foreach ($tab_temps as $cle => $valeur) {
144
				list($usec, $sec) = explode(' ', $valeur);
145
				$tps_fin = ((float)$usec + (float)$sec);
146
 
147
				$tps_ecoule = abs($tps_fin - $tps_debut);
148
				$total_tps_ecoule += $tps_ecoule;
149
 
150
				$tbody .=   str_repeat(' ', ($indentation_origine + ($indentation * 2))).
151
				        '<tr>'.
152
				            '<th>'.$cle.'</th>'.
153
				            '<td>'.number_format($tps_ecoule, 3, ',', ' ').'</td>'.
154
				            '<td>'.number_format($total_tps_ecoule, 3, ',', ' ').'</td>'.
155
				        '</tr>'."\n";
156
				$tps_debut = $tps_fin;
157
			}
158
		}
159
		$tbody .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
160
		        '</tbody>'."\n";
161
 
162
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
163
		        '<tfoot>'."\n";
164
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 2))).
165
		            '<tr>'.
166
		                '<th>'.'Total du temps écoulé (en s.)'.'</th>'.
167
		                '<td colspan="2">'.number_format($total_tps_ecoule,3, ',', ' ').'</td>'.
168
		            '</tr>'."\n";
169
		$sortie .= str_repeat(' ', ($indentation_origine + ($indentation * 1))).
170
		        '</tfoot>'."\n";
171
		$sortie .= $tbody;
172
		$sortie .= str_repeat(' ', $indentation_origine).
173
		    '</table>'."\n";
174
 
175
		return $sortie;
176
	}
177
 
178
}
179
 
180
/* +--Fin du code ----------------------------------------------------------------------------------------+
181
*
182
* $Log$
183
*
184
* +-- Fin du code ----------------------------------------------------------------------------------------+
185
*/
186
?>