Make benchmark.rb more standalone.

This commit is contained in:
Nathan Weizenbaum 2008-05-06 00:43:43 -07:00
parent a0c0005ebb
commit 500be8e3bd
2 changed files with 162 additions and 165 deletions

View File

@ -20,44 +20,35 @@ END
desc temp_desc.chomp
task :benchmark do
require 'test/benchmark'
puts "Running benchmarks #{ENV['TIMES']} times..." if ENV['TIMES']
times = ENV['TIMES'].to_i if ENV['TIMES']
Haml.benchmark(times || 100)
puts '-'*51
sh "ruby test/benchmark.rb #{ENV['TIMES']}"
end
# Benchmarking gets screwed up if some other tasks have been
# initialized.
unless ARGV[0] == 'benchmark'
# ----- Default: Testing ------
# ----- Default: Testing ------
desc 'Default: run unit tests.'
task :default => :test
desc 'Default: run unit tests.'
task :default => :test
require 'rake/testtask'
require 'rake/testtask'
Rake::TestTask.new do |t|
Rake::TestTask.new do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end
Rake::Task[:test].send(:add_comment, <<END)
end
Rake::Task[:test].send(:add_comment, <<END)
To run with an alternate version of Rails, make test/rails a symlink to that version.
END
# ----- Packaging -----
# ----- Packaging -----
require 'rake/gempackagetask'
require 'lib/haml'
load 'haml.gemspec'
require 'rake/gempackagetask'
require 'lib/haml'
load 'haml.gemspec'
Rake::GemPackageTask.new(HAML_GEMSPEC).define
Rake::GemPackageTask.new(HAML_GEMSPEC).define
desc "This is an internal task."
task :revision_file do
desc "This is an internal task."
task :revision_file do
if Haml.version[:rev] && !Rake.application.top_level_tasks.include?('release')
File.open('REVISION', 'w') { |f| f.puts Haml.version[:rev] }
elsif Rake.application.top_level_tasks.include?('release')
@ -65,18 +56,18 @@ END
else
File.open('REVISION', 'w') { |f| f.puts "(unknown)" }
end
end
Rake::Task[:package].prerequisites.insert(0, :revision_file)
end
Rake::Task[:package].prerequisites.insert(0, :revision_file)
# We also need to get rid of this file after packaging.
Rake::Task[:package].enhance { File.delete('REVISION') if File.exists?('REVISION') }
# We also need to get rid of this file after packaging.
Rake::Task[:package].enhance { File.delete('REVISION') if File.exists?('REVISION') }
task :install => [:package] do
task :install => [:package] do
sudo = RUBY_PLATFORM =~ /win32/ ? '' : 'sudo'
sh %{#{sudo} gem install --no-ri pkg/haml-#{File.read('VERSION').strip}}
end
end
task :release => [:package] do
task :release => [:package] do
name, version = ENV['NAME'], ENV['VERSION']
raise "Must supply NAME and VERSION for release task." unless name && version
sh %{rubyforge login}
@ -84,31 +75,31 @@ END
sh %{rubyforge add_file haml haml "#{name} (v#{version})" pkg/haml-#{version}.tar.gz}
sh %{rubyforge add_file haml haml "#{name} (v#{version})" pkg/haml-#{version}.tar.bz2}
sh %{rubyforge add_file haml haml "#{name} (v#{version})" pkg/haml-#{version}.zip}
end
end
# ----- Documentation -----
# ----- Documentation -----
begin
begin
require 'hanna/rdoctask'
rescue LoadError
rescue LoadError
require 'rake/rdoctask'
end
end
rdoc_task = Proc.new do |rdoc|
rdoc_task = Proc.new do |rdoc|
rdoc.title = 'Haml/Sass'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README.rdoc')
rdoc.rdoc_files.include('lib/**/*.rb')
rdoc.rdoc_files.exclude('lib/haml/buffer.rb')
rdoc.rdoc_files.exclude('lib/sass/tree/*')
end
end
Rake::RDocTask.new do |rdoc|
Rake::RDocTask.new do |rdoc|
rdoc_task.call(rdoc)
rdoc.rdoc_dir = 'rdoc'
end
end
Rake::RDocTask.new(:rdoc_devel) do |rdoc|
Rake::RDocTask.new(:rdoc_devel) do |rdoc|
rdoc_task.call(rdoc)
rdoc.rdoc_dir = 'rdoc_devel'
rdoc.options << '--all'
@ -118,11 +109,11 @@ END
rdoc.rdoc_files = Rake::FileList.new(*rdoc.rdoc_files.to_a)
rdoc.rdoc_files.include('lib/haml/buffer.rb')
rdoc.rdoc_files.include('lib/sass/tree/*')
end
end
# ----- Coverage -----
# ----- Coverage -----
unless not_loaded.include? 'rcov/rcovtask'
unless not_loaded.include? 'rcov/rcovtask'
Rcov::RcovTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/**/*_test.rb']
@ -132,18 +123,18 @@ END
end
t.verbose = true
end
end
end
# ----- Profiling -----
# ----- Profiling -----
temp_desc = <<-END
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
desc temp_desc.chomp
task :profile do
require 'test/profile'
engine = ENV['ENGINE'] && ENV['ENGINE'].downcase == 'sass' ? Sass : Haml
@ -159,6 +150,4 @@ END
puts res
puts '-'*51
end
end

24
test/benchmark.rb Normal file → Executable file
View File

@ -1,3 +1,16 @@
#!/usr/bin/env ruby
times = (ARGV.first || 1000).to_i
if times == 0 # Invalid parameter
puts <<END
ruby #$0 [times=1000]
Benchmark Haml against various other templating languages and Sass
on its own.
END
exit 1
end
# There's a bizarre error where ActionController tries to load a benchmark file
# and ends up finding this.
# These declarations then cause it to break.
@ -24,10 +37,7 @@ rescue LoadError
raise "The Haml benchmarks require the benchwarmer gem, available from http://github.com/wycats/benchwarmer"
end
module Haml
# Benchmarks Haml against ERB, Erubis, and Markaby and Sass on its own.
def self.benchmark(runs = 100)
Benchmark.warmer(runs) do
Benchmark.warmer(times) do
columns :haml, :erb, :erubis, :mab
titles :haml => "Haml", :erb => "ERB", :erubis => "Erubis", :mab => "Markaby"
@ -77,12 +87,10 @@ module Haml
haml { @base.render 'haml/templates/action_view' }
erb { @base.render 'haml/rhtml/action_view' }
end
end
end
Benchmark.warmer(runs) do
Benchmark.warmer(times) do
sass_template = File.read("#{File.dirname(__FILE__)}/sass/templates/complex.sass")
report("Sass") { Sass::Engine.new(sass_template).render }
end
end
end