diff options
author | Gonçalo Silva <goncalossilva@gmail.com> | 2011-03-28 03:11:42 +0100 |
---|---|---|
committer | Gonçalo Silva <goncalossilva@gmail.com> | 2011-03-28 03:11:42 +0100 |
commit | c401ad98536b8038bf20f000b41e3f185de0ab7c (patch) | |
tree | 5e93b9dba053434f1c9350f989b44fe9c834a876 /activesupport/lib/active_support/testing | |
parent | 983bd01f42a9a3f106c27b37b1074c648263e861 (diff) | |
download | rails-c401ad98536b8038bf20f000b41e3f185de0ab7c.tar.gz rails-c401ad98536b8038bf20f000b41e3f185de0ab7c.tar.bz2 rails-c401ad98536b8038bf20f000b41e3f185de0ab7c.zip |
improved options handling in performance tests
Diffstat (limited to 'activesupport/lib/active_support/testing')
4 files changed, 42 insertions, 39 deletions
diff --git a/activesupport/lib/active_support/testing/performance.rb b/activesupport/lib/active_support/testing/performance.rb index 6a0fc9af35..3195750841 100644 --- a/activesupport/lib/active_support/testing/performance.rb +++ b/activesupport/lib/active_support/testing/performance.rb @@ -7,7 +7,7 @@ require 'action_view/helpers/number_helper' module ActiveSupport module Testing module Performance - # modified by each implementation + # each implementation should define metrics and freeze the defaults DEFAULTS = if ARGV.include?('--benchmark') # HAX for rake test { :benchmark => true, @@ -21,13 +21,16 @@ module ActiveSupport def self.included(base) base.superclass_delegating_accessor :profile_options - base.profile_options = DEFAULTS + end + + def full_profile_options + DEFAULTS.merge(profile_options) end def full_test_name "#{self.class.name}##{method_name}" end - + def run(result) return if method_name =~ /^default_test$/ @@ -35,13 +38,13 @@ module ActiveSupport @_result = result run_warmup - if profile_options && metrics = profile_options[:metrics] + if full_profile_options && metrics = full_profile_options[:metrics] metrics.each do |metric_name| if klass = Metrics[metric_name.to_sym] run_profile(klass.new) result.add_run else - puts '%20s: unsupported' % metric_name + puts '%20s: unsupported' % @metric.name end end end @@ -83,7 +86,7 @@ module ActiveSupport end def run_profile(metric) - klass = profile_options[:benchmark] ? Benchmarker : Profiler + klass = full_profile_options[:benchmark] ? Benchmarker : Profiler performer = klass.new(self, metric) performer.run @@ -92,20 +95,20 @@ module ActiveSupport end class Performer - delegate :run_test, :profile_options, :full_test_name, :to => :@harness + delegate :run_test, :full_profile_options, :full_test_name, :to => :@harness def initialize(harness, metric) @harness, @metric = harness, metric end def report - rate = @total / profile_options[:runs] + rate = @total / full_profile_options[:runs] '%20s: %s' % [@metric.name, @metric.format(rate)] end protected def output_filename - "#{profile_options[:output]}/#{full_test_name}_#{@metric.name}" + "#{full_profile_options[:output]}/#{full_test_name}_#{@metric.name}" end end @@ -122,12 +125,12 @@ module ActiveSupport class Benchmarker < Performer def run - profile_options[:runs].to_i.times { run_test(@metric, :benchmark) } + full_profile_options[:runs].to_i.times { run_test(@metric, :benchmark) } @total = @metric.total end def record - avg = @metric.total / profile_options[:runs].to_i + avg = @metric.total / full_profile_options[:runs].to_i now = Time.now.utc.xmlschema with_output_file do |file| file.puts "#{avg},#{now},#{environment}" diff --git a/activesupport/lib/active_support/testing/performance/jruby.rb b/activesupport/lib/active_support/testing/performance/jruby.rb index 6d94dacdd6..e489c1f0dd 100644 --- a/activesupport/lib/active_support/testing/performance/jruby.rb +++ b/activesupport/lib/active_support/testing/performance/jruby.rb @@ -5,13 +5,13 @@ import java.lang.management.ManagementFactory module ActiveSupport module Testing module Performance - if ARGV.include?('--benchmark') - DEFAULTS.merge!({:metrics => [:wall_time, :user_time, :memory, :gc_runs, :gc_time]}) - else - DEFAULTS.merge!( + DEFAULTS.merge!( + if ARGV.include?('--benchmark') + {:metrics => [:wall_time, :user_time, :memory, :gc_runs, :gc_time]} + else { :metrics => [:wall_time], - :formats => [:flat, :graph] }) - end + :formats => [:flat, :graph] } + end).freeze protected def run_gc @@ -23,7 +23,7 @@ module ActiveSupport class Profiler < Performer def run @data = JRuby::Profiler.profile do - profile_options[:runs].to_i.times { run_test(@metric, :profile) } + full_profile_options[:runs].to_i.times { run_test(@metric, :profile) } end profile_printer = JRuby::Profiler::GraphProfilePrinter.new(@data) @@ -37,7 +37,7 @@ module ActiveSupport end def record - klasses = profile_options[:formats].map { |f| JRuby::Profiler.const_get("#{f.to_s.camelize}ProfilePrinter") }.compact + klasses = full_profile_options[:formats].map { |f| JRuby::Profiler.const_get("#{f.to_s.camelize}ProfilePrinter") }.compact klasses.each do |klass| fname = output_filename(klass) diff --git a/activesupport/lib/active_support/testing/performance/rubinius.rb b/activesupport/lib/active_support/testing/performance/rubinius.rb index bdbb530b37..55b142e20c 100644 --- a/activesupport/lib/active_support/testing/performance/rubinius.rb +++ b/activesupport/lib/active_support/testing/performance/rubinius.rb @@ -3,13 +3,13 @@ require 'rubinius/agent' module ActiveSupport module Testing module Performance - if ARGV.include?('--benchmark') - DEFAULTS.merge!({:metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time]}) - else - DEFAULTS.merge!( + DEFAULTS.merge!( + if ARGV.include?('--benchmark') + {:metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time]} + else { :metrics => [:wall_time], - :formats => [:flat, :graph] }) - end + :formats => [:flat, :graph] } + end).freeze protected def run_gc @@ -23,7 +23,7 @@ module ActiveSupport @profiler = Rubinius::Profiler::Instrumenter.new @profiler.profile(false) do - profile_options[:runs].to_i.times { run_test(@metric, :profile) } + full_profile_options[:runs].to_i.times { run_test(@metric, :profile) } end @total = @profiler.info[:runtime] / 1000 / 1000 / 1000.0 # seconds @@ -34,13 +34,13 @@ module ActiveSupport end def record - if(profile_options[:formats].include?(:flat)) + if(full_profile_options[:formats].include?(:flat)) create_path_and_open_file(:flat) do |file| @profiler.show(file) end end - if(profile_options[:formats].include?(:graph)) + if(full_profile_options[:formats].include?(:graph)) create_path_and_open_file(:graph) do |file| @profiler.show(file) end diff --git a/activesupport/lib/active_support/testing/performance/ruby.rb b/activesupport/lib/active_support/testing/performance/ruby.rb index b36b201531..a8b26b85eb 100644 --- a/activesupport/lib/active_support/testing/performance/ruby.rb +++ b/activesupport/lib/active_support/testing/performance/ruby.rb @@ -8,14 +8,14 @@ end module ActiveSupport module Testing module Performance - if ARGV.include?('--benchmark') - DEFAULTS.merge!({:metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time]}) - else - DEFAULTS.merge!( + DEFAULTS.merge!( + if ARGV.include?('--benchmark') + ({:metrics => [:wall_time, :memory, :objects, :gc_runs, :gc_time]} + else { :min_percent => 0.01, :metrics => [:process_time, :memory, :objects], - :formats => [:flat, :graph_html, :call_tree] }) - end + :formats => [:flat, :graph_html, :call_tree] } + end).freeze protected def run_gc @@ -36,7 +36,7 @@ module ActiveSupport RubyProf.measure_mode = @metric.measure_mode RubyProf.start RubyProf.pause - profile_options[:runs].to_i.times { run_test(@metric, :profile) } + full_profile_options[:runs].to_i.times { run_test(@metric, :profile) } @data = RubyProf.stop @total = @data.threads.values.sum(0) { |method_infos| method_infos.max.total_time } end @@ -52,13 +52,13 @@ module ActiveSupport def record return unless @supported - klasses = profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact + klasses = full_profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact klasses.each do |klass| fname = output_filename(klass) FileUtils.mkdir_p(File.dirname(fname)) File.open(fname, 'wb') do |file| - klass.new(@data).print(file, profile_options.slice(:min_percent)) + klass.new(@data).print(file, full_profile_options.slice(:min_percent)) end end end @@ -166,9 +166,9 @@ module ActiveSupport end end -if RUBY_VERSION >= '1.9.2' +if RUBY_VERSION.between?('1.9.2', '2.0') require 'active_support/testing/performance/ruby/yarv' -elsif RUBY_VERSION >= '1.8.6' +elsif RUBY_VERSION.between?('1.8.6', '1.9') require 'active_support/testing/performance/ruby/mri' else $stderr.puts 'Update your ruby interpreter to be able to run benchmarks.' |