Skip to content

Instantly share code, notes, and snippets.

@lporras
Forked from mikeauclair/ProfilingGuide.md
Last active August 7, 2019 16:20
Show Gist options
  • Save lporras/ec5929b3bc0d9c7df5e9a52abc129161 to your computer and use it in GitHub Desktop.
Save lporras/ec5929b3bc0d9c7df5e9a52abc129161 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
RubyProf::CallTreePrinter.new(results).print({print_file: true})
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
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