Paluy on Miscellaneous

Of course I'm an optimist - I don't see much point in being anything else. - Winston Churchill

Debugging Rails Application - Part 1

How to debug Rails application?

There are several gems to debug your Ruby code:

debugger - A fork of ruby-debug that works on Ruby 1.9.3

Install (rvm):

1
gem install debugger -- --with-ruby-include=$rvm_path/src/ruby-1.9.3-p374/
Gemfile
1
2
3
  group :development, :test do
    gem 'debugger'
  end

Usage:

1
2
  # Wherever you need a debugger, simply add:
  require 'debugger'; debugger

pry-debugger - Using debugger within pry

Gemfile
1
2
3
  group :development, :test do
    gem 'pry-debugger'
  end

Configuration (Optional):

Add aliases:

$HOME/.pryrc
1
2
3
4
  Pry.commands.alias_command 'con', 'continue'
  Pry.commands.alias_command 's', 'step'
  Pry.commands.alias_command 'n', 'next'
  Pry.commands.alias_command 'fin', 'finish'

NOTE: You won’t be able to use those words as params while debugging

Usage:

1
2
  # Wherever you need a debugger, simply add:
  binding.pry

Example:

home_controller.rb
1
2
3
4
5
6
7
8
9
  class HomeController < ApplicationController
    def index
      # Some code ...

      binding.pry
      # Code to debug
      # ...
    end
  end

Now start your rails server (rails s) and visit your controller url: http://localhost:3000/home. The pry debugger console will appear in your terminal. You can use all pry functionality inside your application in addition to predefined aliases(next, step, continue, etc.).

Other useful commands:

  • help - show a list of available commands
  • exit - exit a context with a user provided value
  • whereami - describe the current location (alternative: $)
  • break - set a breakpoint
  • wtf -v - see the entire backtrace

Improve Rails Bootup Time

Guide: How to improve your Rails Bootup Time

Benchmarks:

I used ruby-1.9.3-p327 with and without patch.

The test is:
1
2
echo puts Time.now > test_script
  time rails runner test_script
The results are:
  • ruby-1.9.3-p327: rails runner test_script 16.01s user 0.61s system 99% cpu 16.725 total
  • ruby-1.9.3-p327-fast_load: rails runner test_script 6.11s user 0.55s system 98% cpu 6.732 total

How to apply:

Download fast_load.patch

Than, install the patch:

1
rvm install 1.9.3 --patch fast_load.patch -n fast_load

Credentials

Thanks to @romanbsd for sending me this patch

Faster Hacking!!!

Update

I found the origin of this patch. It’s here and here It’s a part of falcon patch but only for fast require

Optimize Your RVM Ruby

TL;DR

If you are using rvm with ruby-1.9.3 patch < 327, reinstall it with cflags.patch.

I would like to share my experience and summarize this issue.

There are several posts, regarding Ruby compiling optimization while using RVM, like:

First you should understand what are Optimization Options.

O3 : turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload, -ftree-vectorize, -fvect-cost-model, -ftree-partial-pre and -fipa-cp-clone options.

I followed the RVM issue discussion at Github. It suggested to apply CFLAG.patch for all Ruby versions till ruby-1.9.3-p327.

NOTE: 1.9.3-p327 does not require it, rvm uses a new feature with allowing multiple paths in –with-opt-dir= - so no CFLAGS / LDFLAGS are used.

How to apply:

1
rvm reinstall 1.9.3 --patch cflags.patch

Is it really improves ruby benchmark? Yes.

See my results (benchmark.rb):

ruby-1.9.3-p286 without patch

1
2
3
4
./benchmark
       user     system      total        real
+=  46.020000   2.610000  48.630000 ( 48.827064)
<<   0.220000   0.000000   0.220000 (  0.224581)

ruby-1.9.3-p286 with patch

1
2
3
4
./benchmark
       user     system      total        real
+=  42.830000   2.480000  45.310000 ( 45.521011)
<<   0.100000   0.000000   0.100000 (  0.099425)

Is it good for production?

We use this patch in production with ruby-1.9.3-p286 for more than a month. Everything is stable, and we see significant improve in our application.

Summary

If you are using rvm with ruby-1.9.3 patch < 327, reinstall it with cflags.patch.

REST Debug Tips

If you need to debug Net protocol between your application and some other API, you can output all request, this way:

class Net::HTTP
  class << self
    def new_with_debug(*args)
      new_without_debug(*args).tap { |http| http.set_debug_output(STDERR) }
    end
    alias_method_chain :new, :debug
  end
end

Another “Monkey patch” for RestClient so it will always provide authentication:

require 'restclient'
module RestClient
  class Request
    def execute_with_auth(&block)
      @user ||= 'someuser'
      @pass ||= 'password'
      execute_without_auth(&block)
    end
    alias_method_chain :execute, :auth
  end
end

Thanks to @romanbsd for providing those tips.

Passwordless Login

I noticed that some developers didn’t know about Passwordless Login

On your local computer, you must have an RSA public/private keys. Otherwise generate it:

ssh-keygen -t rsa

One row solution is here:

cat .ssh/id_rsa.pub | ssh user@example.com 'cat >> .ssh/authorized_keys'

Otherwise step-by-step explanation:

Copy your public key to your example.com

scp ~/.ssh/id_rsa.pub user@example.com:~/

SSH to your server

ssh user@example.com

Now, append the public key to your authorized keys file and delete the file you uploaded:

mkdir .ssh
cat id_rsa.pub >> .ssh/authorized_keys
rm id_rsa.pub

Make sure your permissions set properly:

chmod go-w ~
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys