由 徐永久 发表于 2007年03月16日 20:48。
这里的 富 , Rich 其实是指 胖客户, 在 n-Tier 结构的时代,瘦客户成为时尚,在 Web 2.0 时代我们更崇尚 Rich Client,体现更加丰富,更加个性化的服务器内容。
http://talks.php.net/show/oscon06/3
这个 Slide 的前几页讲述了 PHP 性能提升的经过,十分值得一读。
How to Get Rich
1. Build tagged, socially networked, Web 2.0ish, Ajaxy thing
2. Get 500,000 users quickly (and have it actually work)
3. Profit
500,000 users at an average of 100 front/backend requests daily
= 578 requests/second.
x 3 (because your traffic won’t be evenly distributed)
= ~1700 requests/second.
Hardware: 1.8GHz AMD Athlon, w/ 1GByte RAM and 3ware 7200rpm drives under a 3ware 7500-4 RAID card using a full-duplex 100MBit dedicated segment with no other traffic
Software: Ubuntu Linux 2.6.15, Apache-1.3.34, PHP 5.1.3-dev, PostgreSQL 8.1
We need 1700 req/sec and we are at 17 req/sec. We need 100 servers!
我们看看他做了些什么:
So, let’s turn off ssl in the postgresql.conf file and try again.
Better, but another look at the callgraph shows we are still spending 10% of our time connecting to the database and more time tearing the connection back down.
Turn on persistent connections
Ok, we are down to only needing 5 servers now.
Non-persistent: 370 requests/second
Persistent: 525 requests/second
This is with MySQL’s query cache enabled. But Callgrind shows us that we are actually calling MySQL’s internal prepare/execute API.
Callgraph [mysql.out]
PDO::ATTR_EMULATE_PREPARES was added in PHP 5.1.3. You can use PDO::MYSQL_ATTR_DIRECT_QUERY in earlier versions.
That brings us to 580 requests/second. Down to 3 servers!
The same tests with InnoDB reveal similar numbers.
Sqlite3 comes in at around 550 requests/second with persistent connections.
If we look at the last callgraph again, we see that we are spending close to 40% of our time parsing and compiling. We can eliminate that by using an opcode cache like APC.
If we look carefully we see that over 5000 requests we are opening 20000 files. Main script plus 4 includes on each request. This is because include_once and require_once don’t play nice with opcode caches right now. Changing to require brings us to 875 requests/second.
APC also has a no-stat mode. If you give it absolute paths, it can skip the stat() call.
This takes us to 885 requests/second. We are down to 2 machines!
APC has a nice feature where you can store PHP variables in shared memory. We can use that for our config array.
This takes us to 890 requests/second.
Of course, we can take this further and also cache the result of the database query for 5 minutes.
This takes us to 1080 requests/second.
A final optimization is to get rid of an include file. Our utils.inc file was a bit useless.
详细过程可以参见网站内容。从整个过程我们可以看到优化是基于数据库连接,数据库缓冲,PHP本身缓冲来完成的。
2007-03-16 22:26
老徐
发表于
标签: 