如何使用 ApacheBench 在 Arch Linux VPS 上进行负载测试

介绍


在部署之前,负载测试是一个很好的主意,虽然这不是一件好事,但有时在运行更详细的测试之前,可以快速建立一个项目的最佳情况。

ApacheBench工具(ab)可以通过发送任意数量的同时请求来加载测试服务器。

在本教程中,我们将看到如何在负载下执行不同的服务器的Ruby解释器。教程步骤假定新鲜的Arch Linux x86_64图像。

安装


更新包数据库。

1pacman -Sy

安装 apache 包以获取访问 ApacheBench. 或者,它包含在 AUR 中的 apache 工具包中。

1pacman -S apache

有限用户特权


接下来,创建将 Ruby 管理的用户,在下一节中运行某些命令作为 root 并不是一个好主意。

1useradd -m -d /home/test test

转到新用户。

1su test

RVM 的


Ruby 版本管理器使它很容易与不同的 Ruby 环境一起工作。它负责安装特定 Ruby 版本和隔离宝石的过程。

1\curl -L https://get.rvm.io | bash -s stable

要使用 rvm 命令,您需要先运行 rvm 脚本。

1source ~/.rvm/scripts/rvm

接下来,安装 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 ~
2vi 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:        0.0.0.0
 5Server Port:            4567
 6
 7Document Path:          /
 8Document Length:        11 bytes
 9
10Concurrency Level:      100
11Time taken for tests:   2.950 seconds
12Complete requests:      1000
13Failed requests:        0
14Write errors:           0
15Total transferred:      288000 bytes
16HTML transferred:       11000 bytes
17Requests per second:    338.94 [#/sec] (mean)
18Time per request:       295.041 [ms] (mean)
19Time per request:       2.950 [ms] (mean, across all concurrent requests)
20Transfer rate:          95.33 [Kbytes/sec] received
21
22Connection Times (ms)
23              min mean[+/-sd] median max
24Connect:        0 1 2.8 0 15
25Processing:   117 285 94.3 268 553
26Waiting:       70 248 91.8 234 544
27Total:        117 286 93.6 271 553
28
29Percentage of the requests served within a certain time (ms)
30  50%    271
31  66%    327
32  75%    354
33  80%    361
34  90%    413
35  95%    468
36  98%    512
37  99%    539
38100%    553 (longest request)

我的结果接近300请求/秒。WEBrick不为其速度而闻名。继续前进并用Ctrl-c中断服务器。

Thin是一个流行的 Ruby Web 服务器,它使用 Mongrel 进行解析和 EventMachine 进行非阻止 IO. 安装 Thin 并重新运行服务器。

1gem install thin
2ruby app.rb

现在,再试一次负载测试,这次应该更快一点。

注: Thin 似乎不允许通过 localhost 连接 ApacheBench,但它允许 0.0.0.0 或 127.0.0.1。

 1ab -n 1000 -c 100 http://0.0.0.0:4567/
 2
 3...
 4Concurrency Level:      100
 5Time taken for tests:   0.989 seconds
 6Complete requests:      1000
 7Failed requests:        0
 8Write errors:           0
 9Total transferred:      244000 bytes
10HTML transferred:       11000 bytes
11Requests per second:    1011.13 [#/sec] (mean)
12Time per request:       98.899 [ms] (mean)
13Time per request:       0.989 [ms] (mean, across all concurrent requests)
14Transfer rate:          240.93 [Kbytes/sec] received
15...

至少在这种情况下,它看起来像Thin比WEBrick更快的服务器,超过1000请求 / 秒(你可以尝试提高总请求,但它对我来说并不高)。

结论


显然,这些结果并不反映真实的服务器性能。HTTP只是一个谜题。一个缓慢的模板引擎和/或数据库会将这些数字大大拖下来。

您可能感兴趣的其他性能工具:

Submitted by: Robert Qualls
Published At
Categories with 技术
comments powered by Disqus