Subversion Repositories Applications.framework

Rev

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

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