Skip to content

Instantly share code, notes, and snippets.

@MischaTheEvil
Forked from hiltmon/development_profiler.rb
Last active December 31, 2015 03:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save MischaTheEvil/7930996 to your computer and use it in GitHub Desktop.
Save MischaTheEvil/7930996 to your computer and use it in GitHub Desktop.
A helper class to support easy ruby-prof profiling in Rails applications.
# encoding: utf-8
#
# Copyright (c) 2012 Hilton Lipschitz
# Copyright (c) 2013 Mischa The Evil
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# Class to enable easy ruby-prof profiling in Ruby on Rails applications.
# This class derived from: https://gist.github.com/hiltmon/1929287
# For more info see:
# - http://hiltmon.com/blog/2012/02/27/quick-and-dirty-rails-performance-profiling/
# - https://github.com/ruby-prof/ruby-prof
# @license: MIT license
# @author: Hilton Lipschitz (original author)
# @author: Mischa The Evil
#
class DevelopmentProfiler
# Define a class method prof(file_name) that can be used throughout the Rails
# application using DevelopmentProfiler::prof("file_name") do ... end blocks.
def self.prof(file_name)
RubyProf.start
yield
results = RubyProf.stop
# Print a graph profile to text
File.open "#{Rails.root}/tmp/performance/#{file_name}-graph.txt", 'w' do |file|
RubyProf::GraphPrinter.new(results).print(file)
end
# Print a graph profile to html
File.open "#{Rails.root}/tmp/performance/#{file_name}-graph.html", 'w' do |file|
RubyProf::GraphHtmlPrinter.new(results).print(file)
end
# Print a flat profile with line numbers to text
File.open "#{Rails.root}/tmp/performance/#{file_name}-flat.txt", 'w' do |file|
# RubyProf::FlatPrinter.new(results).print(file)
RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
end
# Print a callstack profile to html
File.open "#{Rails.root}/tmp/performance/#{file_name}-stack.html", 'w' do |file|
RubyProf::CallStackPrinter.new(results).print(file)
end
# Print a calltree profile to text
File.open "#{Rails.root}/tmp/performance/#{file_name}-tree.txt", 'w' do |file|
RubyProf::CallTreePrinter.new(results).print(file)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment