IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Récupérer le temps de génération d'une page?


Sujet :

Langage PHP

  1. #1
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut Récupérer le temps de génération d'une page?
    Salut a tous,

    pour optimiser mon code j'aimerais savoir comment doit-on procéder pour pouvoir afficher le temps de génération d'une page?
    manganimes (en construction) -
    zemanga

  2. #2
    Expert éminent sénior
    Avatar de sinok
    Profil pro
    Inscrit en
    Août 2004
    Messages
    8 765
    Détails du profil
    Informations personnelles :
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations forums :
    Inscription : Août 2004
    Messages : 8 765
    Points : 12 977
    Points
    12 977
    Par défaut
    ba tu stocke le temps actuel au début de la génération de la page(style juste apres le <?php) avec la fonction time();

    Puis à la fin de ton code tu refais un appel à time, tu soustrais la première valeur à celle que tu viens de récupérer et tu as le temps de génération de ta page en secondes

    Voilou, ensuite je ne sais pas si ça correspond à ce que tu recherche, mais bon c'est une solution onne peut plus simple
    Hey, this is mine. That's mine. All this is mine. I'm claiming all this as mine. Except that bit. I don't want that bit. But all the rest of this is mine. Hey, this has been a really good day. I've eaten five times, I've slept six times, and I've made a lot of things mine. Tomorrow, I'm gonna see if I can't have sex with something.

  3. #3
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    OUi mais je me demandais si il n'existait pas deja des fonctions toute faites.

    Et j'aurai la même question pour compter le nombre de requette? Faut quand même pas faire un compter et faire + 1 à chaque requette?
    manganimes (en construction) -
    zemanga

  4. #4
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    A oui le probleme avec la fonction time c'est que l'on a les secondes. On ne peut pas avoir les centieme de seconde?
    manganimes (en construction) -
    zemanga

  5. #5
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    C'est bon j'ai trouvé microtime .
    manganimes (en construction) -
    zemanga

  6. #6
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    Par contre c'est bizare. Desfois quand je raffraichi j'obtient un temps négatifs normal?
    manganimes (en construction) -
    zemanga

  7. #7
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Ouahou tu remontes le temps !
    fais voir ton code ... (que je remonte moi aussi, j'ai les résultats de quelques loto intéressants à jouer 8) )

  8. #8
    Membre éclairé
    Avatar de Kioob
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    550
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 550
    Points : 764
    Points
    764
    Par défaut
    Il existe des librairies "toutes pretes" (dans pear entre autre) qui permettent ça...

    Sinon, voici une partie de ce que j'utilise :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    // --------------------- Fonctions de benchmark
    function & k_bench_init()
    {
        static $bench = array(
            'stats'=>array(),
            'count'=>array(),
            'tmp'=>array(),
            );
     
        return $bench;
    }
    function k_bench_start($cat,$info=false)
    {
        $start=k_time_actual();
        $benchInfo = & k_bench_init();
     
        if($tmp=end($benchInfo['tmp']))
        {
            $include=$tmp['cat'];
            if($tmp['info'])
                $include.=' / '.$tmp['info'];
        }
        else $include=false;
     
        array_push($benchInfo['tmp'],
            array('cat'=>$cat,
                'start'=>$start,
                'info'=>$info,
                'include'=>$include));
    }
    function k_bench_stop($info_end=false)
    {
        $benchInfo = & k_bench_init();
        if(!$tmp=array_pop($benchInfo['tmp']))
            return false;
     
        $cat=$tmp['cat'];
        $info=$tmp['info'];
     
        if($info_end !== false)
        {
            if($info) $info.=' / ';
            $info.=$info_end;
        }
     
        if($tmp['include'] !== false)
            $info.=' (included in : '.$tmp['include'].')';
     
        $time=bcsub(k_time_actual(), $tmp['start'], 6);
        if(!isset($benchInfo['stats'][$cat]))
        {
            $benchInfo['stats'][$cat]=array('1 '.$info=>$time);
            $benchInfo['count'][$cat]=1;
        } else {
            $id=(++$benchInfo['count'][$cat]).' '.$info;
            $benchInfo['stats'][$cat][$id]=$time;
        }
     
        return true;
    }
     
    function & k_bench_get()
    {
        $benchInfo = & k_bench_init();
     
        $level = 1;
        while( k_bench_stop('force '.($level++).' level') );
     
        $stats=array( 'php'=>array(
                'total'=>bcsub(k_time_actual(), k_time_start(), 6)
                ) ) + $benchInfo['stats'];
     
        foreach($stats as $cat => $list){
            if(count($list)>1){
                $count=0;
                foreach($list as $time)
                    $count+=$time;
                $stats[$cat]['total']=$count;
                }
            }
     
        return $stats;
    }
     
     
    // --------------------- Fonctions diverses
     
    function k_time_start()
    {
        static $time = NULL;
     
        if( $time === NULL ) $time = k_time_actual();
     
        return $time;
    }
     
    function k_time_actual()
    {
        $tmp = explode( ' ', microtime() );
        return $tmp[1] . substr( $tmp[0], 1 );
    }
    Pour l'utilisation :
    1) le plus tôt possible dans le code, appeler k_time_start() pour pouvoir initialiser le compteur.
    2) entourer les portions de code à chronometrer comme ceci :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    k_bench_start( 'lecture cache', 'nom du cache' );
    [...]
    k_bench_stop()
    3) à la fin du script, afficher les résultats :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    $stats = k_bench_get();
     
    echo '<ul>';
    foreach( $stats as $cat => $list )
    {
        echo '<li>';
        echo '<ul>', htmlspecialchars( $cat );
     
        foreach( $list as $info => $time )
        {
            echo '<li >', htmlspecialchars( round( $time * 1000 , 2 ) ), 'ms ',
                htmlspecialchars( $info ), '</li>';
        }
     
        echo '</ul></li>';
    }
    echo '</ul>';
    Ce qui donne :


    Bon... ce n'est qu'un exemple... mais en tous cas c'est bien pratique.
    Google is watching you !

  9. #9
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    bah j'ai juste fai:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
     
    <? $tempscreaction=microtime(); ?>
    en de ma page

    et ca:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
     
    $tempsfini=microtime(); 
    	$tempsaffi=$tempsfini-$tempscreaction;
    	echo $tempsaffi;
    en bas de la page .
    manganimes (en construction) -
    zemanga

  10. #10
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    relis la doc de microtime, surtout le type de ce qui est retourné...

  11. #11
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    Ba c'est le time stamp en microseconde non?
    manganimes (en construction) -
    zemanga

  12. #12
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    relis encore...

  13. #13
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    microtime() retourne le stimestamp Unix avec les microsecondes.
    Donc si je fait la soustraction entre les 2 j'ai bien le temps en seconde avec les microseconde non?
    manganimes (en construction) -
    zemanga

  14. #14
    Membre éclairé
    Avatar de Kioob
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    550
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 550
    Points : 764
    Points
    764
    Par défaut
    non justement. Mais ma fonction "k_time_actual()" plus haut utilise correctement microtime()...
    Google is watching you !

  15. #15
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    Citation Envoyé par Kioob
    non justement. Mais ma fonction "k_time_actual()" plus haut utilise correctement microtime()...
    C'est un peu de l'artillerie lourde pour obtenir jsute le temps de génération d'un e page quand même.
    manganimes (en construction) -
    zemanga

  16. #16
    Membre éclairé Avatar de Death83
    Profil pro
    Inscrit en
    Mars 2005
    Messages
    1 667
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2005
    Messages : 1 667
    Points : 878
    Points
    878
    Par défaut
    <?php
    $startTime = array_sum(explode(" ",microtime()));



    echo round((array_sum(explode(" ",microtime())) - $startTime),4).' sec';



    ?>
    c'est deja plus simple quand même ?
    manganimes (en construction) -
    zemanga

  17. #17
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    il y a un exemple d'utilisation sur la doc...
    Ou il y a la fonction de Kioob...
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    function k_time_actual()
    {
        $tmp = explode( ' ', microtime() );
        return $tmp[1] . substr( $tmp[0], 1 );
    }
    Ca de l'artillerie lourde...

  18. #18
    Membre éclairé
    Avatar de Kioob
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    550
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 550
    Points : 764
    Points
    764
    Par défaut
    je te parle d'une seule fonction, qui fait 2 lignes... pas de l'ensemble qui permet de benchmarker relativement "finement" de multiples portions de scripts dans un même code...
    Google is watching you !

  19. #19
    Membre éclairé
    Avatar de Kioob
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    550
    Détails du profil
    Informations personnelles :
    Âge : 44
    Localisation : France, Rhône (Rhône Alpes)

    Informations forums :
    Inscription : Septembre 2004
    Messages : 550
    Points : 764
    Points
    764
    Par défaut
    note : en utilisant array_sum() tu perds une grande partie de la précision de microtime()...

    Pour pouvoir faire les calculs correctement il faut utiliser les fonctions bc_xxx(). Or, comme c'est assez couteux il est plus rapide de conserver le résultat de microtime() en chaine, et de ne faire les calculs qu'au dernier moment.


    Edit : d'ailleurs la fonction en question est inspirée de la classe benchmark de PEAR si j'ai bonne mémoire...
    Google is watching you !

  20. #20
    Expert éminent Avatar de Mr N.
    Profil pro
    Inscrit en
    Septembre 2004
    Messages
    5 418
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Septembre 2004
    Messages : 5 418
    Points : 6 449
    Points
    6 449
    Par défaut
    Une autre classe d'artillerie lourde sinon que je trouve sympa : http://phpdebug.sf.net

Discussions similaires

  1. [SQL] mesurer le temps d'affichage d'une page
    Par psychoBob dans le forum PHP & Base de données
    Réponses: 15
    Dernier message: 23/06/2006, 17h03
  2. Calculer le temp de consultation d'une page web?
    Par Chikh001 dans le forum PHP & Base de données
    Réponses: 6
    Dernier message: 05/05/2006, 15h19
  3. Récupérer le temps d'execution d'une methode
    Par Janitrix dans le forum Général Java
    Réponses: 3
    Dernier message: 17/12/2005, 11h01
  4. Réponses: 5
    Dernier message: 24/10/2005, 18h49
  5. [VB.NET] Temps d'éxécution d'une page...
    Par Webman dans le forum ASP.NET
    Réponses: 3
    Dernier message: 04/06/2004, 12h20

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo