How to increase PHP speed over HTML?

I have a PHP file that gets HTML from Memcached and serves the user. When I test it like this:

ab -n 1000 -c 100 http://website.com/test.php

It performs 22 requests per second.

But when I put the same HTML in an HTML file and do this test:

ab -n 1000 -c 100 http://website.com/test.html

I am getting like 4500 requests per second.

I need to stick with PHP because the first time I need to generate the HTML, and the next time I just get the generated from Memcached. And furthermore, the HTML display I am different for every other user (recognized based on the value of $ _GET ['user_id']). Is there a way to make the RPS higher? Closer to serving plain HTML?

I am using lighttpd as my web server.

+2


source to share


4 answers


Thought Chain (to be continued):
I would first check if the problem is caused or significantly worsens with concurrency. With your test -n 1000 -c 100

, you had a 22/4500 ratio. What about -n 1000 -c 10

or -n 1000 -c 1

?

Then I would try the same thing again + keeping track of memory consumption, disk I / O and CPU usage. Is this clearly a limiting factor?

Then I would check out simple PHP scripts:

  • empty script
  • <? php echo 'x';
  • no script at all, but the content is test.html

    copied totest.php

  • serves only for a small Memcached item. As simple as possible, script, build a Memchached object and get the item, no test, no expiration, no add, justecho $mc->get(string $key)

How do they compare to test.html?

Edit:



Take Web Server Performance Comparison: LiteSpeed ​​2.0 VS as a comparison point. The test was performed "on" another "competing" web server product, but for now, let's say they weren't (either) biased; -)

They had

  • Processor: One Intel Xeon 2.4 GHz / 533 MHz FSB / 512 KB L2 cache
  • Memory: 256MB ECC PC2700
  • Hard Drive: Seagate ST336607LW 36GB 10K SCSI Hard Drive
  • Network adapter: Intel PRO / 1000 Gigabit Adapter board.

Lighthttpd served 15475 files per second out of 100 bytes, scripts helloworld.php

1593 times per second and phpinfo.php 399 times per second (both FastCGI ). This ratio is ~ 1:10 (hello world) or 1:40 (phpinfo). The "your" ratio is 22: 4500 ~ 1: 200. And even a stranger doesn't change when the script changes. Your "real" script or empty PHP script, it doesn't matter, is always 1:22. This causes a "strangeness factor" quite a bit (although the tests are not identical).

First of all, I would double check if PHP is compiled with FastCGI support, see http://www.fastcgi.com/docs/faq.html#PHP . Then I would test "my" lighthttpd with a simple C / C ++ FastCGI program as mentioned in the test, a real simple "hello world". Example: http://www.fastcgi.com/devkit/doc/fastcgi-prog-guide/ch2c.htm#4263 . If it scales "well", that is, significantly better than your PHP FastCGI, I would try it with a "barely running" version of PHP, ie. Compiled with --disable-all

, and only those modules (re) that have been activated and built in are needed to run PHP and let it print "hello world". Also use the default php.ini

. Does this change anything ?

+3


source


Is the transition faster than a script?

Try writing simple HTML files and serving them. And create an "updater" script that updates your HTML files from time to time, or on a specific event if you really need that kind of speed.

Try SSI in some places and see how it works ( http://httpd.apache.org/docs/1.3/howto/ssi.html ).



Try using Eaccelerator ( http://eaccelerator.net/ ) or APC ( http://www.php.net/apc/ ) to speed up your script parser, but it won't work wonders on PHP5 ...

Make sure the physical server has enough free resources (FAST hard disk, lots of RAM, multiple processors).

It's pretty normal that your script is slower than the HTML page :). Working with an HTML page means a simple file-by-wire instance. PHP script means initializing the script engine, caching, parsing classes, functions, allocating memory, locking / unlocking a session and saving, reading from the Memcached server, reading configuration files. For each of the requests.

+3


source


You might also want to consider placing an HTTP cache in front of your PHP server. This will reduce the load on your web server and handle re-submitting of previously rendered pages for you.

See Varnish for example. Another option: Squid

Obviously, these are not options if you are on shared hosting - in which case rendering to .html files is a great solution.

+2


source


You can try (psuedo code):

if file myfilecache/$user_$request_$today.html does not exist 
then do
     format page 
     write page to myfilecache/$user_$request_$today.html
done
redirect to myfilecache/$user_$request_$today.html

      

The filesystem makes a pretty good cache and lightpd will do the actual work of serving the page.

+1


source







All Articles