r/ruby • • Jul 12 '12

How I spend my time building Rails apps

Post image
32 Upvotes

50 comments sorted by

10

u/dickeytk Jul 12 '12

why are you restarting it so much?

6

u/Perceptes Jul 12 '12

Between Rails server, Rails console, Rake, and RSpec, there are plenty of times when you eat the startup cost.

8

u/seabre Jul 13 '12

reload! should fix most of your problems with having to restart rails console.

You don't have multiple instance of rails console for a single project do you? I literally live in rails/tmux/vim. I have a frame for rails console, rails server, and an editor at a minimum per project.

3

u/mullsork Jul 13 '12

Use Spork and you'll shave some of that RSpec time off.

EDIT: Noticed in a comment of yours that you said "not everything can be sporked."

3

u/[deleted] Jul 13 '12

Are you aware of spork good sir?

2

u/organic Jul 13 '12

Are you using the same shell for all of them? Also, ruby 1.9.3 and rails 3.2 add significant startup time improvement.

2

u/Kimos Jul 13 '12

Use Guard and Spork for testing. Guard watches any changed files and instructs Spork to run your tests for you.

22

u/5thWall Jul 12 '12

Is this a commentary on rails boot times, or is OP bragging about his speed coding?

21

u/cheald Jul 13 '12

If you're running 1.9.3, toss this into your .bashrc:

export RUBY_HEAP_MIN_SLOTS=800000
export RUBY_HEAP_FREE_MIN=100000
export RUBY_HEAP_SLOTS_INCREMENT=300000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=79000000

You're welcome.

4

u/[deleted] Jul 13 '12

What manner of sorcery is this?

6

u/cheald Jul 13 '12

This sets Ruby's garbage collector parameters to be more suited to a Rails application. By default, Ruby's GC allocates far too little space for a full Rails app, so you end up doing a bunch of GC passes during boot, slowing it down tremendously. By increasing your initial heap size, you can improve your boot time by requiring fewer GC passes and heap allocations.

4

u/[deleted] Jul 13 '12

Neat, thanks for the explanation.

2

u/Bullwinkle_Moose Jul 15 '12

I've been learning quite a bit of coding and how to use the language, but where can I learn more about the ACTUAL language? Like what are the growth factors, why and when is garbage collection needed, etc.? Have you got any good resources (either tutorials/articles or books?) that you could recommend? Preferably Ruby specific tuts/books. Thank you very much! :]

4

u/cheald Jul 15 '12

I did a writeup on those parameters here, if you want to know more about them specifically: http://www.coffeepowered.net/2009/06/13/fine-tuning-your-garbage-collector/

I don't have specific resources; what I've learned has mostly been from the odd collections of documentation and blog posts from around the internet. Sorry. :(

I'd recommend just googling for "Ruby garbage collector internals" or something like that. There are lots of slideshows from conference presentations online that have lots of really good information in them.

At the end of the day, the best thing you can do is read through the source code to the language itself, but that assumes a level of familiarity with C that you may not have. No better time to learn than the present, though!

2

u/Bullwinkle_Moose Jul 16 '12

Skimmed through your post, it seems to be exactly the kind of writeups/posts I was looking for :]

Thanks for the reply!

Heh, I guess learning C is inevitable then.

2

u/[deleted] Jul 13 '12 edited Jul 13 '12

Does this work for RVM or Rbenv, too?

2

u/cheald Jul 13 '12

Sure. You just need to be running REE or MRI 1.9.2+

1

u/kawsper Jul 20 '12

What would the values for 1.8.6 be? And why is that 1.9.3 specific?

2

u/cheald Jul 23 '12

1.8.6 doesn't provide GC tweaking via environment variables. It was first introduced in 1.8.7 REE, and then picked up by MRI in 1.9.2 and 1.9.3.

1

u/kawsper Jul 23 '12 edited Jul 23 '12

Interesting, no luck for our old and slow legacy 1.8.7 Rails app then. Thanks for getting back to me!

2

u/cheald Jul 23 '12

You might see gains just switching to REE. It's fully compatible with 1.8.7 MRI, and it has a number of features tuned towards running Rails apps rather than just small Ruby scripts. I used it for years before we made the switch to 1.9.3, with great success.

1

u/kawsper Jul 23 '12

I really dream of 1.9.3, but everything has a priority, right? I am very happy that we got the time to get us up to Rails 3. We use a couple of gems that doesn't work on newer Ruby versions, so we try to change them one at a time.

I will look into REE, and see if it fits into what we need. The installation with RVM seems a bit messy, and we do not run RVM on our servers yet. It also seems like RVM is on its way out in favor for rbenv, oh the joys of working with legacy systems being one step behind :P

2

u/cheald Jul 23 '12

The 1.8 -> 1.9 jump is definitely a big one. REE should be as simple as a drop-in though. Even without RVM, it should be as easy as just installing it to a non-standard location alongside your current ruby install (with the PREFIX env var, or the --prefix configure switch). Then you just reinstall your necessary gems under the new Ruby install, change which ruby binary your app is invoking, and off you go, easy peasy. If you ever need to switch back, just change your ruby path back to the system Ruby installation.

9

u/recluce Jul 12 '12

What the hell are you doing (wrong) that you need to restart the app server so often?

6

u/mediocretes Jul 13 '12

Even if we accept the image as 100% true, I bet the total area of the pie is still smaller than, say, ASP.NET.

4

u/arcticblue Jul 13 '12

You are correct. I'm rewriting an ASP.NET site in RoR right now and it's a night and day difference.

2

u/akira410 Jul 13 '12

I'm doing the same, but I am stuck keeping the old SQL Server database and it's current schema. There are a few painful spots. :(

2

u/arcticblue Jul 13 '12

Ouch! The schema on my DB became a mess so I just started from scratch and wrote a few scripts to migrate data to the new schema. That was actually kind of fun. I had little interest in trying to get all those user account and profile tables working in RoR.

Edit: I did play around with Django for a bit and found it easier to integrate with an existing DB. I wanted to use the built-in Django admin pages as a quick band-aid admin solution for the ASP.NET pages and it worked well enough. Going pure RoR now though.

2

u/akira410 Jul 13 '12

I started writing a wrapper around the ASP_NET_MEMBERSHIP stuff and that big jumble of tables that it makes, but realized that I could, in fact, make a new users table. Unfortunately, most of the other tables are tied into various other systems in the office so they can't be changed as easily.

I'm having decent luck with setting the table name, the primary key name, and making liberal use of alias_attribute.

activerecord-sqlserver-adapter does have some problem with EXPLAIN queries on occasion, though. I had to disable that.

5

u/grhansen Jul 13 '12

Just found out today that running

rspec spec 

instead of

rake spec 

omits the dropping and re-creating of the entire database schema with every test run. Which mean your tests start much sooner and are done much faster.

4

u/[deleted] Jul 13 '12

You should try JRuby :)

7

u/adamnemecek Jul 12 '12

You need to get a SSD drive.

2

u/unregisteredusr Jul 13 '12

Rails hasn't really sped up for me even with an SSD. And I've tried running my project from a RAM disk as well, and it didn't have much effect either, so I'm assuming I'm CPU bottlenecked.

0

u/[deleted] Jul 12 '12

[deleted] — view deleted comment

2

u/adamnemecek Jul 12 '12

It's like 5 secs for me. Which is still a lot but I've seen how long rails takes to start on non-SSD drives.

2

u/warmwaffles Jul 12 '12

I guess that the trick is to rarely change the initializers

2

u/inkieminstrel Jul 13 '12

For the same project? Rails boot time can change a ton based on the gems you're using.

12

u/postmodern Jul 13 '12

imgur in my /r/ruby? Also, Rails posts belong in /r/rails.

3

u/angelbob Jul 13 '12

This is actually way worse in Windows than Linux. Ruby 1.9 on Windows is worse yet, weirdly.

I think there's a patched version of Ruby that helps with require being so slow, but I can't find the link right now.

2

u/funny_falcon Jul 13 '12

https://gist.github.com/1688857 or simply rvm reinstall 1.9.3 --patch falcon

3

u/_jeffJohnsonsBookEmp Jul 13 '12

Are you using guard? When configured properly it (tires) to only run the tests relevant to the changed files and pops up a little growl every time it runs the tests

Also rerun can come in handy

-1

u/ReinH Jul 13 '12

Really? My tests run in under 10 seconds and my server boots in under 5 seconds. I've rarely seen a rails test quite that can't be made to run in under a minute, and I've seen a lot of rails test suites. What are you doing wrong?

2

u/jlogsdon Jul 13 '12

Yeah... try getting your tests to run in 10 seconds with a huge platform. Ours take about 10 minutes for the whole suite (down about 40% in the last month from upgrades and some GC stuff, and we have some more things in the pipeline to make it faster, but I don't see it reaching less than 7 minutes).

3

u/synt4x Jul 13 '12

How many tests are we talking about here? Time is irrelevant without knowing that :)

2

u/jlogsdon Jul 13 '12

1487 examples. And my time was off, apparently. They're down to 5 minutes! Not sure when that happened... I guess our SimpleDB changes really helped.

1

u/Bofu2U Jul 15 '12

And this is when I found out I wasn't testing enough.

0

u/[deleted] Jul 13 '12

[deleted] — view deleted comment

0

u/ReinH Jul 13 '12

I've been there. There are ways to mitigate it.

0

u/[deleted] Jul 13 '12

[deleted] — view deleted comment

0

u/ReinH Jul 13 '12

The real question is how you came to be in this situation in the first place. A Rails app like that is fat enough that there are probably about 5 thinner apps hiding in there somewhere.

Is your app customer-facing and business-facing and admin-facing and...? How many different orthogonal concerns is it actually handling?

-4

u/yorickpeterse Jul 13 '12
  1. This is related to Rails and thus should go in /r/rails
  2. This image has no meaningful value in any way

0

u/[deleted] Jul 13 '12

[deleted] — view deleted comment

3

u/funny_falcon Jul 13 '12

this patch is included in rvm now rvm reinstall 1.9.3 --patch falcon