gpt4 book ai didi

ruby - 经典sinatra和模块化sinatra的性能?我做错了吗?

转载 作者:数据小太阳 更新时间:2023-10-29 08:42:26 26 4
gpt4 key购买 nike

更新:好的,问题已解决。

我启动服务器的不同方式会产生不同的结果。

# this gives 2800 req/s in a production server, server based on thin
$ bundle exec thin start -R config.ru -e production

# this gives 1600 req/s in the same server, server based on Rack( seems that)
$ bundle exec rackup config.ru -s thin

所以启动sinatra的方式:

  1. 错误:$ ruby​​ main.rb(基于机架?)
  2. 错误:$ rackup config.ru(基于机架)
  3. 错误:$ rackup config.ru -s thin(基于机架的事件)
  4. 正确:$ thin start -R config.ru -e production

----------------原始问题--------------------

今天我正在为 API 应用程序编写 Sinatra 代码,发现:

  1. 经典sinatra代码可以处理:

    1.1 1800请求/s,以瘦为服务器。

    1.2 2000 request/s,服务器为puma。

  2. 模块化的sinatra代码只能处理:

    2.1 1100 requests/s,以瘦为服务器

    2.2 800 requests/s , 以puma为服务器

如何重现:

经典西纳特拉

# test_classic_sinatra.rb
require 'sinatra'
get '/' do
'hihihi'
end

运行:

siwei $ ruby test.rb 
== Sinatra (v2.0.5) has taken the stage on 4567 for development with backup from Thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on localhost:4567, CTRL+C to stop

测试:

$ ab -n 1000 -c 100 http://localhost:4567/

得到结果:

Concurrency Level:      100
Time taken for tests: 0.530 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 211000 bytes
HTML transferred: 6000 bytes
Requests per second: 1885.43 [#/sec] (mean)
Time per request: 53.038 [ms] (mean)
Time per request: 0.530 [ms] (mean, across all concurrent requests)
Transfer rate: 388.50 [Kbytes/sec] received

模块化 sinatra:

# config.ru
require 'sinatra/base'

class App < Sinatra::Application
set :environment, :production
get '/' do
'hihihi'
end
end

run App

以瘦为服务器

运行:

$ rackup config.ru -s thin
Thin web server (v1.7.2 codename Bachmanity)
Maximum connections set to 1024
Listening on localhost:9292, CTRL+C to stop

测试:

Concurrency Level:      100
Time taken for tests: 0.931 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 211000 bytes
HTML transferred: 6000 bytes
Requests per second: 1073.58 [#/sec] (mean)
Time per request: 93.146 [ms] (mean)
Time per request: 0.931 [ms] (mean, across all concurrent requests)
Transfer rate: 221.22 [Kbytes/sec] received

以 puma 作为服务器

运行:

siwei$ rackup config.ru 
Puma starting in single mode...
* Version 3.11.4 (ruby 2.3.8-p459), codename: Love Song
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://localhost:9292
Use Ctrl-C to stop

测试:

$ab -n 1000 -c 100 http://localhost:9292/

得到结果:

Concurrency Level:      100
Time taken for tests: 1.266 seconds
Complete requests: 1000
Failed requests: 0
Total transferred: 178000 bytes
HTML transferred: 6000 bytes
Requests per second: 789.62 [#/sec] (mean)
Time per request: 126.643 [ms] (mean)
Time per request: 1.266 [ms] (mean, across all concurrent requests)
Transfer rate: 137.26 [Kbytes/sec] received

在决定使用Sinatra之前,我阅读了很多关于“sinatra、grape和rails api”的帖子,并针对这些框架进行了测试,最终决定使用Sinatra。

但是现在,我发现Modular Sinatra似乎并没有想象的那么好。有人能告诉我如何使用“Classic Sinatra”或“Modular Sinatra”吗?

如果我不使用 Modular Sinatra,如何为大型应用程序编写代码?

非常感谢!

最佳答案

您的基准不正确。

根据您发布的片段,在第一种情况下您使用 Thin 作为服务器,在第二种情况下使用 Puma。

这些服务器实现了完全不同的并发模型:据我所知,前者是单线程事件循环,后者是多线程。因此,Thin 在轻型非阻塞任务中表现更好,而 Puma 在计算量相对较大或阻塞的场景中表现更好。

你的虚拟示例更适合 Thin 的模型,这导致了差异......模块化 Sinatra 应该绝对没问题,只是比较苹果和苹果:)

关于ruby - 经典sinatra和模块化sinatra的性能?我做错了吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56422645/

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