Subversion Repositories Applications.framework

Rev

Rev 80 | Rev 105 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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