php vs static

ad intuito va piu veloce una pagina realizzaa tramite il php o un file statico

È più veloce servire una url html tramite un file statico o tramite il php?
L’intuito potrebbe guidarci, ma misurare è meglio.
In linux un ottimo tool per fare questi esperimenti si chiama ab sviluppato da apache.
Ad esempio per simulare 1000 richieste, 80 alla volta:

ab -k -c 80 -n 1000 localhost/prova.htm

il comando si interpreta così:
-k per simulare il KeepAlive
-c per quante richieste concorrenti
-n quante ne vogliamo fare in totale
infine l’url da testare.

 

Risultato della prova sul file statico:

Server Software:        Apache/2.2.22
Server Hostname:        localhost
Server Port:            80

Document Path:          /prova.htm
Document Length:        10064 bytes

Concurrency Level:      80
Time taken for tests:   0.231 seconds
Complete requests:      1000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    0
Total transferred:      10345000 bytes
HTML transferred:       10064000 bytes
Requests per second:    4326.94 [#/sec] (mean)
Time per request:       18.489 [ms] (mean)
Time per request:       0.231 [ms] (mean, across all concurrent requests)
Transfer rate:          43713.12 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    1   1.0      1       5
Processing:     4   16   2.4     17      20
Waiting:        3   15   2.4     15      20
Total:          9   18   1.9     18      23

Percentage of the requests served within a certain time (ms)
  50%     18
  66%     19
  75%     19
  80%     19
  90%     20
  95%     20
  98%     20
  99%     21
 100%     23 (longest request)

 

Testiamo tramite php
ab -k -c 80 -n 1000 localhost/prova.ph

Server Software: Apache/2.2.22
Server Hostname: localhost
Server Port: 80

Document Path: /prova.php
Document Length: 10063 bytes

Concurrency Level: 80
Time taken for tests: 0.515 seconds
Complete requests: 1000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 10257000 bytes
HTML transferred: 10063000 bytes
Requests per second: 1942.28 [#/sec] (mean)
Time per request: 41.189 [ms] (mean)
Time per request: 0.515 [ms] (mean, across all concurrent requests)
Transfer rate: 19455.00 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 1.3 3 9
Processing: 3 37 5.6 38 46
Waiting: 3 35 5.6 36 44
Total: 6 40 5.3 40 48

Percentage of the requests served within a certain time (ms)
50% 40
66% 41
75% 42
80% 42
90% 43
95% 44
98% 45
99% 45
100% 48 (longest request)

 

Vince il file statico, con una media di 4326.94 richieste elaborate al secondo, contro le 1942.28 del php, circa il doppio.
La diffrenza grande è nei requisiti di memoria, perchè servendo file statici non si ha bisogno di risorse addizionali, mentre ogni interprete php richiede ulteriori 16mb (quantita configurabile).


Proviamo con 500 richieste concorrenti

ab -k -c 500 -n 4000 localhost/prova.php >result500.php.txt

Concurrency Level: 500
Time taken for tests: 31.597 seconds
Complete requests: 4000
Failed requests: 49
(Connect: 0, Receive: 0, Length: 49, Exceptions: 0)
Write errors: 0
Total transferred: 40873095 bytes
HTML transferred: 39762864 bytes
Requests per second: 126.60 [#/sec] (mean)
Time per request: 3949.580 [ms] (mean)
Time per request: 7.899 [ms] (mean, across all concurrent requests)
Transfer rate: 1263.27 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 5 17 6.9 17 31
Processing: 14 1918 7281.5 48 31562
Waiting: 0 1623 6900.0 32 31561
Total: 37 1935 7283.6 63 31584

Percentage of the requests served within a certain time (ms)
50% 63
66% 64
75% 64
80% 65
90% 90
95% 31499
98% 31572
99% 31583
100% 31584 (longest request)

 

ab -c 500 -n 4000 localhost/prova.htm >result500.htm.txt

Concurrency Level: 500
Time taken for tests: 6.470 seconds
Complete requests: 4000
Failed requests: 0
Write errors: 0
Keep-Alive requests: 0
Total transferred: 41028000 bytes
HTML transferred: 40252000 bytes
Requests per second: 618.25 [#/sec] (mean)
Time per request: 808.734 [ms] (mean)
Time per request: 1.617 [ms] (mean, across all concurrent requests)
Transfer rate: 6192.77 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 3 30 24.4 26 1046
Processing: 39 285 845.2 87 6438
Waiting: 6 261 850.3 62 6438
Total: 57 315 843.9 119 6459

Percentage of the requests served within a certain time (ms)
50% 119
66% 121
75% 122
80% 132
90% 183
95% 2208
98% 2242
99% 6452
100% 6459 (longest request)

 

Con 500 richieste concorrenti il divario si ampia, ed il file statico è 5 volte più veloce.
via php 126.60 richieste/secondo.
via file 618.25 richieste/secondo.