对于Ruby on Rails,使用Webrick时,是否支持并发请求和Keep-Alive,为什么加载.js和.png文件这么慢?

发布于 2024-10-26 10:35:23 字数 714 浏览 6 评论 0原文

我正在运行 Rails 3.0.5,控制台上报告一个页面需要 60 毫秒,但如果我检查 Firefox Net 加载时间图表,需要 2.9 秒。如果我在 Bash 上运行 ab 命令,它会显示需要 300 毫秒。

因此,如果删除支架 javascript(其中 6 个),则需要 1.9 秒......但我想知道为什么这么慢?活着不是很荣幸吗?

同样奇怪的是 Firefox 显示 4 个文件正在同时下载——我以为 Webrick 一次只支持 1 个连接?

(更改为使用 mongrel 或“thin”会使事情有所不同或更好吗?)

同样奇怪的是,如果我

ab -n 10 -c 5 http://www.somesite.com:8080

需要 3 秒,并且为了测试如何支持 keep-alive,我使用了 < code>-k 选项:

ab -n 10 -c 5 -k http://www.somesite.com:8080

但现在总时间从 3 秒变为 4.5 秒。 keep-alive 不是应该让它更快吗?Webrick 支持 keep-alive 吗?

另外,如果它支持并发连接,那么如果某些代码使用类变量来处理事物,那么不会发生竞争条件吗? (因为类变量内容跨请求保留)

I am running Rails 3.0.5, and a page is reported on the console that it take 60ms, but if I check the Firefox Net load time chart, it takes 2.9 seconds. If I run the ab command on Bash, it says it take 300ms.

So if remove the stand javascripts (6 of them), then it takes 1.9 seconds... but I wonder why so slow? Isn't keep-alive honored?

Also strange is Firefox shows that 4 files are downloading concurrently -- I thought Webrick supports only 1 connection as a time?

(Will changing to using mongrel or "thin" make things any different or better?)

also strange is that if I

ab -n 10 -c 5 http://www.somesite.com:8080

it takes 3 seconds, and to test how keep-alive is supported, I used the -k option:

ab -n 10 -c 5 -k http://www.somesite.com:8080

but now the total time changes from 3 seconds to 4.5 seconds. Isn't keep-alive supposed to make it faster, and is keep-alive supported by Webrick?

Also, if it supports concurrent connection, then if some code uses a class variable to handle things, then can't there be race condition happening? (since class variable content stays across requests)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半城柳色半声笛 2024-11-02 10:35:23

这并不能完全回答你的问题,但我会给你一些建议,这可能会让你的问题变得无关紧要。

Webrick 不应该在生产中使用。它是用纯 Ruby 编写的,一次只能处理一个请求。它不能用于开发模式之外的任何用途。

对于生产,您希望在反向代理(如 nginx)后面使用 Thin 的多个实例,或者您可以使用为您执行此操作的乘客(这是现代大多数人使用的)。


部分回答您的问题:

ab 运行速度比 Firefox 快的原因是因为请求 css 和 javascript 文件是浏览器的一项功能。 ab 仅对服务器的响应时间进行基准测试,不包括发送任何图像、CSS 或 js。

Rails 基准测试只有 60ms 的原因是它只测量它在 Rails 堆栈中的时间。它不计算将请求发送回用户所需的时间。

因为 webrick 不是为生产而设计的,所以如果不支持 keep-alive,我也不会感到惊讶。

This doesn't exactly answer your question, but I'm going to give you advice that probably makes your question irrelevant.

Webrick should not be used in production. It is written in pure ruby and can only process one request at a time. It is not made to be used in anything outside of development mode.

For production, you want to use multiple instances of thin behind a reverse proxy like nginx, or you can use passenger which does this for you (and is what most people use in the modern day).


To partially answer your questions:

The reason ab is running faster than firefox is because requesting css and javascript files are a function of the browser. ab is only benchmarking the response time from the server, which doesn't include sending down any images, css, or js.

The reason the rails benchmark is only 60ms is because it only measures the time it was in the rails stack. It does not count the time it takes to send the request back to the user.

Because webrick isn't made for production, it wouldn't surprise me if keep-alive is not supported.

摘星┃星的人 2024-11-02 10:35:23

如果您希望在本地两个环境中运行相同的应用程序,只需在databases.yml中添加另一个条目,如下所示:

dev2:
适配器:mysql2
数据库: 数据库
用户名: 姓名
密码:wpord!
主机:mysql.myexample.com
泳池:5
timeout: 5000

然后运行rails server -e dev2 --port 3001

这对我有用。也就是说,在本地运行的同一个应用程序连接到本地和远程数据库。

If you are looking to run the same app in two environments locally, just make you another entry in databases.yml like so:

dev2:
adapter: mysql2
database: db
username: name
password: wpord!
host: mysql.myexample.com
pool: 5
timeout: 5000

and then run rails server -e dev2 --port 3001

That works for me. Namely, the same app, run locally, connects to a local AND remote db.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文