介绍
在部署之前,负载测试是一个好主意,在运行更详细的测试之前,快速建立一个项目的最佳情况。
ApacheBench工具(ab)可以通过发送任意数量的同时请求来加载测试服务器。
在本教程中,我们将看到不同服务器的Ruby翻译器如何在负载下执行。教程步骤假定一个新鲜的Ubuntu 13.10 x32图像。
安装
更新包数据库。
1apt-get update
安装 apache2-utils 包来访问 ApacheBench。
1apt-get install apache2-utils
有限用户特权
接下来,创建将 Ruby 管理的用户,在下一节中运行某些命令作为 root 并不是一个好主意。
1useradd -m -d /home/test -s /bin/bash -g sudo test
这个命令实现了什么:
- useradd - create a new user
- -m - create the home directory
- -d /home/test - set the user's home directory to /home/test
- -s /bin/bash - make the user's default shell bash (Ubuntu uses dash by default)
- -g sudo - add user to the sudo group (for running commands with sudo)
- test - the name of the new user
给新用户设置密码。
1passwd test
转到新用户。
1su test
RVM 的
Ruby 版本管理器使它很容易与不同的 Ruby 环境一起工作. 它负责安装特定 Ruby 版本和隔离宝石的过程。
1\curl -L https://get.rvm.io | bash -s stable
要使用 rvm 命令,您需要先运行 rvm 脚本。
1source ~/.rvm/scripts/rvm
如果你愿意,你可以把它放在你的.bashrc,这样 rvm 可以随时登录作为用户。
1echo "source ~/.rvm/scripts/rvm" >> ~./bashrc
您可以通过检查类型头来验证使用 rvm 脚本,它应该是一个函数,而不是哈希。
1type rvm | head -1
2
3rvm is a function
接下来,安装 Ruby 2.0.0. RVM 将要求用户的密码,因为它需要安装各种依赖性,才能创建 Ruby。
1rvm install 2.0.0
切换到新的 Ruby. 安装后默认情况下可能发生,但检查不会伤害。
1rvm use 2.0.0
测试
现在Ruby已安装,您可以创建一个简单的网站,并查看它可以处理多少请求。
安装 Sinatra. 这是一个用于创建 Ruby Web 应用程序的微框架/DSL。 --no-* 旗帜跳过了文档。
1gem install sinatra --no-rdoc --no-ri
创建样本 sinatra 应用程序,它只响起你好世界
。
1cd ~
2vim app.rb
3
4# app.rb
5require 'sinatra'
6
7get '/' do
8 'hello world'
9end
运行服务器。
1ruby app.rb
随着服务器终于启动,您可以开始负载测试. 调用 ab 看起来像这样:
1ab -n <num_requests> -c <concurrency> <addr>:<port><path>
打开另一个终端并再次将 ssh 输入到服务器中。使用 ApacheBench 进行测试. 我使用了 1000 个请求,共计 100 个请求。
1ab -n 1000 -c 100 http://localhost:4567/
2
3Server Software: WEBrick/1.3.1
4Server Hostname: localhost
5Server Port: 4567
6
7Document Path: /
8Document Length: 11 bytes
9
10Concurrency Level: 100
11Time taken for tests: 3.410 seconds
12Complete requests: 1000
13Failed requests: 0
14Write errors: 0
15Total transferred: 288000 bytes
16HTML transferred: 11000 bytes
17Requests per second: 293.23 [#/sec] (mean)
18Time per request: 341.034 [ms] (mean)
19Time per request: 3.410 [ms] (mean, across all concurrent requests)
20Transfer rate: 82.47 [Kbytes/sec] received
21
22Connection Times (ms)
23 min mean[+/-sd] median max
24Connect: 0 1 2.0 0 11
25Processing: 185 332 90.3 311 578
26Waiting: 28 280 83.2 267 574
27Total: 193 333 89.7 311 578
28
29Percentage of the requests served within a certain time (ms)
30 50% 311
31 66% 357
32 75% 423
33 80% 446
34 90% 467
35 95% 480
36 98% 490
37 99% 501
38100% 578 (longest request)
我的结果接近300请求/秒。WEBrick不为其速度而闻名。继续前进并用Ctrl-c中断服务器。
安装TIN
Thin是一个流行的 Ruby Web 服务器,它使用 Mongrel 进行解析和 EventMachine 进行非阻止 IO. 安装 Thin 并重新运行服务器。
1gem install thin
2ruby app.rb
现在,再试一次负载测试,这次应该更快一点。
1Server Software: thin
2Server Hostname: localhost
3Server Port: 4567
4
5Document Path: /
6Document Length: 11 bytes
7
8Concurrency Level: 100
9Time taken for tests: 1.339 seconds
10Complete requests: 1000
11Failed requests: 0
12Write errors: 0
13Total transferred: 244000 bytes
14HTML transferred: 11000 bytes
15Requests per second: 747.00 [#/sec] (mean)
16Time per request: 133.870 [ms] (mean)
17Time per request: 1.339 [ms] (mean, across all concurrent requests)
18Transfer rate: 178.00 [Kbytes/sec] received
19
20Connection Times (ms)
21 min mean[+/-sd] median max
22Connect: 0 1 1.8 0 8
23Processing: 55 128 19.9 132 155
24Waiting: 42 116 19.7 121 144
25Total: 62 129 18.5 132 156
26
27Percentage of the requests served within a certain time (ms)
28 50% 132
29 66% 135
30 75% 137
31 80% 139
32 90% 144
33 95% 149
34 98% 152
35 99% 155
36100% 156 (longest request)
至少在这种情况下,它看起来像Thin比WEBrick更快的服务器,超过700请求 / 秒(你可以尝试提高总请求,但它对我来说并不高)。
** 注意**:我能够在Arch Linux dropplet上获得 1000请求/秒。
结论
显然,这些结果并不反映真实的服务器性能。HTTP只是一个谜题。一个缓慢的模板引擎和/或数据库会将这些数字大大拖下来。
您可能感兴趣的其他性能工具: