gpt4 book ai didi

ruby-on-rails - Puma 或 Unicorn VS Webbrick 负载测试基准显示没有改善

转载 作者:数据小太阳 更新时间:2023-10-29 07:19:43 24 4
gpt4 key购买 nike

设置

好的,我正在 Heroku(免费套餐)上运行 Rails 应用程序。

我有 2 个单独的应用程序版本,我们称它们为 StagingFake-Production

Staging 中,我使用 Webbrick 作为服务器。我的 Procfile

web: rails s -p $PORT

Fake-Production 中,我使用 Puma 作为服务器。我的 Procfile

bundle exec puma -C config/puma.rb

我已将 puma 配置为使用 2 个工作线程和每个工作线程 1 个线程运行。config/puma.rb 定义如下(取自 Heroku's Setting up Puma Webserver )

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 1)
threads threads_count, threads_count

preload_app!

rackup DefaultRackup
port ENV['PORT'] || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
# Worker specific setup for Rails 4.1+
# See: https://devcenter.heroku.com/articles/deploying-rails-applications- with-the-puma-web-server#on-worker-boot
ActiveRecord::Base.establish_connection
end

我的 database.yml 配置为具有 20 个连接池。

测试

为了进行负载测试,我使用笔记本电脑上的 ApacheBench 工具来访问 API 端点。 API 基本上执行非常简单的数据库查询以返回固定数量的记录(不会更改)。

我使用以下代码进行了两次部署:

ab -n 1000 -c 100 https://<some heroku endpoint>?access_token=f73f50514c

结果

这里的结果是最令人惊讶的。我原以为 Puma 部署会完全破坏 Webbrick 部署,但实际上,它几乎是一样的。我尝试访问不同的 API 端点以及 Puma worker 和线程的不同组合(一度是 4 个 worker 和 5 个线程),但没有任何明显的改进。

Webbrick 结果

Server Software:        WEBrick/1.3.1
Server Hostname: webbrick-build.herokuapp.com
Server Port: 443
SSL/TLS Protocol: TLSv1,DHE-RSA-AES128-SHA,2048,128

Document Path: /api/v1/packages?access_token=f73f50514c6
Document Length: 488 bytes

Concurrency Level: 100
Time taken for tests: 21.484 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 995000 bytes
HTML transferred: 488000 bytes
Requests per second: 46.55 [#/sec] (mean)
Time per request: 2148.360 [ms] (mean)
Time per request: 21.484 [ms] (mean, across all concurrent requests)
Transfer rate: 45.23 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 714 1242 278.1 1214 2012
Processing: 248 842 493.6 699 2883
Waiting: 247 809 492.3 677 2876
Total: 1072 2085 643.5 1929 4845

Percentage of the requests served within a certain time (ms)
50% 1929
66% 2039
75% 2109
80% 2168
90% 2622
95% 3821
98% 4473
99% 4646
100% 4845 (longest request)

内存影响

source=web.1 dyno=heroku.1234567899 sample#memory_total=198.41MB sample#memory_rss=197.60MB sample#memory_cache=0.30MB sample#memory_swap=0.51MB sample#memory_pgpgin=103879pages sample#memory_pgpgout=53216pages

Puma 结果(无论 Worker/Thread 数量如何,大致相同)

Server Software:        Cowboy
Server Hostname: puma-build.herokuapp.com
Server Port: 443
SSL/TLS Protocol: TLSv1,DHE-RSA-AES128-SHA,2048,128

Document Path: /api/v1/packages?access_token=fb7168c147adc2ccd83b2
Document Length: 489 bytes

Concurrency Level: 100
Time taken for tests: 23.299 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 943000 bytes
HTML transferred: 489000 bytes
Requests per second: 42.92 [#/sec] (mean)
Time per request: 2329.949 [ms] (mean)
Time per request: 23.299 [ms] (mean, across all concurrent requests)
Transfer rate: 39.52 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 743 1304 283.9 1287 2092
Processing: 253 951 740.3 684 5353
Waiting: 253 898 729.0 627 5196
Total: 1198 2255 888.0 1995 7426

Percentage of the requests served within a certain time (ms)
50% 1995
66% 2085
75% 2213
80% 2444
90% 3755
95% 4238
98% 5119
99% 5437
100% 7426 (longest request)

内存影响(4 个工作线程,5 个线程)

source=web.1 dyno=heroku.1234567890 sample#memory_total=406.75MB sample#memory_rss=406.74MB sample#memory_cache=0.00MB sample#memory_swap=0.00MB sample#memory_pgpgin=151515pages sample#memory_pgpgout=47388pages

根据上面的代码片段,有时 Puma 部署会比 Webbrick 更快,而其他时候可能会更慢(如代码片段所示)。即使快很多,速度也并不显着,可能只会增加 1-5 个请求/秒。

我的问题是,我做错了什么?我的数据库池有问题吗?我是否错误地对其进行了基准测试?我是否错误地使用了 Puma?

编辑:

Puma 的最高 CPU 负载(每个 5 个工作线程和 5 个线程)

source=web.1 dyno=heroku.123456789 sample#load_avg_1m=2.98

然而,大多数时候,它要么是 0.00 要么小于 0.1。

最重要的是,在 Controller 中调用的唯一代码是:

@package = Package.all

紧随其后,呈现在 HAML 中声明的 JSON 响应。

顺便说一句,Package.all 只返回大约 5 条记录。

编辑 2:

unicorn 结果

根据 .运行 3 个 unicorn worker 。

Server Software:        Cowboy
Server Hostname: unicorn-build.herokuapp.com
Server Port: 443
SSL/TLS Protocol: TLSv1,DHE-RSA-AES128-SHA,2048,128

Document Path: /api/v1/packages?access_token=f73f50514c6b8a3ea
Document Length: 488 bytes

Concurrency Level: 100
Time taken for tests: 22.311 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 942000 bytes
HTML transferred: 488000 bytes
Requests per second: 44.82 [#/sec] (mean)
Time per request: 2231.135 [ms] (mean)
Time per request: 22.311 [ms] (mean, across all concurrent requests)
Transfer rate: 41.23 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 846 1326 294.5 1304 2720
Processing: 245 627 342.8 540 3061
Waiting: 244 532 313.6 470 3057
Total: 1232 1954 463.0 1874 4875

Percentage of the requests served within a certain time (ms)
50% 1874
66% 2016
75% 2161
80% 2250
90% 2466
95% 2799
98% 3137
99% 3901
100% 4875 (longest request)

我注意到的一件事是多次运行相同的 ab 负载测试代码将返回不同的“每秒请求数”。这适用于 Unicorn 和 Puma。对于 Unicorn 和 Puma,最好的“每秒请求数”约为 48-50,而最差的约为 25-33。

不管怎样,它仍然没有意义。为什么 Puma 或 Unicorn 都没有击败 Webbrick?

最佳答案

我相信您已经关注了 Heroku 的 Deploying Rails Applications with the Puma Web Server彻底指导。

我的猜测是您的测试环境将多线程优势降到最低,或者 HTTP 服务器被 SQL 数据库瓶颈。

您的 API 调用,尤其是当它们缓存数据库结果时,可能会占用大量 CPU。当 CPU 仅被 1 个 100% 使用时,拥有 10 个线程没有优势。在这种情况下,管理线程实际上会阻碍性能。

当您的工作线程长时间等待资源(数据库、文件等)而不是使用 CPU 时,多线程很有用。

第二种可能是你的HTTP服务器受数据库限制。可能是 WEBrick 的移动速度与数据库允许的速度一样快,没有通过切换到性能更好的 HTTP 服务器来改进的余地。

你应该给 this综合基准​​报告一读。

您会注意到 Puma 并不是最快的 Rails HTTP 服务器之一。如果您只关心速度,请尝试 Unicorn , 或 Torquebox 4如果使用 JRuby

这是一个 guide关于如何在 Heroku 上设置 Unicorn。

关于ruby-on-rails - Puma 或 Unicorn VS Webbrick 负载测试基准显示没有改善,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35036073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com