From eb13419f3b27210d665193f40c0546db37262057 Mon Sep 17 00:00:00 2001 From: nex3 Date: Sun, 7 Jan 2007 06:29:12 +0000 Subject: [PATCH] Profiling works for Sass. git-svn-id: svn://hamptoncatlin.com/haml/trunk@281 7063305b-7217-0410-af8c-cdc13e5119b9 --- Rakefile | 7 +++++-- TODO | 1 - test/profile.rb | 45 +++++++++++++++++++++++++++++++-------------- 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/Rakefile b/Rakefile index 3dde01a0..5e0e5a7e 100644 --- a/Rakefile +++ b/Rakefile @@ -141,20 +141,23 @@ unless ARGV[0] == 'benchmark' temp_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'. END desc temp_desc.chomp task :profile do require 'test/profile' + + engine = ENV['ENGINE'] && ENV['ENGINE'].downcase == 'sass' ? Sass : Haml - puts '-'*51, "Profiling Haml::Template", '-'*51 + puts '-'*51, "Profiling #{engine}", '-'*51 args = [] args.push ENV['TIMES'].to_i if ENV['TIMES'] args.push ENV['FILE'] if ENV['FILE'] - profiler = Haml::Profiler.new + profiler = engine::Profiler.new res = profiler.profile(*args) puts res diff --git a/TODO b/TODO index 78d939b1..7f09e444 100644 --- a/TODO +++ b/TODO @@ -1,5 +1,4 @@ Testing: - Test Sass for alternate options settings Add Sass support for various utilities (benchmark, profile) Documentation: diff --git a/test/profile.rb b/test/profile.rb index 0e76a7e1..26275bc7 100644 --- a/test/profile.rb +++ b/test/profile.rb @@ -7,7 +7,26 @@ require 'profiler' require 'stringio' module Haml - # A profiler for haml, mostly for development use. This simply implements + # 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 @@ -28,19 +47,17 @@ module Haml # # Returns the results of the profiling as a string. def profile(runs = 100, template_name = 'standard') - # Runs the profiler, collects information - Profiler__::start_profile - runs.times { @base.render template_name } - 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 - return result + 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