pagina-snelheid-testen

Gesponsorde koppelingen

PHP script bestanden

  1. pagina-snelheid-testen

« Lees de omschrijving en reacties

speedtest.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
<?php
// Totaal aantal requests per URL
$iRequestLimit = 20;

$arrURLS[] = "http://www.google.nl";
// $arrURLS[] = ; // Voeg een willekeurig aantal urls in

$arrTimes = array();

foreach($arrURLS as $URL){
    set_time_limit(500);
    
    $oTimer = new Div_Timer();
    
    for($i=0; $i<$iRequestLimit; $i++){
        $URL1 = $URL.'?'.md5(rand());
        if(($fp = fopen($URL1, "rb")) !== false){
            // Downloadcode verwijderd, fopen wacht totdat de pagina compleet is.
            fclose($fp);
        }
    }

    
    $fTime = $oTimer->GetTime();
    
    $arrTimes[] = $fTime;
}


array_multisort($arrTimes, SORT_ASC, SORT_NUMERIC, $arrURLS);

echo '
Total requests: '
.$iRequestLimit.'<br />
Ordered by time (fastest above)<br />
<table cellpadding="2" border="1">
<tr><td>URL</td><td>Total Req. time</td><td>AVG Req time</td><td>Index</td></tr>
'
;

for($i=0; $i<count($arrTimes); $i++){
    
    if($i==0){
        $fIndexedTime = $arrTimes[$i];
    }

    
    echo '<tr><td>'.$arrURLS[$i].'</td><td>'.$arrTimes[$i].' s</td><td>'.($arrTimes[$i]/$iRequestLimit).' s</td><td>'.($arrTimes[$i]/$fIndexedTime*100).'</td></tr>';
}


echo '</table>';


class Div_Timer{
    
    private $_fStartTime;
    private $_fEndTime;
    private $_blnFinished = false;
    private $_strFile;

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

    function __construct($strFile = null){
        if(!is_null($strFile)) $this->SetFile($strFile);
        $this->Start();
    }


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

    function SetFile($strFile){
        $this->_strFile = $strFile;
    }

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

    function Start(){
        $this->_fStartTime = $this->MicrotimeAsFloat();
        $this->_blnFinished = false;
    }


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

    function MicrotimeAsFloat(){
        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->MicrotimeAsFloat();
            $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 GetTime(){
        if($this->_blnFinished){
            return ($this->_fEndTime - $this->_fStartTime);
        }

        else{
            return ($this->MicrotimeAsFloat() - $this->_fStartTime);
        }
    }
}


?>

 
 

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.