想看看rails实际的性能
用rails写了一个简单的查询用户的action,controller中show方法如下:
def show
@user = User.find_by_id(params[:id])
respond_to do |format|
format.xml
end
end
部署采用 ruby 1.8.7 rails 2.3.2 , lighttpd + fastcgi 和 nginx + mongrel 两种方式实验, 操作系统是debian 5.0,数据库是mysql 5.0.51a, 服务器硬件配置是 两个4核cpu, 2G内存, rails的实例数10-15之间
使用另外一台机器多次执行 ab -n 10000 -c 20 http://[server_ip]/users/1.xml 进行简单的性能测试,两种部署方式的压力测试结果比较接近,下面是其中某次测试的结果
Requests per second: 1124.64 [#/sec] (mean)
Time per request: 17.783 [ms] (mean)
Time per request: 0.889 [ms] (mean, across all concurrent requests)
从 production.log的日志上来看,大部分的请求的处理时长都在1-2ms, qps的值还算可以接受
但是比较奇怪的是,整个压力测试期间,所有cpu的资源占用 几乎都是接近100%,使用htop观察都是rails进程占用,内存占用几乎维持在585M以内,使用vmstat观察,没有swap in 和 out的操作, vmstat在性能测试期间的输出
procs -----------memory---------- ---swap-- -----io---- -system-- ----cpu----
r b swpd free buff cache si so bi bo in cs us sy id wa
0 0 0 949500 109752 413004 0 0 0 3 12 25 0 0 100 0
14 0 0 947656 109752 413184 0 0 0 0 4742 10677 62 6 32 0
14 0 0 948152 109752 413504 0 0 0 0 6198 13899 91 9 0 0
16 0 0 947164 109752 413876 0 0 0 0 6106 13828 91 9 0 0
15 0 0 947664 109752 414316 0 0 0 0 6035 14070 92 8 0 0
15 0 0 947128 109752 414664 0 0 0 0 6338 14701 92 8 0 0
15 0 0 946780 109752 415072 0 0 0 2016 5815 13463 91 8 1 0
16 0 0 946740 109752 415420 0 0 0 0 6045 13756 90 10 0 0
15 0 0 945656 109752 415900 0 0 0 0 6153 14314 90 10 0 0
为什么cpu的资源占用会这么高呢?