简体   繁体   中英

Why WEBrick server is faster in production mode rather in development mode? + Rails

I have been developing ruby on rails application since some couple of months. I use the default WEBrick server to run the applications. And I found that when I start the WEBrick server in the development and production modes, the server works more speed for production mode than for the development mode .

Is there any specific reason behind that? Can anybody explain me?

In dev mode classes are not cached, so Rails reloads all the classes each time you refresh. Also, asset compilation is not done in development (by default), so Rails reloads all the assets (CSS, Javascript etc) each time you refresh.

In production mode, a server loads code into the cache, which makes things quick. However, that's not the case in development mode (since you don't want to restart your webrick every time you made a change). Every request loads the according code again, which takes a bit of time.

And the most of all time-eaters is the asset pipeline. In production, you get a compiled version of your assets (javascripts and css) in maybe one or two requests. In development, you get them split, for debugging purpose (based on your environment settings, of course). And because a browser does not handle all requests simultaneously, some assets are loaded after other ones are finished loading. You can watch this behaviour with eg firebug's network console. That means: the more assets you have, the longer your page takes to load in development mode.

The difference is between 2 environments. In Rails, there are several environment. Each has his own database configuration and Rails options.

You can use the Rails.env variable to made some different change with particular environment.

By default, the development environment is without all cache and activate the auto-reloading. The production environment is with all cache.

But if you want you can make a production environment like development or development environment like production.

You can add some new specific environment too.

Creating new Environment:

Assuming you want create the hudson environment.

Create a new environment file in config/environments/hudson.rb. 

You can start by cloning an existing one, for instance config/environments/test.rb. Add a new configuration block in config/database.yml for your environment. That's all.

Now you can start the server

ruby script/server -e hudson

Run the console

ruby script/server hudson

And so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM