Skip to content

Instantly share code, notes, and snippets.

@matthutchinson
Created March 15, 2012 09:42
Show Gist options
  • Save matthutchinson/2043296 to your computer and use it in GitHub Desktop.
Save matthutchinson/2043296 to your computer and use it in GitHub Desktop.
Profiling with ruby-prof
require 'rubygems'
require 'ruby-prof'
def slideview_image_url(photo)
if CDN_OPTIONS && CDN_OPTIONS[:s3_host_alias]
hosts = Array.wrap(CDN_OPTIONS[:s3_host_alias])
host = hosts[photo.id % hosts.size]
"http://#{host}/photos/images/#{photo.id}/search2.jpg"
else
"/system/images/#{photo.id}/search2/#{photo.image_file_name}"
end
end
def property_search_images_json_for_slideview(properties)
properties.collect_hash do |property|
photos = property.photos.sort! { |a,b| (a.default_photo == b.default_photo) ? 1 : -1 }
image_urls = photos.collect {|photo| slideview_image_url(photo) }
[property.id, image_urls]
end.to_json
end
properties = Property.find(:all, :limit => 10, :include => [:photos, :default_photo])
# force object instantiation
properties.each do |p|
puts 'x' if p.id.present?
end
# profile the code!!
RubyProf.start
photos = property_search_images_json_for_slideview(properties)
result = RubyProf.stop
# show output
puts photos.inspect
printer = RubyProf::FlatPrinter.new(result)
printer.print(STDOUT, 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment