For Ruby on Rails, when using Webrick, does it sup

2019-04-14 11:50发布

问题:

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)

回答1:

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.



回答2:

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.