Permalink
Browse files

extracted more generic methods, added a note for methods that need to…

… be overridden by each implementation
  • Loading branch information...
1 parent c50df63 commit e57d264f3795537cc3a5be61d766f956f882a5bd @goncalossilva goncalossilva committed Mar 25, 2011
@@ -3,17 +3,6 @@
require 'active_support/core_ext/class/delegating_attributes'
require 'active_support/core_ext/string/inflections'
-if !defined?(RUBY_ENGINE) or RUBY_ENGINE == "ruby" # MRI 1.8 or 1.9
- begin
- require 'ruby-prof'
- rescue LoadError
- $stderr.puts "Specify ruby-prof as application's dependency in Gemfile to run benchmarks."
- exit
- end
-
- require 'active_support/testing/performance/mri'
-end
-
module ActiveSupport
module Testing
module Performance
@@ -80,6 +69,19 @@ def run_test(metric, mode)
end
protected
+ # overridden by each implementation
+ def run_gc; end
+
+ def run_warmup
+ run_gc
+
+ time = Metrics::Time.new
+ run_test(time, :benchmark)
+ puts "%s (%s warmup)" % [full_test_name, time.format(time.total)]
+
+ run_gc
+ end
+
def run_profile(metric)
klass = profile_options[:benchmark] ? Benchmarker : Profiler
performer = klass.new(self, metric)
@@ -106,6 +108,13 @@ def output_filename
"#{profile_options[:output]}/#{full_test_name}_#{@metric.name}"
end
end
+
+ # overridden by each implementation
+ class Profiler < Performer
+ def run; end
+ def report; end
+ def record; end
+ end
class Benchmarker < Performer
def run
@@ -180,21 +189,20 @@ def name
@name ||= self.class.name.demodulize.underscore
end
- def measure_mode
- self.class::Mode
- end
-
- def measure
- 0
- end
-
def benchmark
with_gc_stats do
before = measure
yield
@total += (measure - before)
end
end
+
+ # overridden by each implementation
+ def profile; end
+
+ protected
+ # overridden by each implementation
+ def with_gc_stats; end
end
class Time < Base
@@ -210,7 +218,60 @@ def format(measurement)
end
end
end
+
+ class ProcessTime < Time
+ # overridden by each implementation
+ def measure; end
+ end
+
+ class WallTime < Time
+ # overridden by each implementation
+ def measure; end
+ end
+
+ class CpuTime < Time
+ # overridden by each implementation
+ def measure; end
+ end
+
+ class Memory < Base
+ # overridden by each implementation
+ def measure; end
+
+ def format(measurement)
+ '%.2f KB' % measurement
+ end
+ end
+
+ class Objects < Base
+ # overridden by each implementation
+ def measure; end
+
+ def format(measurement)
+ measurement.to_i.to_s
+ end
+ end
+
+ class GcRuns < Base
+ # overridden by each implementation
+ def measure; end
+
+ def format(measurement)
+ measurement.to_i.to_s
+ end
+ end
+
+ class GcTime < Base
+ # overridden by each implementation
+ def measure; end
+
+ def format(measurement)
+ '%.2f ms' % measurement
+ end
+ end
end
end
end
end
+
+require 'active_support/testing/performance/mri'
@@ -8,17 +8,12 @@
module ActiveSupport
module Testing
module Performance
+
protected
- def run_warmup
- GC.start
-
- time = Metrics::Time.new
- run_test(time, :benchmark)
- puts "%s (%s warmup)" % [full_test_name, time.format(time.total)]
-
+ def run_gc
GC.start
end
-
+
class Performer; end
class Profiler < Performer
@@ -81,6 +76,10 @@ def output_filename(printer_class)
module Metrics
class Base
+ def measure_mode
+ self.class::Mode
+ end
+
def profile
RubyProf.resume
yield
@@ -107,11 +106,6 @@ def with_gc_stats
ensure
GC.disable_stats
end
-
- else
- def with_gc_stats
- yield
- end
end
end
@@ -162,10 +156,6 @@ def measure
RubyProf.measure_memory / 1024.0
end
end
-
- def format(measurement)
- '%.2f KB' % measurement
- end
end
class Objects < Base
@@ -183,10 +173,6 @@ def measure
RubyProf.measure_allocations
end
end
-
- def format(measurement)
- measurement.to_i.to_s
- end
end
class GcRuns < Base
@@ -204,10 +190,6 @@ def measure
RubyProf.measure_gc_runs
end
end
-
- def format(measurement)
- measurement.to_i.to_s
- end
end
class GcTime < Base
@@ -225,10 +207,6 @@ def measure
RubyProf.measure_gc_time / 1000
end
end
-
- def format(measurement)
- '%.2f ms' % measurement
- end
end
end
end

0 comments on commit e57d264

Please sign in to comment.