介绍
在任何部署之前,负载测试是一个好主意。 这是很好的快速建立一个项目的最佳案例,然后在路上运行更详细的测试。
所述ApacheBench工具(AB)可以通过发送的并发请求的任意数量的负载测试的服务器。 虽然ab是为测试Apache安装而设计的,但它可用于对任何HTTP服务器进行基准测试。
在本教程中,我们将看到不同服务器的Ruby解释器如何在负载下执行。 教程步骤假设一个新的Ubuntu 13.10 x32映像。 结果从512MBDroplet获得。
安装
刷新包数据库。
apt-get update
安装apache2-utils软件包以访问ApacheBench。
apt-get install apache2-utils
受限特权用户
接下来,创建将管理Ruby的用户。 在下一节中以root身份运行一些命令不是一个好主意。
useradd -m -d /home/test -s /bin/bash -g sudo test
这个命令完成什么:
useradd – 创建一个新用户
-m – 创建主目录
-d / home / test – 将用户的主目录设置为/ home / test
-s / bin / bash – 使用户的默认shell bash(Ubuntu默认使用dash)
-g sudo – 将用户添加到sudo组(用于使用sudo运行命令)
test – 新用户的名称
设置新用户的密码。
passwd test
切换到新用户。
su test
RVM
Ruby版本管理器可以方便地使用不同的Ruby环境。 它负责安装特定的Ruby版本和隔离gemsets的过程。 它当前是通过从其网站运行bash脚本安装的。
\curl -L https://get.rvm.io | bash -s stable
为了使用rvm命令,您需要先运行rvm脚本。
source ~/.rvm/scripts/rvm
如果你想要,你可以把它放在你的.bashrc中,这样rvm是可用的任何时候,你作为用户登录。
echo "source ~/.rvm/scripts/rvm" >> ~./bashrc
您可以通过检查类型的头来验证是否正在使用rvm脚本。 它应该是一个函数,而不是散列。
type rvm | head -1rvm is a function
接下来,安装Ruby 2.0.0。 RVM将要求用户的密码,因为它需要安装各种各样的依赖,然后才能制作Ruby。 因为RVM从源代码构建Ruby,所以这一步可能需要一段时间。
rvm install 2.0.0
切换到新的Ruby。 这可能在安装后默认情况下发生,但检查不会伤害。
rvm use 2.0.0
测试
现在Ruby已经安装,你可以创建一个简单的网站,看看它可以处理多少请求。
安装Sinatra。 它是一个用于创建Ruby Web应用程序的微框架/ DSL。 –no- *标志跳过文档。
gem install sinatra --no-rdoc --no-ri
创建示例sinatra应用程序,只是回应“hello world”。
cd ~vim app.rb# app.rbrequire 'sinatra'get '/' do 'hello world'end
运行服务器。
ruby app.rb
随着服务器最终开始,您可以开始负载测试。 对ab的调用如下所示:
ab -n <num_requests> -c <concurrency> <addr>:<port><path>
再次打开另一个终端和ssh到服务器。 使用ApacheBench运行测试。 我使用1000个请求,并发性为100.不要忘记路径的最后一个’/’。
ab -n 1000 -c 100 http://localhost:4567/Server Software: WEBrick/1.3.1Server Hostname: localhostServer Port: 4567Document Path: /Document Length: 11 bytesConcurrency Level: 100Time taken for tests: 3.410 secondsComplete requests: 1000Failed requests: 0Write errors: 0Total transferred: 288000 bytesHTML transferred: 11000 bytesRequests per second: 293.23 [#/sec] (mean)Time per request: 341.034 [ms] (mean)Time per request: 3.410 [ms] (mean, across all concurrent requests)Transfer rate: 82.47 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 1 2.0 0 11Processing: 185 332 90.3 311 578Waiting: 28 280 83.2 267 574Total: 193 333 89.7 311 578Percentage of the requests served within a certain time (ms) 50% 311 66% 357 75% 423 80% 446 90% 467 95% 480 98% 490 99% 501100% 578 (longest request)
我的结果约为300请求/秒。 WEBrick的速度并不为人所知。 使用Ctrl-c中断服务器。
安装Thin
薄是使用杂种解析和EventMachine的非阻塞IO一个流行的Ruby Web服务器。 安装Thin并再次运行服务器。 Sinatra应该自动加载Thin并让你知道(“…用Thin备份”)。
gem install thinruby app.rb
现在,再次尝试负载测试。 这次应该有点快。
Server Software: thinServer Hostname: localhostServer Port: 4567Document Path: /Document Length: 11 bytesConcurrency Level: 100Time taken for tests: 1.339 secondsComplete requests: 1000Failed requests: 0Write errors: 0Total transferred: 244000 bytesHTML transferred: 11000 bytesRequests per second: 747.00 [#/sec] (mean)Time per request: 133.870 [ms] (mean)Time per request: 1.339 [ms] (mean, across all concurrent requests)Transfer rate: 178.00 [Kbytes/sec] receivedConnection Times (ms) min mean[+/-sd] median maxConnect: 0 1 1.8 0 8Processing: 55 128 19.9 132 155Waiting: 42 116 19.7 121 144Total: 62 129 18.5 132 156Percentage of the requests served within a certain time (ms) 50% 132 66% 135 75% 137 80% 139 90% 144 95% 149 98% 152 99% 155100% 156 (longest request)
至少在这种情况下,看起来Thin使服务器速度明显快于WEBrick超过700请求/秒(你可以尝试提高总请求,但它没有得到更高的我)。
注 :我能得到1000个请求/秒上Arch Linux的Droplet。
结论
显然,这些结果不能反映真实的服务器性能。 HTTP只是一个谜题。 缓慢的模板引擎和/或数据库会显着拖动这些数字。 仍然,它给你一个快速ballpark数字用于比较。
您可能感兴趣的其他性能工具: