已更新:好的,问题已解决。
启动服务器的方式不同,结果也不同。
# 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的方法:
-------------------原始问题--------------------
今天,我正在为API应用程序编码Sinatra,发现:
经典sinatra代码可以处理:
1.1 1800请求/秒,服务器为瘦服务器。
1.2 2000次请求/秒,以puma作为服务器。
模块化sinatra代码只能处理:
2.1 1100个请求/秒,服务器为瘦
2.2 800个请求/秒,以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
# 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
运行:
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似乎不如预期的好。有人可以给我一个有关如何使用“经典Sinatra”或“模块化Sinatra”的线索吗?
如果我不使用模块化Sinatra,如何为大型应用程序编写代码?
非常感谢!
答案 0 :(得分:2)
您的基准测试不正确。
根据您发布的摘录,在第一种情况下,您将Thin用作服务器,在第二种情况下,将Puma用作服务器。
这些服务器实现了完全不同的并发模型:据我所知,前者是一个单线程事件循环,后者是多个线程。结果,Thin在轻型非阻塞任务上表现更好,而Puma在计算量较大或阻塞的情况下胜过它。
您的虚拟示例更适合Thin的模型,这会导致差异……模块化Sinatra应该绝对不错,只需将苹果与苹果进行比较即可:)
答案 1 :(得分:1)
好的,我找到了根本原因,在锡曼的建议下,我在这里发布了答案。
问题是“ Web服务器”,而不是“框架”
启动服务器的方式不同,结果也不同。
# 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: