mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Ruby 1.9 and GC::Profiler updates
This commit is contained in:
parent
38c7d73e73
commit
7fbe226de5
1 changed files with 60 additions and 6 deletions
|
@ -25,7 +25,7 @@ module ActiveSupport
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
base.class_inheritable_hash :profile_options
|
base.class_inheritable_hash :profile_options
|
||||||
base.profile_options = DEFAULTS.dup
|
base.profile_options = DEFAULTS
|
||||||
end
|
end
|
||||||
|
|
||||||
def full_test_name
|
def full_test_name
|
||||||
|
@ -34,6 +34,7 @@ module ActiveSupport
|
||||||
|
|
||||||
def run(result)
|
def run(result)
|
||||||
return if method_name =~ /^default_test$/
|
return if method_name =~ /^default_test$/
|
||||||
|
self.profile_options ||= DEFAULTS
|
||||||
|
|
||||||
yield(self.class::STARTED, name)
|
yield(self.class::STARTED, name)
|
||||||
@_result = result
|
@_result = result
|
||||||
|
@ -43,8 +44,6 @@ module ActiveSupport
|
||||||
if klass = Metrics[metric_name.to_sym]
|
if klass = Metrics[metric_name.to_sym]
|
||||||
run_profile(klass.new)
|
run_profile(klass.new)
|
||||||
result.add_run
|
result.add_run
|
||||||
else
|
|
||||||
$stderr.puts '%20s: unsupported' % metric_name.to_s
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -164,7 +163,14 @@ module ActiveSupport
|
||||||
end
|
end
|
||||||
|
|
||||||
class Profiler < Performer
|
class Profiler < Performer
|
||||||
|
def initialize(*args)
|
||||||
|
super
|
||||||
|
@supported = @metric.measure_mode rescue false
|
||||||
|
end
|
||||||
|
|
||||||
def run
|
def run
|
||||||
|
return unless @supported
|
||||||
|
|
||||||
RubyProf.measure_mode = @metric.measure_mode
|
RubyProf.measure_mode = @metric.measure_mode
|
||||||
RubyProf.start
|
RubyProf.start
|
||||||
RubyProf.pause
|
RubyProf.pause
|
||||||
|
@ -173,7 +179,17 @@ module ActiveSupport
|
||||||
@total = @data.threads.values.sum(0) { |method_infos| method_infos.sort.last.total_time }
|
@total = @data.threads.values.sum(0) { |method_infos| method_infos.sort.last.total_time }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def report
|
||||||
|
if @supported
|
||||||
|
super
|
||||||
|
else
|
||||||
|
'%20s: unsupported' % @metric.name
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def record
|
def record
|
||||||
|
return unless @supported
|
||||||
|
|
||||||
klasses = profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact
|
klasses = profile_options[:formats].map { |f| RubyProf.const_get("#{f.to_s.camelize}Printer") }.compact
|
||||||
|
|
||||||
klasses.each do |klass|
|
klasses.each do |klass|
|
||||||
|
@ -202,8 +218,7 @@ module ActiveSupport
|
||||||
|
|
||||||
module Metrics
|
module Metrics
|
||||||
def self.[](name)
|
def self.[](name)
|
||||||
klass = const_get(name.to_s.camelize)
|
const_get(name.to_s.camelize)
|
||||||
klass if klass::Mode
|
|
||||||
rescue NameError
|
rescue NameError
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
@ -250,6 +265,16 @@ module ActiveSupport
|
||||||
ensure
|
ensure
|
||||||
GC.disable_stats
|
GC.disable_stats
|
||||||
end
|
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
|
else
|
||||||
def with_gc_stats
|
def with_gc_stats
|
||||||
yield
|
yield
|
||||||
|
@ -310,7 +335,7 @@ module ActiveSupport
|
||||||
RubyProf.measure_memory / 1024.0
|
RubyProf.measure_memory / 1024.0
|
||||||
end
|
end
|
||||||
|
|
||||||
# Ruby 1.8 + adymo patch
|
# Ruby 1.8 + railsbench patch
|
||||||
elsif GC.respond_to?(:allocated_size)
|
elsif GC.respond_to?(:allocated_size)
|
||||||
def measure
|
def measure
|
||||||
GC.allocated_size / 1024.0
|
GC.allocated_size / 1024.0
|
||||||
|
@ -322,11 +347,27 @@ module ActiveSupport
|
||||||
GC.heap_info['heap_current_memory'] / 1024.0
|
GC.heap_info['heap_current_memory'] / 1024.0
|
||||||
end
|
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
|
# Ruby 1.9 unpatched
|
||||||
elsif GC.respond_to?(:malloc_allocated_size)
|
elsif GC.respond_to?(:malloc_allocated_size)
|
||||||
def measure
|
def measure
|
||||||
GC.malloc_allocated_size / 1024.0
|
GC.malloc_allocated_size / 1024.0
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def format(measurement)
|
def format(measurement)
|
||||||
|
@ -341,10 +382,23 @@ module ActiveSupport
|
||||||
def measure
|
def measure
|
||||||
RubyProf.measure_allocations
|
RubyProf.measure_allocations
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Ruby 1.8 + railsbench patch
|
||||||
elsif ObjectSpace.respond_to?(:allocated_objects)
|
elsif ObjectSpace.respond_to?(:allocated_objects)
|
||||||
def measure
|
def measure
|
||||||
ObjectSpace.allocated_objects
|
ObjectSpace.allocated_objects
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
def format(measurement)
|
def format(measurement)
|
||||||
|
|
Loading…
Reference in a new issue