我在一台强大的机器上托管了一个 Rails 应用程序——我认为——它应该能够处理比我给它的负载高得多的负载。以下是来自 ApacheBench 的令人印象深刻的测试结果:
ab -kc 250 -n 1000 -H "Accept-Encoding: gzip,deflate" https://www.mysite.com/
Server Software: nginx/1.0.15
Server Hostname: www.mysite.com
Server Port: 443
SSL/TLS Protocol: TLSv1/SSLv3,DHE-RSA-AES256-SHA,2048,256
Document Path: /
Document Length: 4258 bytes
Concurrency Level: 250
Time taken for tests: 16.498 seconds
Complete requests: 1000
HTML transferred: 4258000 bytes
Requests per second: 60.61 [#/sec] (mean)
Time per request: 4124.432 [ms] (mean)
Time per request: 16.498 [ms] (mean, across all concurrent requests)
Transfer rate: 282.83 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 302 1801 866.3 1623 3583
Processing: 183 1993 867.9 1873 7050
Waiting: 183 1880 864.2 1743 7036
Total: 1397 3794 1389.0 3558 10580
Percentage of the requests served within a certain time (ms)
50% 3558
66% 4012
75% 4179
80% 4712
90% 4947
95% 6411
98% 8376
99% 8380
100% 10580 (longest request)
呸。也许我弄错了,但我觉得我应该能够获得比每秒 60 个请求高得多的请求,并且每个请求的时间比 4.1 秒要低得多。
该应用程序位于 c1.xlarge EC2 实例上(7 GB 内存,20 个 EC2 计算单元,8 个虚拟内核,每个内核有 2.5 个 EC2 计算单元,1690 GB 实例存储 64 位平台,I/O 性能:高)。运行 ApacheBench 基准测试的站点根目录正在被操作缓存,甚至不会触及数据库。对它的单个请求如下所示:
Started GET "/" for ##.###.###.## at Thu Apr 19 13:05:50 +0000 2012
Processing by OneOfMyControllers#index as HTML
Read fragment views/www.mysite.com/index (1.1ms)
Completed 200 OK in 2ms
这台机器运行的是 Ubuntu 11.04,我使用的是 Rails 3.1、Ruby 企业版、Passenger 和 nginx。我的 nginx 配置如下所示:
worker_processes 8;
worker_rlimit_nofile 65535;
events {
worker_connections 4096;
}
http {
passenger_root /usr/lib/ruby/gems/1.8/gems/passenger-3.0.8;
passenger_ruby /usr/bin/ruby1.8;
rails_spawn_method smart;
rails_app_spawner_idle_time 0;
rails_framework_spawner_idle_time 0;
passenger_max_pool_size 60;
passenger_pool_idle_time 1000;
ssl_session_cache shared:SSL:3m; # 3MB can hold about 12k SSL cache sessions
ssl_session_timeout 5m; # average user spends 5 minutes on our site
include mime.types;
default_type application/octet-stream;
sendfile on;
tcp_nopush on; # useful with the sendfile option
tcp_nodelay off;
keepalive_timeout 10;
send_timeout 10;
client_body_timeout 10;
client_header_timeout 10;
gzip on;
gzip_static on;
gzip_disable "MSIE [1-6]\.";
gzip_comp_level 4;
gzip_buffers 16 4k;
gzip_min_length 1000;
gzip_proxied any;
gzip_vary on;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_http_version 1.0; # Amazon CloudFront uses HTTP/1.0
include /opt/nginx/conf/sites-enabled/*;
}
在 /opt/nginx/conf/sites-enabled/mysite.com 内部,我有:
server {
listen 80;
server_name mysite.com;
rewrite ^/(.*) http://www.mysite.com/$1 permanent;
}
server {
listen 443;
ssl on;
server_name mysite.com;
ssl_certificate /opt/mysite/ssl/wildcard_gandi_2012/combined-certification.crt;
ssl_certificate_key /opt/mysite/ssl/wildcard_gandi_2012/monserveur.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH:!MD5;
ssl_prefer_server_ciphers on;
rewrite ^/(.*) https://www.mysite.com/$1 permanent;
}
server {
listen 80;
server_name *.mysite.com www.mysite.com;
root /opt/mysite/public;
passenger_enabled on;
access_log /opt/mysite/log/nginx_access.log;
error_log /opt/mysite/log/nginx_error.log;
# Set the maximum file upload size to 25 MB.
client_max_body_size 25M;
}
server {
listen 443;
ssl on;
server_name *.mysite.com www.mysite.com;
ssl_certificate /opt/mysite/ssl/wildcard_gandi_2012/combined-certification.crt;
ssl_certificate_key /opt/mysite/ssl/wildcard_gandi_2012/monserveur.key;
ssl_protocols SSLv3 TLSv1;
ssl_ciphers HIGH:!ADH:!MD5;
ssl_prefer_server_ciphers on;
root /opt/mysite/public;
passenger_enabled on;
access_log /opt/mysite/log/nginx_access.log;
error_log /opt/mysite/log/nginx_error.log;
# Set the maximum file upload size to 25 MB.
client_max_body_size 25M;
# Asset caching
location ~ ^/(assets)/ {
root /opt/mysite/public;
gzip_static on;
access_log off;
expires 1y;
add_header Cache-Control public;
add_header Last-Modified "";
add_header ETag "";
break;
}
}
知道为什么我不能超过 60 个请求/秒吗?有什么明显的我忽略了吗?