sabato 12 aprile 2014

[Ruby] Memory profiling of a rake task with Ruby 1.9.3

These are only some notes to remember what I did to do a memory profiling of a rake task.

The environment was Ruby 1.9.3 with a Rails application and a Rake task to run in order to import the data in the DB.

The Rake task was just a wrapper for an Importer Object Class.

First add ruby-prof to the Gemfile:

gem "ruby-prof"

Then require it into the file you want to profile, and start to profile the task you want to track:

 require 'ruby-prof'
 
 def my_method()
   # We want to profile the Memory rather than Process time

   RubyProf.measure_mode = RubyProf::MEMORY
   # Start profile!
   RubyProf.start
  
   ... do your crazy stuff here...

   # Stop profiling and get the result
   result = RubyProf.end

   # Write the result in somewhere
   RubyProf.MultiPrinter.new(result).print { :path => "#{Rails.root}/", :profile => "profiling" }
 end

Easy, right?
Yeah, too much, in fact opening the profile.html the result will be a full stack of NaN... indeed have a look to this issue for 1.9.3 in ruby-prof.

So, what to do is to use RVM to install a patched version of 1.9.3 and try again!

$ rvm install 1.9.3-p194 --patch railsexpress

Go and make a tea while waiting for building...

At this point create a new gemset, bundle install and have another go with the profiling: this time it should work!

Nessun commento:

Posta un commento