Skip to content

Commit

Permalink
Ruby 1.9 and GC::Profiler updates
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Aug 18, 2008
1 parent 38c7d73 commit 7fbe226
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions activesupport/lib/active_support/testing/performance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Performance

def self.included(base)
base.class_inheritable_hash :profile_options
base.profile_options = DEFAULTS.dup
base.profile_options = DEFAULTS
end

def full_test_name
Expand All @@ -34,6 +34,7 @@ def full_test_name

def run(result)
return if method_name =~ /^default_test$/
self.profile_options ||= DEFAULTS

yield(self.class::STARTED, name)
@_result = result
Expand All @@ -43,8 +44,6 @@ def run(result)
if klass = Metrics[metric_name.to_sym]
run_profile(klass.new)
result.add_run
else
$stderr.puts '%20s: unsupported' % metric_name.to_s
end
end

Expand Down Expand Up @@ -164,7 +163,14 @@ def output_filename
end

class Profiler < Performer
def initialize(*args)
super
@supported = @metric.measure_mode rescue false
end

def run
return unless @supported

RubyProf.measure_mode = @metric.measure_mode
RubyProf.start
RubyProf.pause
Expand All @@ -173,7 +179,17 @@ def run
@total = @data.threads.values.sum(0) { |method_infos| method_infos.sort.last.total_time }
end

def report
if @supported
super
else
'%20s: unsupported' % @metric.name
end
end

def record
return unless @supported

klasses = profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact

klasses.each do |klass|
Expand Down Expand Up @@ -202,8 +218,7 @@ def output_filename(printer_class)

module Metrics
def self.[](name)
klass = const_get(name.to_s.camelize)
klass if klass::Mode
const_get(name.to_s.camelize)
rescue NameError
nil
end
Expand Down Expand Up @@ -250,6 +265,16 @@ def with_gc_stats
ensure
GC.disable_stats
end
elsif defined?(GC::Profiler)
def with_gc_stats
GC.start
GC.disable
GC::Profiler.enable
yield
ensure
GC::Profiler.disable
GC.enable
end
else
def with_gc_stats
yield
Expand Down Expand Up @@ -310,7 +335,7 @@ def measure
RubyProf.measure_memory / 1024.0
end

# Ruby 1.8 + adymo patch
# Ruby 1.8 + railsbench patch
elsif GC.respond_to?(:allocated_size)
def measure
GC.allocated_size / 1024.0
Expand All @@ -322,11 +347,27 @@ def measure
GC.heap_info['heap_current_memory'] / 1024.0
end

# Ruby 1.9 with total_malloc_allocated_size patch
elsif GC.respond_to?(:malloc_total_allocated_size)
def measure
GC.total_malloc_allocated_size / 1024.0
end

# Ruby 1.9 unpatched
elsif GC.respond_to?(:malloc_allocated_size)
def measure
GC.malloc_allocated_size / 1024.0
end

# Ruby 1.9 + GC profiler patch
elsif defined?(GC::Profiler)
def measure
GC.enable
GC.start
kb = GC::Profiler.data.last[:HEAP_USE_SIZE] / 1024.0
GC.disable
kb
end
end

def format(measurement)
Expand All @@ -341,10 +382,23 @@ class Objects < Base
def measure
RubyProf.measure_allocations
end

# Ruby 1.8 + railsbench patch
elsif ObjectSpace.respond_to?(:allocated_objects)
def measure
ObjectSpace.allocated_objects
end

# Ruby 1.9 + GC profiler patch
elsif defined?(GC::Profiler)
def measure
GC.enable
GC.start
last = GC::Profiler.data.last
count = last[:HEAP_LIVE_OBJECTS] + last[:HEAP_FREE_OBJECTS]
GC.disable
count
end
end

def format(measurement)
Expand Down

0 comments on commit 7fbe226

Please sign in to comment.