~bohwaz/blog/

Avec de vrais morceaux de 2.0 !

Is serving files from PHAR slower? (PHAR benchmark)

PHP PHAR archives allow you to put a whole application in one file, including code and static content (CSS, images, etc.). Serving static files from a PHAR archive might look attracting as you don't have to ship these files separately from your code, and you can say ship a PHAR file instead of a ZIP file, which will work out of the box (except with Suhosin). But isn't serving static content from PHP too slow? Well let's benchmark that.

Benchmark context

  • Hardware: Atom N450 @ 1.66GHz, RAM 1GB, 7200 rpm hard drive
  • PHAR configuration: cache not enabled for this archive
  • Tool used for benchmark: ab -n 1000 (repeated 5 times)
  • PHAR archive: native Phar format (not Tar)

Serving dynamic index file (PHP code)

  • Average request time : 8.894 ms
  • Average uncompressed PHAR request time : 12.816 ms (+44%)
  • Average GZ-compressed PHAR request time : 13.455 ms (+4% from uncompressed)
  • Average BZ2-compressed PHAR request time : 14.211 ms (+5% from GZ, +10% from uncompressed)

Serving static CSS file

  • Average request time : 7.150 ms
  • Average uncompressed PHAR request time : 12.275 ms (+63%)
  • Average GZ-compressed PHAR request time : 12.909 ms (+5% from uncompressed)
  • Average BZ2-compressed PHAR request time : 13.710 ms (+6% from GZ, +11% from uncompressed)

Obviously, serving static content from PHAR archives is a huge overhead. Serving dynamic PHP content from PHAR archives is "just" 44% slower. But as you can notice, compressing the archive and serving content from it is not really noticeable.

If you still want to distribute your static files inside the PHAR archive, I'd advise you to extract those files from archive at first run so that they will be served directly by the HTTP server, or maybe just use the PHAR file as a self-contained installer that will extract application files at first run?

Write a comment
(optional)
(optional)
(mandatory)
                     _  __  _      
 _ __ ___   ___   __| | \_\| | ___ 
| '_ ` _ \ / _ \ / _` |/ _ \ |/ _ \
| | | | | | (_) | (_| |  __/ |  __/
|_| |_| |_|\___/ \__,_|\___|_|\___|
                                   
(mandatory)

URLs will create links automatically.
Allowed HTML tags: <blockquote> <cite> <pre> <code> <var> <strong> <em> <del> <ins> <kbd> <samp> <abbr>

Scopey

The real test here should be a PHP file that does considerable work. Percentages are probably not relevant as the work done by the script should be independent from the uncompressing of the PHAR.