为什么我的Web应用程序在LAMP EC2实例上的并发能力如此之低?

我来自Web开发人员背景,一直在使用CakePHP框架构build我的PHP应用程序。 当我开始在应用程序所在的Amazon EC2实例上进行ab(Apache Bench)testing时,出现了这个问题。 即使我正在运行一个c1.medium实例(2个内核,2GB内存),我的平均页面加载时间也非常可怕,我想我正在做的一切都是正确的。

我会跑:

ab -n 200 -c 20 http://localhost/heavy-but-view-cached-page.php 

结果如下:

 Concurrency Level: 20 Time taken for tests: 48.197 seconds Complete requests: 200 Failed requests: 0 Write errors: 0 Total transferred: 392111200 bytes HTML transferred: 392047600 bytes Requests per second: 4.15 [#/sec] (mean) Time per request: 4819.723 [ms] (mean) Time per request: 240.986 [ms] (mean, across all concurrent requests) Transfer rate: 7944.88 [Kbytes/sec] received 

在abtesting运行时,我运行VMStat,它显示Swap保持在0,CPU一直在80-100%(尽pipe我不确定我是否可以信任这个虚拟机),RAM利用率可以提高到1.6 G(剩下400M免费)。 负载上升到8左右,网站缓慢爬行。

以下是我认为我在代码方面做的事情:

  • 在Chrome浏览器中,未caching的页面通常在800-1000毫秒内加载,caching页面在300-500毫秒内加载。 不是惊艳,但也不可怕。
  • 由于视图caching,每个页面加载最多可以有一个DB查询来写入会话数据。 所以我们可以排除一个DB瓶颈。
  • 我有APC。
  • 我正在使用Memcached来提供视图caching和其他网站caching。
  • xhprof代码分析器显示caching页面占用10MB-40MB的内存和100毫秒 – 1000毫秒的墙壁时间。

在xhprof中,那些可能是最糟糕的罪犯的页面将如下所示:

 Total Incl. Wall Time (microsec): 330,143 microsecs Total Incl. CPU (microsecs): 320,019 microsecs Total Incl. MemUse (bytes): 36,786,192 bytes Total Incl. PeakMemUse (bytes): 46,667,008 bytes Number of Function Calls: 5,195 

我的Apacheconfiguration:

 KeepAlive On MaxKeepAliveRequests 100 KeepAliveTimeout 3 <IfModule mpm_prefork_module> StartServers 5 MinSpareServers 5 MaxSpareServers 10 MaxClients 120 MaxRequestsPerChild 1000 </IfModule> 

服务器有问题吗? 一些与EC2陷阱? 或者是我的代码? 我应该考虑一些明显的设置? DNS查询过多? 我错过了什么? 我真的希望达到1,000个并发能力,但是以这样的速度,这不会发生。

你正在传输大量的数据。 8mbytes / s,你最大化你的networking连接。