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

Performance of classic sinatra and modular sinatra? am I doing wrong?

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

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

# 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. 错误:$rubymain.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 request/s,以瘦为服务器。

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

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

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

    2.2 800 requests/s , puma 作为服务器.

如何重现:

经典 sinatra

# 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 and rails api"的帖子,并且我运行测试了这些框架,最终决定使用Sinatra。

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

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

非常感谢!

您的基准不正确。

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

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

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

好的,我找到了根本原因,在铁皮人的建议下,我post在这里回答。

问题是 "web server" ,而不是 "framework"

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

# 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
so the ways of starting sinatra:
  • 错误:$rubymain.rb(基于机架?)
  • 错误:$ rackup config.ru(基于机架)
  • 错误:$ rackup config.ru -s thin(基于机架的事件)
  • 正确:$ thin start -R config.ru -e production