Cleaning up the profile Rake task.
This commit is contained in:
parent
c8f67659bb
commit
b9d4015743
41
Rakefile
41
Rakefile
|
@ -109,26 +109,37 @@ rescue LoadError; end
|
|||
|
||||
# ----- Profiling -----
|
||||
|
||||
begin
|
||||
require 'ruby-prof'
|
||||
|
||||
desc <<END
|
||||
Run a profile of haml.
|
||||
ENGINE=str sets the engine to be profiled (Haml or Sass).
|
||||
TIMES=n sets the number of runs. Defaults to 100.
|
||||
FILE=n sets the file to profile. Defaults to 'standard'.
|
||||
ENGINE=str sets the engine to be profiled. Defaults to Haml.
|
||||
TIMES=n sets the number of runs. Defaults to 1000.
|
||||
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
|
||||
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 = []
|
||||
args.push ENV['TIMES'].to_i if ENV['TIMES']
|
||||
args.push ENV['FILE'] if ENV['FILE']
|
||||
|
||||
profiler = engine::Profiler.new
|
||||
res = profiler.profile(*args)
|
||||
puts res
|
||||
|
||||
puts '-'*51
|
||||
file = File.read("#{File.dirname(__FILE__)}/test/haml/templates/#{file || 'standard'}.haml")
|
||||
obj = Object.new
|
||||
Haml::Engine.new(file).def_method(obj, :render)
|
||||
result = RubyProf.profile { times.times { obj.render } }
|
||||
end
|
||||
|
||||
RubyProf.const_get("#{(ENV['OUTPUT'] || 'Flat').capitalize}Printer").new(result).print
|
||||
end
|
||||
rescue LoadError; 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