Skip to content

Instantly share code, notes, and snippets.

@mikeauclair
Last active August 7, 2019 16:19
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save mikeauclair/6225917 to your computer and use it in GitHub Desktop.
Profiling
class DevelopmentProfiler
def self.prof(file_name)
if Rails.env.development?
RubyProf.start
yield
results = RubyProf.stop
File.open "#{Rails.root}/tmp/performance/callgrind.#{file_name}.clt", 'w' do |file|
RubyProf::CallTreePrinter.new(results).print(file)
end
else
yield
end
end
end
DevelopmentProfiler.prof('junk') do
Shitty.new(45000)
end
DevelopmentProfiler.prof('better') do
Better.new(45000)
end

Easy steps for CPU profiling a Rails app

  1. add ruby-prof to your gemfile (https://github.com/ruby-prof/ruby-prof)
  2. grab kcachegrind or qcachegrind (brew install qcachegrind graphviz on os x)
  3. plop development_profiler.rb in lib in your app
  4. wrap your questionable code in a prof block
  5. open the file in qcachegrind (It'll live in tmp/performance in your app)
class Shitty
def initialize(some_int)
@symbols = (0..some_int).map(&:to_s).map(&:to_sym)
end
end
class Better
def initialize(some_int)
@symbols = (0..some_int).map{|i| i.to_s.to_sym}
end
end
@mikeauclair
Copy link
Author

I'd give opening the files it generates that way a shot in qcachegrind - I haven't written much Ruby in the past ~4 years, so I wouldn't be shocked if RubyProf's API changed in the intervening time

hi @mikeauclair thanks for this gist, but i'm getting this error:

ArgumentError: RubyProf::CallTreePrinter#print cannot print to IO objects
from /Users/luisalfredoporraspaez/.rvm/gems/ruby-2.3.4@webdox/gems/ruby-prof-0.18.0/lib/ruby-prof/printers/call_tree_printer.rb:55:in `validate_print_params'

I found that I can change it with:

RubyProf::CallTreePrinter.new(results).print({print_file: true})

But it creates some files in my directory, example:

profile.callgrind.out.78859
profile.callgrind.out.78859.70346902832000
profile.callgrind.out.78859.70346902838880

I see that they don't have the extension .clt...

@lporras
Copy link

lporras commented Aug 7, 2019

@mikeauclair I could open the file in qcachegrind, I have an example in this gist: https://gist.github.com/lporras/317d25bb0641ad3426851ea06eb8c3ea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment