schlitt.info - php, photography and private stuff ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :Author: Tobias Schlitt :Date: Fri, 09 Jan 2009 12:34:32 +0100 :Revision: 2 :Copyright: CC by-nc-sa ======================================= Apache vs. Lighttpd: "echo" performance ======================================= :Description: For a little private project, which makes extensive use of caching, I recently checked, where I could get gather some more performance from. Kore told me, that Lighttpd ships all of the pages of one of his projects in about 0.001 seconds, while mine still took 0.004 seconds on Apache. After some tracing I found the actual point of problem: The echo of the final output, which took most of the time. I tried to run the same project on Lighttpd and guess what: There were the 0.001 seconds. For a little private project, which makes extensive use of caching, I recently checked, where I could get gather some more performance from. `Kore`__ told me, that `Lighttpd`__ ships all of the pages of one of his projects in about 0.001 seconds, while mine still took 0.004 seconds on `Apache`__. After some tracing I found the actual point of problem: The `echo`__ of the final output, which took most of the time. I tried to run the same project on `Lighttpd`__ and guess what: There were the 0.001 seconds. .. __: http://kore-nordmann.de .. __: http://www.lighttpd.net/ .. __: http://apache.org .. __: http://php.net/echo .. __: http://www.lighttpd.net/ After this adventure, I made a little benchmark between Apache (with mod_php) and Lighttpd (with fastcgi). I took 3 files of different sizes: a small one, with about 30kb, 1 medium size (about 70kb) and a large image (about 280kb). The only thing my PHP script does is reading the file using file_get_contents() (this part is not measured) and echoing it to the browser (this part is measured). The results, in seconds, are (average values from 1000 script runs): .. csv-table:: "**Files**", "**Apache**", "**Lighttpd**" "**Small**", "8.74919891357E-05", "6.94532394409E-05" "**Medium**", "0.000167278051376", "0.000110051393509" "**Large**", "0.00403597259521", "0.00102773714066" |image_1| .. |image_1| image:: http://files.schlitt.info/blog/apache_lighttpd_stats.png Note: For this image I multiplied the "medium" and "small" numbers with 10! I found this results quite impressive and it confirmed my usage of Lighttpd, although I wondered, where this may come from. A discussion in our usergroup brought up the thesis, that fastcgi uses shared memory to transfer data, while mod_php seems to use something else. I actually have no clue if this is correct, but it sounded valid to me. Maybe someone can enlighten us? .. Local Variables: mode: rst fill-column: 79 End: vim: et syn=rst tw=79 Trackbacks ========== Comments ======== - TLJ at Sat, 28 Oct 2006 19:21:00 +0200 You should try the same benchmark on apache w/fastcgi too. I have a setup with seperate servers for web and PHP. Recently I replaced lighty with a really small and minimal compile of Apache 1.3 running PHP as FastCGI, with the exact same config as lighty had done. To my big surprise, Apache is actually a lot faster. And it doesn't drop 10-30 reqs/sec, like Lighty had a tendency to do. - Tom at Sat, 28 Oct 2006 21:06:29 +0200 Hm, what exatly did you measure and how? For me it looks like some wrong measurment method.... - Ilia Alshanetsky at Sat, 28 Oct 2006 21:45:55 +0200 The slowdown may have to do with the way data is transmitted. Both PHP and Apache buffer output and when the buffers mismatch and/or are too small you can lose a lot of speed. For the sake of a test I'd recommend setting PHP's output buffering to roughly a page size and in Apache setting buffer to page size + 4kb (to allow for headers & misc overhead). - Jan Kneschke at Sat, 28 Oct 2006 23:25:25 +0200 FastCGI is purely socket based. It either uses TCP/IP or Unix Domain Sockets. If you care about really small setup costs for a request, take a look at http://blog.lighttpd.net/articles/2006/10/08/reducing-requests-setup-costs or http://jan.kneschke.de/projects/lua/ - Damien at Sun, 29 Oct 2006 00:08:30 +0200 I don't have any clue about this phenomenon, but it could be very interesting to compare PHP4 in CGI (with and without fastcgi) in Apache environnement with PHP4 in Lighttpd in order to see where the problem is. - Christian at Sun, 29 Oct 2006 13:36:01 +0100 Yeah, this is kinda impressive. Isn't there a possibilty to use php with apache via fastcgi? Would be nice to get times for this, too. Greetings - Brian Moon at Mon, 30 Oct 2006 16:14:39 +0100 Interesting. I found the opposite results when testing lighttpd vs. apache for caching large output. The HTML on our front page at dealnews.com can approach 120k. At those sizes, I found Apache must faster. It did take about 20 trips to the lighttpd support forums to get my config right as it seems it does not come configured very well IMO. See http://forum.lighttpd.net/topic/322 - Alexandre Girao at Sat, 04 Nov 2006 17:02:17 +0100 The main difference between apache and lighttpd is the serving model, lighttpd is event-driven and apache is threaded or pre-forked, you can read more information here: http://www.kegel.com/c10k.html