Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment