Cleaning up the profile Rake task.
This commit is contained in:
parent
c8f67659bb
commit
b9d4015743
45
Rakefile
45
Rakefile
|
@ -109,26 +109,37 @@ rescue LoadError; end
|
||||||
|
|
||||||
# ----- Profiling -----
|
# ----- Profiling -----
|
||||||
|
|
||||||
desc <<END
|
begin
|
||||||
|
require 'ruby-prof'
|
||||||
|
|
||||||
|
desc <<END
|
||||||
Run a profile of haml.
|
Run a profile of haml.
|
||||||
ENGINE=str sets the engine to be profiled (Haml or Sass).
|
ENGINE=str sets the engine to be profiled. Defaults to Haml.
|
||||||
TIMES=n sets the number of runs. Defaults to 100.
|
TIMES=n sets the number of runs. Defaults to 1000.
|
||||||
FILE=n sets the file to profile. Defaults to 'standard'.
|
FILE=str sets the file to profile.
|
||||||
|
Defaults to 'standard' for Haml and 'complex' for Sass.
|
||||||
|
OUTPUT=str sets the ruby-prof output format.
|
||||||
|
Can be Flat, CallInfo, or Graph. Defaults to Flat. Defaults to Flat.
|
||||||
END
|
END
|
||||||
task :profile do
|
task :profile do
|
||||||
require 'test/profile'
|
engine = (ENV['ENGINE'] || 'haml').downcase
|
||||||
|
times = (ENV['TIMES'] || '1000').to_i
|
||||||
|
file = ENV['FILE']
|
||||||
|
|
||||||
engine = ENV['ENGINE'] && ENV['ENGINE'].downcase == 'sass' ? Sass : Haml
|
if engine == 'sass'
|
||||||
|
require 'lib/sass'
|
||||||
|
|
||||||
puts '-'*51, "Profiling #{engine}", '-'*51
|
file = File.read("#{File.dirname(__FILE__)}/test/sass/templates/#{file || 'complex'}.sass")
|
||||||
|
result = RubyProf.profile { times.times { Sass::Engine.new(file).render } }
|
||||||
|
else
|
||||||
|
require 'lib/haml'
|
||||||
|
|
||||||
args = []
|
file = File.read("#{File.dirname(__FILE__)}/test/haml/templates/#{file || 'standard'}.haml")
|
||||||
args.push ENV['TIMES'].to_i if ENV['TIMES']
|
obj = Object.new
|
||||||
args.push ENV['FILE'] if ENV['FILE']
|
Haml::Engine.new(file).def_method(obj, :render)
|
||||||
|
result = RubyProf.profile { times.times { obj.render } }
|
||||||
|
end
|
||||||
|
|
||||||
profiler = engine::Profiler.new
|
RubyProf.const_get("#{(ENV['OUTPUT'] || 'Flat').capitalize}Printer").new(result).print
|
||||||
res = profiler.profile(*args)
|
end
|
||||||
puts res
|
rescue LoadError; end
|
||||||
|
|
||||||
puts '-'*51
|
|
||||||
end
|
|
||||||
|
|
|
@ -1,65 +0,0 @@
|
||||||
require 'rubygems'
|
|
||||||
require 'active_support'
|
|
||||||
require 'action_controller'
|
|
||||||
require 'action_view'
|
|
||||||
|
|
||||||
require File.dirname(__FILE__) + '/../lib/haml'
|
|
||||||
require 'haml/template'
|
|
||||||
|
|
||||||
require 'profiler'
|
|
||||||
require 'stringio'
|
|
||||||
|
|
||||||
module Haml
|
|
||||||
# Used by both Haml::Profiler and Sass::Profiler.
|
|
||||||
# Encapsulates profiling behavior.
|
|
||||||
module AbstractProfiler
|
|
||||||
def self.profile(times, &block)
|
|
||||||
# Runs the profiler, collects information
|
|
||||||
Profiler__::start_profile
|
|
||||||
times.times &block
|
|
||||||
Profiler__::stop_profile
|
|
||||||
|
|
||||||
# Outputs information to a StringIO, returns result
|
|
||||||
io = StringIO.new
|
|
||||||
Profiler__::print_profile(io)
|
|
||||||
io.pos = 0
|
|
||||||
result = io.read
|
|
||||||
io.close
|
|
||||||
result
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# A profiler for Haml, mostly for development use. This simply implements
|
|
||||||
# the Ruby profiler for profiling haml code.
|
|
||||||
class Profiler
|
|
||||||
|
|
||||||
# Creates a new profiler that looks for templates in the base
|
|
||||||
# directory.
|
|
||||||
def initialize(base = File.join(File.dirname(__FILE__), 'haml', 'templates'))
|
|
||||||
unless base.class == ActionView::Base
|
|
||||||
@base = ActionView::Base.new(base)
|
|
||||||
else
|
|
||||||
@base = base
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Profiles haml on the given template with the given number of runs.
|
|
||||||
# The template name shouldn't have a file extension; this will
|
|
||||||
# automatically look for a haml template.
|
|
||||||
#
|
|
||||||
# Returns the results of the profiling as a string.
|
|
||||||
def profile(runs = 100, template_name = 'standard')
|
|
||||||
AbstractProfiler.profile(runs) { @base.render template_name }
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
module Sass
|
|
||||||
class Profiler
|
|
||||||
def profile(runs = 100, template_name = 'complex')
|
|
||||||
Haml::AbstractProfiler.profile(runs) do
|
|
||||||
Sass::Engine.new("#{File.dirname(__FILE__)}/sass/templates/#{template_name}.sass").render
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue