php-timer-object

Gesponsorde koppelingen

PHP script bestanden

  1. php-timer-object

« Lees de omschrijving en reacties

voorbeeld.php

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?
include('TIMER.class.php');

// Creer het timer object
$oTimer = new DIV_TIMER('log.txt');

// Creer ook een performance meter, gewoon als voorbeeld
$oPerfmeter = new DIV_PERFORMANCE_METER();

// doe wat dingen
$j = 0;
for($i=0; $i<100; $i++)
{

    $j = $j + $i * 2;
    if($i%10 == 0)
    {

        echo 'Tien rondjes gedaan  j = '.$j.'<br />'."\r\n";
        $oPerfmeter->add_point('Tijd na ronde '.$i);
    }
}


// Print de tijd die je script erover gedaan heeft:
$oTimer->stop();
echo $oTimer->get_time();

// Leuk om te zien hoelang iedere ronde duurde?
//  dit wordt nog als html commentaar geplaatst. Staat dus automatisch in je source
// Niet erg netjes, moet nog aangepast worden.

echo '<br />Voor ronde tijden, zie de source!<br/>'."\r\n";
echo '<a href="log.txt">klik hier voor de log</a>';

$oPerfmeter->parse();

?>


TIMER.class.php
Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?
class DIV_TIMER
{
    var
$_fStartTime;
    var
$_fEndTime;
    var
$_blnFinished = false;
    var
$_strFile;

    /**
     * Constructor. Autostart the timer.
     *
     */

    function DIV_TIMER($strFile = null)
    {

        $this->_strFile = $strFile;
        $this->start();
    }


    /**
     * Set the filename to save statistics in
     *
     * @param unknown_type $strFile
     */

    function set_file($strFile)
    {

        $this->_strFile = $strFile;
    }

    
    /**
     * Start the internal timer. Can also be used for a restart
     *
     */

    function start()
    {

        $this->_fStartTime = $this->microtime_as_float();
        $this->_blnFinished = false;
    }


    /**
     * Can be used static
     *
     * @return unknown
     */

    function microtime_as_float()
    {

        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }


    /**
     * Stop the timer. The stoptime will be remembered.
     *  If a file isset, then the time will be saved.
     *
     */

    function stop()
    {

        if(!$this->_blnFinished)
        {

            $this->_fEndTime = $this->microtime_as_float();
            $this->_blnFinished = true;
            
            if(!is_null($this->_strFile))
            {

                $strPerfData = '';
                $strMyFilename = $_SERVER['REQUEST_URI'];
                $fTimeDiff = ($this->_fEndTime - $this->_fStartTime);
                
                if(file_exists($this->_strFile) && filesize($this->_strFile))
                {

                    $f = fopen($this->_strFile, "r");
                    $strPerfData = fread($f, filesize($this->_strFile));
                    fclose($f);
                }

                
                if(strpos($strPerfData, $strMyFilename) !== false)
                {

                    $iLinestart = strpos($strPerfData, $strMyFilename);
                    $iLineend = strpos($strPerfData, ';', $iLinestart);
                    
                    $strLineData = substr($strPerfData, $iLinestart, $iLineend-$iLinestart);
                    list($strFile, $iCount, $fAvgTime) = explode(':', $strLineData);
                    $iCount = intval($iCount) + 1;
                    $fAvgTime = (floatval($fAvgTime) * ($iCount-1) + $fTimeDiff) / $iCount;
                    
                    $strPerfData = substr_replace($strPerfData, $strMyFilename.':'.$iCount.':'.$fAvgTime, $iLinestart, $iLineend-$iLinestart);
                }

                else
                {
                    $strPerfData .= $strMyFilename.':1:'.$fTimeDiff.";\r\n";
                    
                }

                
                $f = fopen($this->_strFile, "w+");
                fwrite($f, $strPerfData);
                fclose($f);
            }
        }
    }


    /**
     * Stop the timer and reset the data
     *
     */

    function reset()
    {

        $this->_fStartTime = 0;
        $this->_fEndTime=0;
        $this->_blnFinished = true;
    }


    /**
     * Get the time in seconds
     *
     * @return timing time in seconds
     */

    function get_time()
    {

        if($this->_blnFinished)
            return ($this->_fEndTime - $this->_fStartTime);
        else return ($this->microtime_as_float() - $this->_fStartTime);
    }
}


class DIV_PERFORMANCE_METER
{
    var
$_oTimer;
    var
$_arrAssocPoints = array();

    /**
     * Constructor
     *
     * @return DIV_PERFORMANCE_METER
     */

    function DIV_PERFORMANCE_METER()
    {

        $this->_oTimer = new DIV_TIMER();
    }


    /**
     * Add a save point to the performance meter. Name it as you like, the name has to be unique, otherwise it will be overwritten
     *
     * @param unknown_type $strName
     */

    function add_point($strName)
    {

        $this->_arrAssocPoints[$strName] = $this->_oTimer->get_time();
    }


    /**
     * Deprecated function. Now named parse_as_htmlcomment
     *
     */

    function parse()
    {

        $this->parse_as_htmlcomment();
    }

    
    /**
     * Output the data in a htmlcomment element
     *
     */

    function parse_as_htmlcomment()
    {

        $strComment = '<!--'."\n";
        foreach($this->_arrAssocPoints as $key=>$value)
            $strComment .= $key.':'."\t".$value."\n";
            
        $strComment .= '-->';
        
        echo $strComment;
    }

    
    /**
     * Get an array with all the points, the array is associative. The key of every element is equal to the name passed with add_point()
     *
     * @return unknown
     */

    function get_timedata()
    {

        return $this->_arrPoints;
    }
    
}

?>

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.