diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2011-05-17 14:28:40 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2011-05-17 14:28:40 -0700 |
commit | b77e032ccf50249685c672c2d75f173db2ee2dbf (patch) | |
tree | 378cb69c599c5d0198731d1ffe6696e427ffe42c /railties/lib | |
parent | 1d8d799a01956525eef9bf22088c34e0d1ad4423 (diff) | |
parent | 7fcf0ca69e18841be9fb0dcce2721f4ed2c8eca9 (diff) | |
download | rails-b77e032ccf50249685c672c2d75f173db2ee2dbf.tar.gz rails-b77e032ccf50249685c672c2d75f173db2ee2dbf.tar.bz2 rails-b77e032ccf50249685c672c2d75f173db2ee2dbf.zip |
Merge pull request #247 from goncalossilva/performance_test
Performance tests improved
Diffstat (limited to 'railties/lib')
4 files changed, 63 insertions, 64 deletions
diff --git a/railties/lib/rails/commands/benchmarker.rb b/railties/lib/rails/commands/benchmarker.rb index f230f405c0..b06c915ac3 100644 --- a/railties/lib/rails/commands/benchmarker.rb +++ b/railties/lib/rails/commands/benchmarker.rb @@ -1,25 +1,34 @@ -require 'active_support/core_ext/object/inclusion' +require 'optparse' +require 'rails/test_help' +require 'rails/performance_test_help' -if ARGV.first.in?([nil, "-h", "--help"]) - puts "Usage: rails benchmarker [times] 'Person.expensive_way' 'Person.another_expensive_way' ..." - exit 1 -end +ARGV.push('--benchmark') # HAX +require 'active_support/testing/performance' +ARGV.pop -begin - N = Integer(ARGV.first) - ARGV.shift -rescue ArgumentError - N = 1 +def options + options = {} + defaults = ActiveSupport::Testing::Performance::DEFAULTS + + OptionParser.new do |opt| + opt.banner = "Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]" + opt.on('-r', '--runs N', Numeric, 'Number of runs.', "Default: #{defaults[:runs]}") { |r| options[:runs] = r } + opt.on('-o', '--output PATH', String, 'Directory to use when writing the results.', "Default: #{defaults[:output]}") { |o| options[:output] = o } + opt.on('-m', '--metrics a,b,c', Array, 'Metrics to use.', "Default: #{defaults[:metrics].join(",")}") { |m| options[:metrics] = m.map(&:to_sym) } + opt.parse!(ARGV) + end + + options end -require 'benchmark' -include Benchmark - -# Don't include compilation in the benchmark -ARGV.each { |expression| eval(expression) } - -bm(6) do |x| - ARGV.each_with_index do |expression, idx| - x.report("##{idx + 1}") { N.times { eval(expression) } } +class BenchmarkerTest < ActionDispatch::PerformanceTest + self.profile_options = options + + ARGV.each do |expression| + eval <<-RUBY + def test_#{expression.parameterize('_')} + #{expression} + end + RUBY end end diff --git a/railties/lib/rails/commands/profiler.rb b/railties/lib/rails/commands/profiler.rb index 7959d8a981..94cf32d32d 100644 --- a/railties/lib/rails/commands/profiler.rb +++ b/railties/lib/rails/commands/profiler.rb @@ -1,48 +1,32 @@ -require 'active_support/core_ext/object/inclusion' +require 'optparse' +require 'rails/test_help' +require 'rails/performance_test_help' +require 'active_support/testing/performance' -if ARGV.first.in?([nil, "-h", "--help"]) - $stderr.puts "Usage: rails profiler 'Person.expensive_method(10)' [times] [flat|graph|graph_html]" - exit(1) -end - -# Define a method to profile. -if ARGV[1] and ARGV[1].to_i > 1 - eval "def profile_me() #{ARGV[1]}.times { #{ARGV[0]} } end" -else - eval "def profile_me() #{ARGV[0]} end" +def options + options = {} + defaults = ActiveSupport::Testing::Performance::DEFAULTS + + OptionParser.new do |opt| + opt.banner = "Usage: rails benchmarker 'Ruby.code' 'Ruby.more_code' ... [OPTS]" + opt.on('-r', '--runs N', Numeric, 'Number of runs.', "Default: #{defaults[:runs]}") { |r| options[:runs] = r } + opt.on('-o', '--output PATH', String, 'Directory to use when writing the results.', "Default: #{defaults[:output]}") { |o| options[:output] = o } + opt.on('-m', '--metrics a,b,c', Array, 'Metrics to use.', "Default: #{defaults[:metrics].join(",")}") { |m| options[:metrics] = m.map(&:to_sym) } + opt.on('-f', '--formats x,y,z', Array, 'Formats to output to.', "Default: #{defaults[:formats].join(",")}") { |m| options[:formats] = m.map(&:to_sym) } + opt.parse!(ARGV) + end + + options end -# Use the ruby-prof extension if available. Fall back to stdlib profiler. -begin - begin - require "ruby-prof" - $stderr.puts 'Using the ruby-prof extension.' - RubyProf.measure_mode = RubyProf::WALL_TIME - RubyProf.start - profile_me - results = RubyProf.stop - if ARGV[2] - printer_class = RubyProf.const_get((ARGV[2] + "_printer").classify) - else - printer_class = RubyProf::FlatPrinter - end - printer = printer_class.new(results) - printer.print($stderr) - rescue LoadError - require "prof" - $stderr.puts 'Using the old ruby-prof extension.' - Prof.clock_mode = Prof::GETTIMEOFDAY - Prof.start - profile_me - results = Prof.stop - require 'rubyprof_ext' - Prof.print_profile(results, $stderr) +class ProfilerTest < ActionDispatch::PerformanceTest + self.profile_options = options + + ARGV.each do |expression| + eval <<-RUBY + def test_#{expression.parameterize('_')} + #{expression} + end + RUBY end -rescue LoadError - require 'profiler' - $stderr.puts 'Using the standard Ruby profiler.' - Profiler__.start_profile - profile_me - Profiler__.stop_profile - Profiler__.print_profile($stderr) end diff --git a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb b/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb index 867fc8c985..5d1be041a5 100644 --- a/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb +++ b/railties/lib/rails/generators/rails/app/templates/test/performance/browsing_test.rb @@ -1,8 +1,11 @@ require 'test_helper' require 'rails/performance_test_help' -# Profiling results for each test method are written to tmp/performance. class BrowsingTest < ActionDispatch::PerformanceTest + # Refer to the documentation for all available options + # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] + # :output => 'tmp/performance', :formats => [:flat] } + def test_homepage get '/' end diff --git a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb index e827aa918f..14a878328b 100644 --- a/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb +++ b/railties/lib/rails/generators/test_unit/performance/templates/performance_test.rb @@ -2,7 +2,10 @@ require 'test_helper' require 'rails/performance_test_help' class <%= class_name %>Test < ActionDispatch::PerformanceTest - # Replace this with your real tests. + # Refer to the documentation for all available options + # self.profile_options = { :runs => 5, :metrics => [:wall_time, :memory] + # :output => 'tmp/performance', :formats => [:flat] } + def test_homepage get '/' end |